[ZPT] URL quoting in ZPT

Jeffrey P Shell jeffrey@cuemedia.com
Thu, 29 Aug 2002 15:28:45 -0600


On 8/29/02 3:12 PM, "Shane Hathaway" <shane@zope.com> wrote:

> I just ran into a puzzling little issue.  Let's say I want to construct
> an URL with a query string like this in ZPT:
> 
> <a tal:attributes="href string:${here/absolute_url}?id=${myid}">Go</a>
> 
> The problem is that while "here/absolute_url" gets URL quoted as it
> should, "myid" does not.  If the value of myid contains a space or some
> other disallowed character, some browsers (like MSIE) will break.
> 
> So there must be some simple way to get a string url_quoted, but I can't
> think of what it would be.  Any suggestions?

I always use ZTUtils.make_query these days.  It HTML quotes, it does Zope
marshalling (:int, etc), and is easier to expand.  Usually I define a short
variable to ZTUtils in an outer block or globally (tal:define="ztu
modules/ZTUtils"), and then:

<a tal:define="query python:ztu.make_query(id=myid)"
   tal:attributes="href string:${here/absolute_url}?$query">Go</a>

It's a bit more typing, but it's more flexible, especially when a query
needs to be expanded.

In the Products.PythonScripts.standard module are the 'url_quote' and
'url_quote_plus' formatting functions that DTML uses.

-- 
Jeffrey P Shell 
www.cuemedia.com