Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Default filter in TT

by morgon (Priest)
on Jan 01, 2011 at 18:28 UTC ( [id://880014]=perlquestion: print w/replies, xml ) Need Help??

morgon has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am using the template toolkit to produce html and xml.

When e.g. producing html I use filters to properly escape template-variables like this:

<h1>[% hubba FILTER html %]</h1>
Now on my templates basically all variables would need to go through such a filter and I find it tedious to add the FILTER-directive to all variables, so I wonder if there is a way to configure TT in such a way that it applies a default-filter to all variables that do not explicitly set one.

Many thanks!

Replies are listed 'Best First'.
Re: Default filter in TT
by mithaldu (Monk) on Jan 01, 2011 at 21:20 UTC
    Edit: Please do not use this and look at Template::AutoFilter for a much better solution instead!
    (You can study this though if you want to see some pretty terrible monkey-patching.)


    After some poking by tinita on the subject and looking at the horror that is Template::HTML i decided to implement a much more brutal, but more effective and way more straight-forward method of applying a user-defined filter to any unfiltered token, while still allowing exclusion of specific tokens.

    As i implemented it just now, you won't find it on CPAN (yet). You can get the code here:

    AutoFilter.pm

    Please let me know if it breaks in some way for you and i'll attempt to get it fixed.
      Many thanks for this, I will test it next week.

      For future feature requests I will try to poke you directly rather than going through tinita :-)

        I hope you meant you are going to use Template-AutoFilter. Also, if you hop on irc.perl.org you can poke me under Mithaldu/xsheep/sheepx most times for fastest fixing. ;)
Re: Default filter in TT
by tinita (Parson) on Jan 01, 2011 at 18:50 UTC
      Thanks for the reply, however I use TT to generate all sorts of stuff (not only html) and would like to know if there is a general solution.

      For me wanting to apply some filter (or even a whole chain of them) to all variables in a template is such a natual requirement that I am surprised that I cannot find anything in the documentation but I would be surprised if it would not exist...

        Then I would look at the source of Template::HTML to see how it is implemented. It doesn't look very complicated at a first glance. seems it is necessary to subclass 4 Template::* classes though.
Re: Default filter in TT
by Your Mother (Archbishop) on Jan 01, 2011 at 19:41 UTC

    There was a thread in the past on the TT2 list about this. I couldn't find it though. Google is increasingly useless (to me) as it apparently only indexes around 200 pages (bing! has 16 listed…) of the TT2 mailing list archive. So, either poke around in there, try searching yourself, you might be smarter than I am today, or maybe sign-up for the list and ask there.

      There was a thread in the past on the TT2 list about this.
      default_escape for Template::Toolkit? maybe?
      edit: oh, the TT mailing list, sorry.
      But as far as I remember whenever this question came up here the answer was complicated, and the last answer I got about this was Template::HTML. I would still be curious if there is an easy solution.

        ++ Good find. That thread points to a relevant month in the mailing list too.

Template::AutoFilter
by mithaldu (Monk) on Jan 02, 2011 at 16:57 UTC
    I just released Template-AutoFilter to CPAN, which means it'll be available for installation in a few hours. It solves the problem cleanly by sub-classing Template::Toolkit with a modified parser that adds filter instructions to unfiltered tokens and supplying a pass-through filter for exclusion.

    I've only done very restricted testing on it, so please do poke me if it breaks things for you, but aside from that it should not be very dangerous to use as long as you remember that it tries to filter ALL TT instructions.

    If you're the impatient type, you can get the code from Github as well. :)
      Thanks for implementing this feature.

      However at the moment I cannot figure out how to use it with CGI::Application::Plugin::TT.

      Is that possible?

Re: Default filter in TT
by Rhandom (Curate) on Jan 26, 2011 at 17:25 UTC
    Template::Alloy now also supports the AUTO_FILTER directive.

    my @a=qw(random brilliant braindead); print $a[rand(@a)];

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://880014]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-19 14:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found