Textpattern CMS Template Tags

<txp:variable /> sing/cont tag, 2 attributes

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, the result of the contained statements are assigned to the given variable name.

<txp:variable>
...contained statements...
</txp:variable>

Related: if_variable

attributes

name="unset" 
  • The variable name for which you wish to assign a value. Valid variable names must not contain any single or double quotes.
value="unset" 
  • (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.

genealogy

Version 4.0.7

  • Added as a new tag.
<txp:variable /> 3 examples

Store sitewide constants

<txp:variable /> allows a site/theme designer to define constants at a central location (a form or the location near the top of a page template) and use them elsewhere later on, for instance as the e-mail address for zem_contact_reborn’s to attribute or as the AdSense Publisher id for all three AdSense forms which are scattered throughout your template.

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:variable name="site-owner" value="john.doe@example.com" />
<txp:variable name="adsense-pub" value="pub-9999999" />
<txp:variable name="client-is-always-right" value="yesss" />

Later down the page template or in a separate form you can read the attribute values previously set:

<txp:zem_contact to='<txp:variable name="site-owner"  />' />

Conditionals come in handy at times:

<txp:if_variable name="client-is-always-right">
<!-- will change in two weeks, I'm sure -->
  <txp:css n="fugly" />
</txp:if_variable>

Elsewhere :

<script type="text/javascript"><!--
google_ad_client = '<txp:variable name="adsense-pub" />';
google_ad_width = 120;
google_ad_height = 240;
//-->
</script>

Other tags used: if_variable, css

Use any tag’s value as a conditional expression

Example:

<!-- step one: store the output of any tag into an arbitrarily named variable -->
<!-- nb: the use of parsed attributes (delimited by single quotes) -->
<txp:variable name="foo" value='<txp:permlink />' />
<!-- step two: instant conditional -->
<txp:if_variable name="foo" value="http://example.com/bar/baz">
  <!-- It matched! -->
</txp:if_variable>

Other tags used: if_variable

Build a table of article titles

Textpattern will build a three-column table where each row has the title of the article. If a number of articles is not divisible by 3 then empty cells will be inserted.

First, use an article_custom or article tag somewhere:

<txp:article_custom limit="100" section="article" form="tables" />

Form tables:

<txp:if_first_article>
<table>
</txp:if_first_article>
<!-- Switch trigger's value -->
<txp:if_variable name="trigger">
   <txp:if_variable name="trigger" value="2">
      <txp:variable name="trigger" value="3" />
   </txp:if_variable>
   <txp:if_variable name="trigger" value="1">
      <txp:variable name="trigger" value="2" />
   </txp:if_variable>
<txp:else />
   <txp:variable name="trigger" value="1" />
</txp:if_variable>
<!-- Building third cell -->
<txp:if_variable name="trigger" value="3">
   <td><txp:title /></td>
   </tr>
   <txp:variable name="trigger" value="1" />
</txp:if_variable>
<!-- Building second cell -->
<txp:if_variable name="trigger" value="2">
   <td><txp:title /></td>
   <!-- Add 1 empty cell -->
   <txp:if_last_article>
      <td></td>
      </tr>
   </txp:if_last_article>
</txp:if_variable>
<!-- Building first cell -->
<txp:if_variable name="trigger" value="1">
   <tr>
   <td><txp:title /></td>
   <!-- Add 2 empty cells -->
   <txp:if_last_article>
      <td></td><td></td>
      </tr>
   </txp:if_last_article>
</txp:if_variable>
<txp:if_last_article>
</table>
</txp:if_last_article>

In this case an article title was used but any article information could be inserted in the cells.

Other tags used: if_variable, if_first_article, if_last_article, title, else