On 8/5/09 09:08, kakim...@tpg.com.au wrote:
> using "| uri"
> ===========
>    - passes W3C validation

You should only use the uri filter for encoding query values in the url.  e.g.

   [% term = 'Ben & Jerry' %]
   <a href="http://foo.com/search?term=[% term | uri %]">...</a>

This generates:

   <a href="http://foo.com/search?term=Ben%20%26%20Jerry";>...</a>

This stops the '&' in the search term from being confused with the URL
parameter separator.

But the URI filter is too aggressive for encoding complete URLs.  The only
time you would want to do that is if you're using a URL as a query parameter.
Consider this rather contrived example:

   [% term = 'http://www.google.co.uk/search?q=uri+encoding&ie=utf-8' %]
   <a href="http://foo.com/search?term=[% term | uri %]">...</a>

Which produces:

   <a 
href="http://foo.com/search?term=http%3A%2F%2Fwww.google.co.uk%2Fsearch%3Fq%3Duri%2Bencoding%26ie%3Dutf-8";>...</a>

> using "| url"
> ===========

Yes, the url filter is for encoding complete URLs.  It's a less
aggressive form of the uri filter that attempts to Do The Right Thing.
But be warned that there's no such thing as URL encoding in formal terms
so the Right Thing is somewhat arbitrary.

>    - fails W3C validation with the error below. W3C validator just doesn't
 > like the special characters here.

Yes, that's correct.  A valid URL is not necessarily valid HTML.  You must
pass it through the html filter to escape any '&' characters as '&amp;'

   [% my_url | html %]

To clarify, this URL:

   http://foo.com/search?term=Ben%20%26%20Jerry&page=20

Must be embedded in HTML as:

   <a href="http://foo.com/search?term=Ben%20%26%20Jerry&amp;page=20";>...</a>
                                                        ^^^^^

The most reliable way to generate URLs is to use the URL plugin.  It takes
care of all this mess for you:

   [% USE search = URL('http://foo.com/search') %]
   <a href="[% search_link(term="Ben & Jerry", page=20) %]">GO</a>

This generates:

   <a href="http://foo.com/search?page=20&amp;term=Ben%20%26%20Jerry";>GO</a>

Which is the correct HTML encoded representation of a URL with URI encoded
query terms.  Phew!

HTH
A


_______________________________________________
templates mailing list
templates@template-toolkit.org
http://mail.template-toolkit.org/mailman/listinfo/templates

Reply via email to