I interpolate a lot of variables into HTML, but I also end up putting a lot of variables into URLs, which require URI-encoding first before HTML-ent'ing. So a general rule, as attractive as it may sound, will probably only get in the way later. (Witness the PHP rule that tries to backwhack all single quotes, and then you end up with occasional backwhacks in the output because of a double application of the rule.)

So, in my mind, I simply keep track of "possibly user data" apart from "data I've constructed", and to the former, add "| html". You do realize that you need only those five characters, right?

[% CGI.h2("Greetings!") %] Hello, [% firstname | html %] [% lastname | html %]!
The "h2" should not be escaped (it would print literally as <h2> which is very wrong), but the firstname and lastname need it, so I add "| html" to them.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.


In reply to Re: Template Toolkit filtering all variables by merlyn
in thread Template Toolkit filtering all variables by marvell

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.