Log in |
Generate URLs for Objects(Note that the impatient can jump to the end of the how-to to get the answer without reading through the rational. :) It's common to need to provide URLs for objects. There are a number of ways that this can be done. You can just give a relative URL using the object name, as in: <a href="spam"> This works even if the object spam is not in the current folder but is acquired. A significant disadvantage of this approach is that, if the object is acquired, it can drive persistent spiders insane because it sets up situations where a program following links generates an unlimited sequence of strange but valid URLs. To see how this can happen, imagine that the above reference appears
in the
Another problem with relative URLs is that they defeat image caching.
To see why, imagine that you have an image in your root folder named
<img src="logo"> but this will make It's better to use absolute references. An easy way to build an absolute reference is to prepend the request variable URL1, as in: <a href="&dtml-URL1;/spam"> But this suffers from the same problem as a relative URL, since it's really still a relative reference. (Note that we use the entity reference form of the var tag here because we're sticklers for writing, uh, well-formed HTML. We could, instead, have used: <a href="<dtml-var spam>"> but this looks too weird and might upset an HTML editior.) To generate a true absolute reference to an object, you can call
it's <dtml-with "_(abs_spam=spam.absolute_url())"> <a href="&dtml-abs_spam;"> </dtml-with> This is good because we get the same URL for This version is a bit long-winded. We had to resort to an expression
to call Fortunately, the <a href="&dtml.url-spam;"> So, in summary, when you want to generate a URL
for an object, use the entity-reference version
of the var tag with the |