Using Regular Expressions with Variables in Movable Type

| 11 Comments | No TrackBacks |
One of the interesting features of Movable Type's templating language is that you can use the value of variables as the value for attributes of another tag.  The only thing you need to keep in mind is to use the $-sign notation in that case instead of the <mt:var> tag.  So don't use <mt:entries category="<mt:var name="mycategory">"> but use <mt:entries category="$mycategory">.
When using the $-sign notation, one other important thing to keep in mind is that you cannot have anything beside the variable name as the attribute value.  So something like <mt:entries categories="$mycategory, Default, Sports"> is not possible.

Or is it?

What if you do this:

<mt:setvarblock name="morecategories"><mt:var name="mycategory">, Default, Sports</mt:setvarblock>
<mt:entries categories="$morecategories">

That actually works.  And you can even apply this in combination with the regex_replace attribute:

<mt:entrybody regex_replace="/the\scategories\sof\sthis\sentry/gsi","$morecategories">

This will replace all instances of the phrase 'the categories of this entry" with the actual names of the categories (assuming they are in the $morecategories variable of course).

It even works the other way round: you could put the entire definition of the regular expression in a variable as well, and this allows you to put the contents of other variables or the output of template tags into the expression:

<mt:setvarblock name="expression">/<mt:entrycategory>/gsi</mt:setvarblock>
<mt:entrybody regex_replace="$expression","the category of this entry">

(Note: don't forget to make the slashes part of the variable content)

This should replace all instances of the name of the category of the entry with the string 'the category of this entry' (assuming the category name doesn't contain characters that break the regular expression syntax of course).

Other entries you might like

No TrackBacks

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

11 Comments

Hey~! Thanks for the great tip. The documentation for regular expressions is quite poor in MT's site. Could you explain why the regex replace variable has to start with a "/" and end with a "/gsi" ?

I relish, lead to I found exactly what I was having a look for.
You have ended my four day long hunt! God Bless you man. Have a nice day.

Bye

My brother suggested I might like this web site.
He was entirely right. This post truly made my day.
You cann't imagine simply how much time I had spent for this information! Thanks!

I like the helpful info you provide in your articles. I'll bookmark your blog and check again here frequently. I am quite sure I will learn lots of new stuff right here! Good luck for the next!

Excellent post. I was checking constantly this blog and I'm impressed! Very useful information specially the last part :) I care for such information a lot. I was looking for this certain information for a long time. Thank you and best of luck.

You actually make it seem so easy with your presentation but I find this matter to be actually something that I think
I would never understand. It seems too complicated and extremely broad for me.
I am looking forward for your next post, I'll try to get the hang of it!

Once you turns on per game cheap basketball jerseys the tonneau's cheap jersey http://www.mindcolors.com//custom-nfl-jerseys-cheap-1768505.html s right on top of http://www.acemarathon.org/ http://www.mytotallyunglamorouslife.com/wp-admin/cheap-jerseys-2584036.html cheap-jerseys/ on my feet cheap nfl jerseys . assuming you have http://cheloniapottery.com strong cheapest nfl jerseys from china writing cheap jerseys tips http://www.no http://www.woollyowl.com/about_us/replica-louis-vuitton-2648717.html r

Great blog here! Also your web site loads up fast! What web
host are you using? Can I get your affiliate link to your
host? I wish my web site loaded up as fast as yours lol

Leave a comment


Movable Type Help?

"Can my Movable Type issue or project be taken care of by a professional?" Yes It Can Be! Contact YesItCan.be
Powered by Movable Type 5.2.7