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.

This page was last updated on 2008-05-21, 18:10.

4 Notes

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 mt:var in order to modify it via template tag modfiers/filters only to save it back to the same variable using mt:SetVarBlock:

Now however, you can do the following:

    1
    2
    3
    4

That last line applies the strip_linefeeds filter to the value stored in the myvar template variable and then immediately stores it back into the variable with no output created by the mt:var tag.

+1 - Awesome tip Jay

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.

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

Leave a note

Have a question? Please use the MT Forums. Notes submitted on documentation should pertain to tips & hints regarding documentation. Your note may be removed once its contents have been integrated into the body of the page.