On this page:
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:
<txp:variable> ...contained statements... </txp:variable>
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
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:
<txp:if_search> <title>My blog search results: <txp:search_term /></title> <meta name="description" content="Blog article search results."> <meta name="robots" content="none"> <txp:else /> <txp:if_category> <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" /> </txp:if_category> </txp:if_search>
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... </txp:if_variable>
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
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... </txp:if_variable>
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.
Tag support added.