Textpattern tips, tutorials and code snippets

Use of new tag if_variable to replace chh_if_data

As a follow up to our previous TXP Tip on Custom content if data exists, the latest version of Textpattern – 4.07 – allows us to replace the plugin with native functionality.

The original code

Lets take a look at some of the original code from the article:

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

Introducing if_variable

With the new if_variable tag introduced in 4.07, we no longer need chh_if_data. Instead, we can use this code, placed early in your page (before you need to display the results):

<txp:variable name="if_announcements" value='<txp:article_custom section="news" form="news_announcement" category="Announcements" limit="1" />' />

Displaying the results

Further down the page, lets take a look at the results:

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

Your usage may vary, but the method should be clear.

2 Comments Comment feed

For better understanding I would reduce the code to the skeleton functions or add simple comments in the code:

  1. set variable
    • In this case calling the output of a TXP tag by using single quotes
  2. use variable in an empty/filled situation
    • value="" = empty

In this example also the usage of name="if_anouncements" is disorienting.

Instead, we can use this code, placed early in your page (before you need to display the results):

Don’t use proverbs!

Instead, with Textpattern >4.0.7 it is possible to set a variable first with <txp:variable name="variable" value="value" />. After setting the variable a second step using a <txp:if_variable …> container makes it possible to create different output like in every other TXP <txp:if_…> condition.

IMHO… the if_variable tag is more different to the plugin and requires an additional step to work. I hope that in the next release – 4.0.9 – the team add a new tag if_data to replace the useful plugin :)

Add a comment

Use Textile help to style your comments