Links to alternative archives: regex_replace to the rescue!

| No Comments | No TrackBacks |
So you have your boring old monthly, category and individual entry archives, neatly published in the default locations with the default path names.  Nothing is easier than generating links to these archives, using the standard <mt:archivelink> tag.  But what if you are also publishing a version of your archives that is optimized for mobile browsing, published in the same location but with a slightly different filename?
Let's say your normal monthly archives have links that look like this:


Generating a bunch of links to these is easy:

<mt:archivelist archive_type="Monthly">

Suppose your 'mobile' archives are published under:


How do you link to these?  There is no built-in tag to do this quickly and easily.  But the regex_replace modifier comes to the rescue:

<mt:archivelist archive_type="Monthly">
<li><mt:archivelink regex_replace="/(^.+\d+\/\d+\/)$/","\1index-mobile.html">

In my experience, regex_replace is one of the most powerful modifiers Movable Type has to offer.  Some quick tips:

  • Don't forget the two slashes inside the first pair of quotes that delineate the regular expression: regex_replace="//",""
  • If you capture a part of the regular expression using brackets, you can refer to the matched part in the second expression by using the \1, \2, \3... notation: regex_replace="/(foo)(bar)/","\1-bli-\2"
  • You can use the standard regular expression modifiers after the second slash.  For example regex_replace="//i","" will match case insensitive, regex_replace="//g","" will do a global replace (i.e. if there are more matches they will all be replaced) and regex_replace="//s","" will treat the string to be matched against as a single line, ignoring line breaks etc.
  • In the second part of the expression you can use variables that you defined earlier in the template: <mt:setvar name="foo" value="bla">... <mt:sometag regex_replace="/pattern/","$foo">

No TrackBacks

TrackBack URL:

Leave a comment