Textpattern tips, tutorials and code snippets

Custom content if data exists

One of Textpattern’s hidden gems is a plugin called chh_if_data. This plugin outputs conditional content if data exists within a TXP tag.

This TXP Tip provides a simple example of how to display content based on the results of a conditional statement.

  1. Download and install chh_if_data

Enter some code in your page template

<txp:if_section name="news">
<txp:if_individual_article>
<txp:article form="news_single" limit="1" />
<txp:else />
<div id="newsbox">
<h2>Site Announcements</h2>
<txp:chh_if_data>
<txp:article_custom section="news" form="news_announcement" category="Announcements" limit="1" />
<txp:else />
<p>There are no current announcements.</p>
</txp:chh_if_data>
</div>
<h2>News</h2>
<txp:article form="news_list" limit="10" />
<txp:ob1_pagination firsttext="First page" previoustext="Previous page" nexttext="Next page" lasttext="Last page" ulclass="pagination" />
</txp:if_individual_article>
</txp:if_section>

Most of the example code is self-explanatory. This is the important part:

<txp:chh_if_data>
<txp:article_custom section="news" form="news_announcement" category="Announcements" limit="1" />
<txp:else />
<p>There are no current announcements.</p>
</txp:chh_if_data>

The chh_if_data plugin here checks if there is any data if we are in an article list in the news section. If there is data, show one article from the Announcements article category.

If there is no data, the plugin displays a short message “There are no current announcements”.

For an example, visit the Snoooz Tennis news page.

5 Comments Comment feed

In Textpattern 4.0.7, this can be handled without a plugin using the txp:if_variable tag in tandem with setting a variable for the content you’re checking. I’m willing to post an example somewhere if needed.

Hi John,

I was going to hold off from 4.07 examples until it is released next week. But – if you have an example ready to go, please send the tip over using the submit form. Thanks!

Brilliant plugin. Great tip.

@John Stephens : Please elaborate, I’ve been willing to see some of this stuff for a while now.

@all – this new tip elaborates on the use of the new if_variable tag. The tip explains how to replace the use of chh_if_data (like in this tip) with if_variable.

Add a comment

Use Textile help to style your comments