Go to main content


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

add="value" v4.7.2+
Adds value to the current variable value. If both are numeric and separator is not set, the result is their sum. Otherwise, value is appended as a string, optionally separated by separator (see below).
Default: unset.
The variable name for which you wish to assign a value. Valid variable names must not contain any single or double quotes.
output="boolean" v4.7.2+
Force the variable output, e.g. <txp:variable name="name" value="value" output />.
Values: 0 (no) or 1 (yes).
Default: 0.
reset="value" v4.7.2+
A shortcut for <txp:variable name="name" /><txp:variable name="name" value="value" />. Outputs the old value and then assigns the new one.
Default: unset.
separator="string" v4.7.2+
A symbol or a string used to separate values appended via add attribute (see above).
Default: unset.
(Optionally) define the value to which you wish to set the variable. Without this attribute, the tag outputs 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. in form templates, or even at tops 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">
    <txp:else />
        <title>My blog homepage</title>
        <meta name="description" content="The great homepage of my great blog.">
        <meta name="robots" content="index, follow">
        <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="example.com/bar/baz">
    …do this…

The conditional is saying if there is a variable named ‘foo’ having a specific value of ‘example.com/bar/baz’, 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.

Example 4: Output a counter in an article list

<txp:article wraptag="p" break="br">
    Article <txp:variable name="counter" add="1" output />: <txp:title />

Other tags used: article, title.

Example 5: Link to the latest article in a section

<txp:variable name="is_latest"><txp:page_url type="most-recent" /></txp:variable>
<txp:if_variable name="is_latest" value="1">
    <txp:article limit="1" />
    <txp:article />

Using the above code in a page template would permit you to use example.org/articles?latest=1 as a URL and have Textpattern capture the ‘latest’ parameter and take action to display just the most recent article in that section.

Other tags used: article, else, if_variable, page_url.


Version 4.7.2

add, output, reset and separator attributes added.

Version 4.0.7

Tag support added.

If you notice any kind of problem with this page’s construction or content (outdated information, typos, broken links, or whatever), open an issue to have it sorted. Or have a go at it yourself. :)