Introducing Textpattern Tags

Textpattern CMS has an in-built tag system which can be used to output things from the Textpattern database. The default front-end theme page includes some tags in its markup that are great examples of these. I’ll use a snippet of text from the HTML body to demonstrate:

<!-- header -->
  <header role="banner">
    <hgroup>
      <h1><txp:link_to_home><txp:site_name /></txp:link_to_home></h1>
      <h3><txp:site_slogan /></h3>
    </hgroup>
  </header>

If you’re familiar with HTML5, you should already recognise most of the code above. The Textpattern tags stand out as they all include the string ‘txp’. Looking at the text above, we can pare it down to just the Textpattern tags:

<txp:link_to_home><txp:site_name /></txp:link_to_home>
<txp:site_slogan />

I can tell you there are two single tags in that block, and there’s 1 container tag. Let’s start with the single tags:

<txp:site_name />
<txp:site_slogan />

When you use either of these tags in Textpattern pages, articles or forms, they will output either the site name or site slogan. Both the site name and the site slogan are set in the Administration screen, in the Admin tab, then the Preferences sub-tab. Using these tags can be a real help if you, for example, want to change your site slogan across all the pages at one; rather than manually editing each reference, the tag just checks for the current slogan. The same goes for the site name, same process, same approach.

Here’s a test case: you are Jane Doe, managing director of Example, Inc in Chicago. Your site name is set in your preferences as:

Example, Inc

…and your slogan is set to:

 Chicago's finest tool manufacturer

Both of these pieces of information are in your page header, your article footers, peppered around your site; but rather than inserting Example, Inc or Chicago’s finest tool manufacturer into each page/article, you used a <txp:site_name /> and <txp:site_slogan />. Each view of the page or article will have Textpattern check for the site name and site slogan, outputting it from the preferences accordingly.

Then, your company is acquired by a mysterious benefactor.  The benefactor wants to change your company name to:

Example, Corporation (Chicago)

…and your slogan changes to:

Chicago's finest tool manufacturer, established 1950

There may be hundreds of occurrences of your name and slogan on your site. Simply changing them in Preferences will update all the pages where <txp:site_name /> and <txp:site_slogan /> are used. It’s an instant change: search engines and visitors will find the new references when pages are crawled/viewed.

A Textpattern single tag follows this format:

<txp:example />

Note the trailing slash which self-closes it. An example in HTML might be:

<meta name="generator" content="Example CMS" />

A container tag works in a different way. Let’s refer to the code from earlier on and strip out just the container tag:

<txp:link_to_home></txp:link_to_home>

This tag constructs an HTML-valid hyperlink to the URL specified in the Preferences sub-tab. What makes a container tag different to a single tag is the format: there is code to open it, and code to close it – and crucially, there can be something between the two. In the original code, there was this:

<txp:link_to_home><txp:site_name /></txp:link_to_home>

The container tag provides the hyperlink, and the single tag in the middle provides the anchor to which the hyperlink is assigned. Going back to our Chicago example, using this code:

<txp:link_to_home><txp:site_name /></txp:link_to_home>

…would end up outputting something like this:

<a href="http://example.com">Example, Corporation (Chicago)</a>

In each case, the tag substitutes values from the database and builds valid HTML for your site. For completeness, our original code was:

<!-- header -->
  <header role="banner">
    <hgroup>
      <h1><txp:link_to_home><txp:site_name /></txp:link_to_home></h1>
      <h3><txp:site_slogan /></h3>
    </hgroup>
  </header>

…and that might render as:

<!-- header -->
  <header role="banner">
    <hgroup>
      <h1><a href="http://example.com">Example, Corporation (Chicago)</a></h1>
      <h3>Chicago's finest tool manufacturer, established 1950</h3>
    </hgroup>
  </header>

What the browser outputs depends on what Textpattern has stored, and the key point is that it can be changed whenever you want or need to.

This an overview of tags in Textpattern, I’ll cover it in more detail in future posts. You can read more on all the Textpattern tags if the spirit moves you. Be warned, if you’re so inclined, you can spend many hours poking around and finding lots of amazing things.

Thank you for reading my introduction to Textpattern tags, I hope you found it useful. In my next post I’ll be showing you how to install and use a Textpattern plugin. I hope you’ll join me.

Looking for quality Textpattern Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , | Posted under Textpattern | RSS 2.0

Author Spotlight

Pete Cooper

Pete Cooper

Pete Cooper has been using Textpattern since 2005. Textpattern is his preferred CMS weapon of choice. Its logical and flexible approach to content management makes Pete happy, as does its lightweight core and helpful user community. Pete's website - petecooper.org - runs on top of Textpattern and chronicles his day-to-day experiences from his home near the Atlantic in north Cornwall, United Kingdom.

Leave a Reply

Your email address will not be published. Required fields are marked *


+ 5 = 14

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>