Textpattern CMS conditional tags: nesting or not

admin-ajax

In my previous article regarding Textpattern CMS conditional tags, I walked through an example code chunk that used a bunch of conditional tags to context-sensitively populate the `<title>` of a web page. The code used nested tags (i.e., tags inside tags) to figure out what was going on and output the appropriate parts of the `<title>`. Here’s the code I used:

<title>
<txp:if_status status="200">
<txp:if_article_list>
<txp:if_author>
Author: <txp:author />
<txp:else />
<txp:if_category>
Category: <txp:category title="1" />
<txp:else />
<txp:if_search>
Search: '<txp:search_term />'
<txp:else />
<txp:if_section name="">
Home
<txp:else />
<txp:section title="1" />
</txp:if_section>
</txp:if_search>
</txp:if_category>
</txp:if_author>
<txp:else />
<txp:title />
</txp:if_article_list>
<txp:else />
<txp:error_status />: <txp:page_url />
</txp:if_status>
&middot; <txp:site_name />
</title>

If you don’t get on with nesting, and it’s not for everyone, then you can strip out most of the `<txp:else />` instances and run all the conditional checks in sequence instead of one monolithic, nested query. The best part about this is you’ll get the same end result on the page. Here’s the revised code, with all-but-one of the `<txp:else />`s chopped:

<title>
<txp:if_status status="200">
<txp:if_article_list>
<txp:if_author>
Author: <txp:author />
</txp:if_author>
<txp:if_category>
Category: <txp:category title="1" />
</txp:if_category>
<txp:if_search>
Search: '<txp:search_term />'
</txp:if_search>
<txp:if_section name="">
Home
<txp:else />
<txp:section title="1" />
</txp:if_section>
</txp:if_article_list>
<txp:if_individual_article>
<txp:title />
</txp:if_individual_article>
</txp:if_status>
&middot; <txp:site_name />
</title>

The remaining `<txp:else />` is there to get around the section article list and homepage naming conundrum. The homepage is an article list, as is a section. The only way to differentiate them is to query the section for a name. The homepage has no name, but has a title of `default`; section article lists have unique names and titles. There’s significantly less nesting involved with this revised code, and most of the queries now happen sequentially in series.

What’s interesting is the difference in behind-the-scenes resource use when these two blocks of code are used. Broadly the same effect in the browser, but Textpattern does more work to achieve the outcome of one of them. Textpattern can output the number of queries, runtime and approximate memory used to render something in your browser. To achieve this, set your production status to `Testing` in your preferences then refresh the page you wish to check, and view the source code. There will be comments in the page source with the statistics.

On a shared, Personal Class Arvixe server I tried both code blocks. In each case, I viewed the same article and reset my browser before each test. First, the results of the nested code:

<!-- Runtime: 0.0521 -->
<!-- Query time: 0.019415 -->
<!-- Queries: 23 -->
<!-- Memory: 4705Kb, end of textpattern() -->

…and the second code block with far less nesting:

<!-- Runtime: 0.0240 -->
<!-- Query time: 0.009932 -->
<!-- Queries: 23 -->
<!-- Memory: 4706Kb, end of textpattern() -->

I ran each test three times and the numbers varied slightly, but within a margin of error. What’s notable in this test is that the nested code (the first block) was processed much more slowly when compared to the second block. The number of queries is the same, and the memory use is about the same, too. Shorter runtime and a smaller query time make less work for MySQL, and if you’re scaling a site up to have hundreds, thousands or millions of visitors then every bit of performance counts.

Looking for quality Textpattern Hosting? Look no further than Arvixe Web Hosting and use coupon TEXTPATTERN for 20% off your first invoice!

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

Author Spotlight

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 *