bcosca/fatfree — pull request #550 by ikkez (2014-02-13 01:37:18+00:00)
Title: enhanced template token rendering

Hi Bong.
We had the question how to format little text vars in template in IRC yesterday. I thought it would be nice to use the format flag for template tokens here to do so, just like `{{'

{0}

',@title | format }}`.

This works great, but when we put that format string into the hive as well and do `{{@formatAsHeadline,@title | format }}`, we run into trouble with the auto-escaping (`ESCAPE`), because the format-string will be escaped before the format happens in template rendering. So we would end up with a result like `<h1>Headline</h1>`, which is not what we wanted.
Of cause we don't want to turn off the auto-escaping, nor stupid hacks,.. so i came up with an idea:
why not make the token suffixes / modifiers chainable..

currently we have `{{ expr | esc }}`, `{{ expr | raw }}` and `{{ expr, arg1,... | format }}`. Well this problem would be fixed if we could do something like `{{ expr, arg1,... | format raw }}` - first do the format, then convert to raw - awesome!

I just made some slight modifications to the Preview class that handles this part. And i also tried to make it extendable! In a similar way Template->extend works. New "Modifiers" could be added with `\Preview::instance()->modify('crop', '\App\Modifiers->crop');`

So you are now able to crop your text in templates easiely: `{{ @text, 150 | crop esc }}`.
I guess there are also many other situations where this could be useful, or what do you folks think?

I used Base->relay in the template to get this chain working.. to the first modifier in line starts, and returns the result to the next one.

The only thing i could not include yet: `Template->extend` is able to save a callback / lambda function... this does not work here, because custom tags are build at render-time but token modifiers impact on execution of the pre-renderer / cached template. And at this point the lambda functions could be away, and putting a Closure inline into the pre-render template does not work... at least, i have no idea atm.

so far. regards. cheers :beer: