Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output iteration in textarea #231

Open
christiangarsia opened this issue May 14, 2020 · 5 comments
Open

Output iteration in textarea #231

christiangarsia opened this issue May 14, 2020 · 5 comments

Comments

@christiangarsia
Copy link

Possibly I'm completely missing something, but I can't figure out how to iterate over an array and output that list within a text area followed by "\r\n" after each value.

eg. ["foo", "bar"] would output as

<textarea> foo \r\n bar \r\n </textarea>
@cj-clx
Copy link

cj-clx commented Jun 19, 2020

I think this is largely a limitation stemming from HTML. All HTML <input> types take a single value attribute, including the textarea type. So I don't think there is any way that Transphporm can easily append a new string to the previous iteration's string without a lot of new grammar being added. Not impossible, but difficult and perhaps undesirable for such a single special case.

@solleer
Copy link
Collaborator

solleer commented Jul 6, 2020

I made a transphporm extension for calling native php functions on data in the tss which could be used to implode an array. I can share the simple extension I made if you wish.

@jpeurich
Copy link

jpeurich commented Jul 6, 2020

I made a transphporm extension for calling native php functions on data in the tss which could be used to implode an array. I can share the simple extension I made if you wish.

This extension seems like a feature that I'd like to use. Please provide an example of how to use it.

@christiangarsia
Copy link
Author

I made a transphporm extension for calling native php functions on data in the tss which could be used to implode an array. I can share the simple extension I made if you wish.

Thanks for the tip. I’d love to know how to use that extension you made.

@solleer
Copy link
Collaborator

solleer commented Jul 14, 2020

class PhpFunc implements \Transphporm\TSSFunction {
    public function run(array $args, \DomElement $element) {
        return call_user_func(...$args);
    }
}
class PhpModule implements \Transphporm\Module {
    public function load(\Transphporm\Config $config) {
        $functionSet = $config->getFunctionSet();
        $functionSet->addFunction('php', new PhpFunc());
    }
}

Above is the classes needed. I have them in a namespace in my actual code and removed some extra stuff I add with the module. Just load the module through the loadModule method.

Example basic use case

<?php
require "vendor/autoload.php";

$xml = '<span></span>';

$tss = "span  { content: php('implode', ', ', data()); } ";

$data = ['A', 'B', 'C'];

$template = new Transphporm\Builder($xml, $tss);
echo $template->output($data)->body . PHP_EOL;

would return

<span>A,B,C</span>

The function when used in transphporm takes the php function name to call as the first argument and then the following arguments are what is passed to the function being called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants