Keeping Long URLs Readable in Movable Type Templates

| No Comments | No TrackBacks |
I've been working on a few projects recently which required numbered pagination at the bottom of the page. This is no problem for the most recent versions of Movable Type, even with static publishing.  How to do it is documented quite well here.  In the examples in this documentation I found a neat trick unrelated to pagination I want to share.
More precisely, have a look at this code snippet:

<mt:Ignore><!-- Construct the url for querying entries. --></mt:Ignore>
<mt:SetVarBlock name="search_link">
    <$mt:CGIPath$><$mt:SearchScript$>?IncludeBlogs=<$mt:BlogID$>
    &template_id=<$mt:BuildTemplateID$>
    &limit=<$mt:Var name="entries_per_page"$>
    &archive_type=Index
    &page=
</mt:SetVarBlock>
<mt:Ignore><!-- Strip spaces and trim value. --></mt:Ignore>
<$mt:Var name="search_link" strip="" trim="1" setvar="search_link"$>

Basically what this does is create an URL pointing to Movable Type's search script, with a tail of parameters hanging off it.  The values of these parameters depend on the output of various MT tags.

You could do all this in one long line of template code, but the above example is much more readable.  Each important part of the URL is on its own line, making them much easier to find if something needs changing.

The smart trick about this is in the last line: the 'strip' attribute replaces all whitespace with its own value, which in this case means deleting all of it, 'trim' removes any leading or trailing whitespace (a bit of overkill I think, after 'strip') and the 'setvar' attribute puts the output of the tag in a variable instead of displaying it, in this case back into the search_link variable.

The end result is a variable (search_link) containing the desired URL, properly formatted on one line and ready for use.

No TrackBacks

TrackBack URL: https://www.movabletips.com/cgi-bin/mt/mt-tb.cgi/88

Leave a comment