<$mt:SetVar$>

A function tag used to set the value of a template variable. 

For simply setting variables you can use the Var tag with a value attribute to assign template variables.

Attributes:

  • var or name

    Identifies the name of the template variable. See Var for more information on the format of this attribute.

  • value

    The value to assign to the variable.

  • op (optional)

    See the Var tag for more information about this attribute.

  • prepend (optional)

    If specified, places the contents at the front of any existing value for the template variable.

  • append (optional)

    If specified, places the contents at the end of any existing value for the template variable.


Related Tags:


This page was last updated on 2009-04-24, 17:58. [Edit]

5 Notes

Caution! It appears these statements at the beginning of templates outputs blank lines before the Header template. If using any php in the Header before the Doctype, you either need to move the MTInclude Header before the SetVar or put the php code in a MTInclude (or the code itself) before any of the SetVar statements.

MT SetVar (SetVarBlock or SetVarTemplate) should not be creating blank lines in the generated code.

Merv, I've seen similar behavior with SSI statements. I bet it's not the variable statements that output the line breaks; it's the fact that the variable statments are each on their own line.

In other words, the template contains line breaks between each MT variable statement; after the MT tags are used, the line breaks remain!

So run all your variable tags together. It's harder to read and maintain, but removes your excess line breaks:

<mt:SetVar name="foo1" value="bar" /><mt:SetVar name="foo2" value="bar" /><mt:SetVar name="foo3" value="bar" />

A quick note for anyone reading the comments above: This issue was fixed in MT 4.2 for all XML-based templates (i.e. the only ones for which it matters.). At this point, it's merely aesthetic.

If however, you are OCD about your HTML source, you can always wrap your entire template in an mt:Section block and strip the whitespace with a regex.

Example

<mt:Section regex_replace="/^\s+/g","">

    [[The contents of your entire main index template]]

</mt:Section>

One note about using SetVar - though the details above would lead you to believe that if you use the value attribute you can replace it with Var. You would not always be correct.

The problem appears to stem from the use of the op attribute.

Say for instance you use something like op="add" value="1" - you would think since you used value in there, you could use Var. Not. Stick with SetVar.

If you only use value, it's generally safe to use Var. But just make sure you check the output.

Chad, are you absolutely sure that that is true as of v4.23 and above? I never use SetVar, always Var and I know that I've used the op attribute a number of times before although I will say I usually do it this way:

 <mt:var name="whatevers" op="++" setvar="whatevers">

And yes, having to use setvar when I've specified ++ annoys me given that in perl (and others?) ++ specifically means increment the internal value.

Regardless, if Var doesn't play nice with op and value, that's a bug and should be fixed immediately if it has not yet been, and SetVar should be re-deprecated.

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.