setvar
Takes the content from the tag it is applied to and assigns it to the given variable name.
Example, assigning a HTML link of the last published entry with a '@featured' tag to a template variable named 'featured_entry_link':
<mt:Entries lastn="1" tags="@featured" setvar="featured_entry_link">
<a href="<$mt:EntryPermalink$>"><$mt:EntryTitle$></a>
</mt:Entries>
The output from the Entries tag is suppressed, and placed into the template variable 'featured_entry_link' instead. To retrieve it, just use the Var tag.
Jay Allen
June 21, 2008 6:07 PM | Reply
One additional way in which this is extraordinarily useful is in modifying a template variable in situ.
Before the
setvarattribute, you used to have to resort to outputting the saved variable usingmt:varin order to modify it via template tag modfiers/filters only to save it back to the same variable usingmt:SetVarBlock:Now however, you can do the following:
That last line applies the
strip_linefeedsfilter to the value stored in the myvar template variable and then immediately stores it back into the variable with no output created by themt:vartag.Beau Smith
June 21, 2008 7:58 PM | Reply
+1 - Awesome tip Jay
Gautam Patel
September 6, 2008 6:27 AM | Reply
This is brilliant, and definitely not to be confused with the now deprecated SetVar template tag.
An alternative is this:
This will turn the value of mylabel into uppercase.
MG
January 30, 2010 11:48 AM | Reply
It's important that you use setvar last in the attributes.
Everyone above does so, but I still didn't pick up on that at first. :-P