On this page:


<txp:variable />

The variable tag is both a single and a container tag which sets or returns a user-defined global variable.

If used as a container tag, the result of the contained statements are assigned to the given variable name, like this:

    …contained statements…

Note: Avoid entering white space characters for better code readability between the opening and closing variable tags, they will lead to falsified results in the if_variable evaluation.


Tag will accept the following attributes (case-sensitive):

The variable name for which you wish to assign a value. Valid variable names must not contain any single or double quotes.
(Optionally) define the value to which you wish to set the variable. Without this attribute, the tag returns the current value assigned to the named variable.


Example 1: Store site-wide constants

Allows you to define constants at a single location (e.g. Form templates, or even at the top of Page templates) and use them elsewhere later on.

Somewhere at the very beginning of a template you would define names and values, just like you do on your desktop calculator’s ‘memory’ keys:

    <title>My blog search results: <txp:search_term /></title>
    <meta name="description" content="Blog article search results.">
    <meta name="robots" content="none">
<txp:else />
        <title>Blog category: <txp:category title="1" /></title>
        <meta name="description" content="Blog article ‘<txp:category title="1" />’ category archive.">
        <meta name="robots" content="noindex, follow, noodp, noydir">
    <txp:else />
        <title>My blog homepage</title>
        <meta name="description" content="The great homepage of my great blog.">
        <meta name="robots" content="index, follow, noodp, noydir">
        <txp:variable name="homepage" value="1" />

Later down the Page template or in a separate Form template you can read the attribute values previously set conditionals come in handy at times:

<txp:if_variable name="homepage" value="1">
    …homepage content…

Other tags used: else, if_category, if_search, if_variable, search_term.

Example 2: Use any tag’s value as a conditional expression

There are two parts to making this work. First a variable is created that stores the output of any tag as the value (the name is arbitrary)…

<txp:variable name="foo" value='<txp:permlink />' />

Note: A Textpattern tag, used as an attribute (a parsed attribute), must be surrounded with single quotes.

The variable ‘foo’ can then be used as a conditional later in the code.

<txp:if_variable name="foo" value="">
    …do this…

The conditional is saying if there is a variable named ‘foo’ having a specific value of ‘’, then output what is defined, i.e. ‘do this’.

Other tags used: if_variable.

Example 3: Check if this is the first page of a page group

<txp:variable name="page" value='<txp:page_url type="pg" />' />
<txp:if_variable name="page" value="1">
    This is the first page.
<txp:else />
    This is page number <txp:variable name="page" />.

Other tags used: else, if_variable, page_url.


Version 4.0.7

Tag support added.

See something wrong in this document? Outdated info, a broken link, faulty code example, or whatever? Please write an issue and we’ll fix it.