setvar
Takes the content from the tag it is applied to and assigns it to the given variable name.
Note: this modifier must be the last attribute specified. Modifiers are cumulative and processed in order.
Examples
Assign a String from a Block Tag
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.
Multiple Assignment
One additional way in which this is extraordinarily useful is in modifying a template variable in situ.
Before the setvar
attribute, you used to have to resort to outputting the saved variable using the Var
template tag in order to modify it via template tag modfiers/filters only to save it back to the same variable using the SetVarBlock
template tag:
<mt:SetVarBlock name="myvar">
1
2
3
4
</mt:SetVarBlock>
<mt:SetVarBlock name="myvar"><mt:Var name="myvar" strip_linefeeds="1"></mt:SetVarBlock>
Now however, you can do the following:
<mt:SetVarBlock name="myvar">
1
2
3
4
</mt:SetVarBlock>
<mt:Var name="myvar" strip_linefeeds="1" setvar="myvar">
That last line applies the strip_linefeeds
modifier to the value stored in the myvar
template variable and then immediately stores it back into the variable with no output created by the Var
tag.