[Templates] custom filters and chaining

Josh Rosenbaum josh@infogears.com
Wed, 24 May 2006 11:02:37 -0600


Dustin Frazier wrote:
 > Second question: I can't quite figure out the syntax for chaining 
> filters.  I would like to replace:
>  
> [% FILTER indent("\t") %]
> [% FILTER wrap(95) %]
> [% FILTER html_entity %]
> A long block of raw text to be converted to HTML, wrapped, and indented...
> [% END %]
> [% END %]
> [% END %]
>  
> With:
>  
> [% FILTER html_entity | wrap(95) | indent("\t") %]
> ...
>  
> The rather short explanation of filter chaining in the documentation 
> seems to imply (to me, at least) that this is possible.  However, I get 
> a syntax error that complains about the "|" after the first filter 
> (html_entity).
>  
> Either solution, a custom filter that encapsulates a few others, or the 
> ability to chain, would sure save me a lot of typing...

I don't use ttree so I don't know if the following is possible or not, but I'd imagine so.

If you don't use a custom filter, then some other solutions might be to do:
[% result = BLOCK %]
A long block of raw text to be converted to HTML, wrapped, and indented...
[% END %]
[% result | html_entity | wrap(95) | indent("\t") %]

OR:

[% BLOCK myblock %]
A long block of raw text to be converted to HTML, wrapped, and indented...
[% END %]
[% PROCESS myblock | html_entity | wrap(95) | indent("\t") %]

OR:

You might even be able to use a wrapper. (You can keep reusing it too.)
[% BLOCK myfilters %]
[% content | html_entity | wrap(95) | indent("\t") %]
[% END %]
[% WRAPPER myfilters %]
A long block of raw text to be converted to HTML, wrapped, and indented...
[% END %]


Disclaimer: I have not tested any of these, but they sound like they should work. At the very least they might give you some ideas.

-- Josh