Using Variables in Textpattern

On one of our client sites we need to display different form types for a few sections but otherwise the page is the same.

So in order to achieve this I’ve been using the *<txp:variable />* tag.

First up I set some defaults using a simple variable/value pair:

* <txp:variable name=”sortthis” value=”custom_8″ />
* <txp:variable name=”showtime” value=”past” />
* <txp:variable name=”listform” value=”v3_article_list” />
* <txp:variable name=”listlimit” value=”10″ />
* <txp:variable name=”displayform” value=”article_display” />

Next I display our standard form that we want to see on the main sections of the site:

<txp:article form='<txp:variable name=”displayform” />’ limit='<txp:variable name=”listlimit” />’ listform='<txp:variable name=”listform” />’ pgonly=”0″ searchall=”0″ searchsticky=”0″ sort='<txp:variable name=”sortthis” />’ status=”4″ time='<txp:variable name=”showtime” />’ />

This will now work quite happily. In order to show the different formats for the ‘other’ sections we now add this between the default variables and the <txp:article /> tag overriding any variables required:

* <txp:if_section name=”news-and-articles”>
* <txp:variable name=”sortthis” value=”Posted desc” />
* <txp:variable name=”showtime” value=”past” />
* </txp:if_section>
* <txp:if_section name=” events”>
* <txp:variable name=”sortthis” value=”Posted” />
* <txp:variable name=”showtime” value=”future” />
* </txp:if_section>
* <txp:if_section name=” our-clients”>
* <txp:variable name=”listform” value=”v3_client_list” />
* <txp:variable name=”listlimit” value=”100″ />
* </txp:if_section>

The real magic here is the use of single quotes and double quotes. Basically the stuff inside the single quotes gets evaluated first meaning you can fire variables into other TXP tags.

It’s especially useful for creating custom article lists based upon current section or current category:

* <txp:article_custom section='<txp:section />’ />

Enjoy!

Leave a Reply

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