Not a developer? Go to MovableType.com

Documentation

SetVarBlock

A block tag used to set the value of a template variable. Note that you can also use the global ‘setvar’ modifier to achieve the same result as it can be applied to any MT tag.

Attributes:

  • var or name (required)

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

  • 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:

Back

5 Comments

Gautam Patel

Gautam Patel on December 14, 2007, 5:18 p.m. Reply

Note that the <SetVarBlock> preserves linefeeds. Using the block function as shown in the example above will put linefeeds into ‘foo’, so that you get LF(variable value)LF.

If your variable result is, say, the number 9, and you use the syntax in the example above, you will get:

LF
9
LF

In a certain situation, if you compare the generated value (using <mt:if> or <mt:unless>), the result will be incorrect.

To generate a variable value without linefeeds, use the following, all in one line:

<mt:setvarblock name="foo"><mt:foldercount></mt:setvarblock>

To strip linefeeds, use the strip_linefeeds modifier, like so:

<mt:setvarblock name="foo" strip_linefeeds="1">
    <mt:VariablesHereWhichCanNotBeSetWithMTVar>
</mt:setvarblock>

Thanks to Chris Hall for the heads up on this!

Elise Bauer

Elise Bauer on February 11, 2008, 6:14 p.m. Reply

Gautam, what’s a linefeed?

Adagio on May 3, 2009, 8:02 p.m. Reply

Hi Elise - Thanks for the great “Learning MT Blog”!

A linefeed puts a new line character in a line like a just did after the word “line” above.

-Arif Diwan

Gautam Patel

Gautam Patel on July 25, 2009, 10:49 p.m. Reply

This can be used for a possibly convenient way to compare dates, if you have MT Pro and CustomFields. You can use this to automatically expire a post on a specified date.

Here’s what you do:

Create a custom date field, and set it to show “date only”. Let’s call it “ExpiryDate”, so the corresponding MT tag is <MT:ExpiryDate>

Now, in your template, the thing to do is to compare the entry’s publish date with the expiry date. If the two are not equal, show the entry. If the two are equal — i.e., the publish date has ‘hit’ your chosen expiry date — don’t show the entry.

The code would go like this:


    <mt:entries>
    <mt:setvarblock name="pubdate"><mt:entrydate format="%Y%m%d" strip_linefeeds="1"$></mt:setvarblock>  
    <mt:setvarblock name="expdate"><mt:expirydate format="%Y%m%d" strip_linefeeds="1"$></mt:setvarblock>  
    <mt:if name="pubdate" ne="$expdate">  
    <h1><$MTEntryTitle$></h1>  
    <div><$MTEntryBody$></div>  
    </mt:if>  
    </mt:entries>  

Note that both variables must be defined in <setvarblock> tag pairs; and the <strip_linefeeds=”1”> is also required, because typically, <setvarblock> inserts a linefeed.

Beau Smith

Beau Smith on October 27, 2009, 5:36 p.m. Reply

Display the pages in the same folder as the current page, if the page is tagged “@ShowRelatedPages”.

Append each new page link if the page is not the current page.

<mt:PageIfTagged tag="@ShowRelatedPages">
    <$mt:PageID setvar="CurrentPageID"$>
    <mt:PageFolder>
        <$mt:FolderLabel setvar="pageFolder"$>
    </mt:PageFolder>
    <mt:Pages folder="$pageFolder">
        <mt:Unless tag="PageID" eq="$CurrentPageID">
            <mt:SetVarBlock name="relatedPages" append="1">
                <li><a href="<$mt:PagePermalink$>"><$mt:PageTitle$></a></li>
            </mt:SetVarBlock>
        </mt:Unless>
    </mt:Pages>
    <mt:If name="relatedPages">
<div class="related-pages">
    <h2>Related Pages</h2>
    <ul>
        <$mt:Var name="relatedPages"$>
    </ul>
</div>
    </mt:If>
</mt:PageIfTagged>