04 – Conditional content using Kazoo
Why have conditional content?
You don’t have to browse WordPress related forums very long to find someone asking about how to make some content on their site conditional. They have content they want to show for some but not for others and they’re searching for a plugin or PHP code to make that happen. Maybe they want to display links or other information ONLY to members of their site or that they want to display advertising ONLY to people who are not logged in. Whatever the reason Kazoo can help with this sort of thing and you probably won’t need to code any PHP.
Kazoo does conditional content.
Kazoo is based on the KudzuPHP template engine which supports conditional content right out of the box and so Kazoo supports it too. There are four powerful built in template tags to handle conditional content:
<!--{IfTrue|val_name}--> ... <!--{/IfTrue}-->
<!--{IfFalse|val_name}--> ... <!--{/IfFalse}-->
<!--{If|val_name}-->
<!--{Then}--> ... <!--{/Then}-->
<!--{Else}--> ... <!--{/Else}-->
<!--{/If}-->
<!--{Case|val_name}-->
<!--{match_value_a}--> ... <!--{/match_value_a}-->
<!--{match_value_b}--> ... <!--{/match_value_b}-->
<!--{match_value_c}--> ... <!--{/match_value_c}-->
<!--{Else}--> ... <!--{/Else}-->
<!--{/Case}-->
Using the IfTrue and IfFalse tags:
These two tags include content on the condition that a value in the template engine evaluates to either true or false respectively. It’s pretty simple really – you add a tag and specify a value from the template engine. The content is either included or it isn’t.
If..Then..Else using the IF tag:
The If tag operates a little differently. Although If also accepts the name of a value to evaluate as either true of false it’s what happens after that differs. Unlike IfTrue and IfFalse which either include everything or nothing the If tag selects an appropriate child tag to evaluate based upon the value you specifiy. If evaluates either the “Then” or the “Else” child tag. Of course you don’t have to specify either child tag so you can write the If without the “Then” or you can skip the “Else”. No matter what you can only have content of you specify one of these nodes and the value evaluates accordingly.
Using the Case tag:
The Case tag operates much like the If..Then…Else construct of the If tag. Just like the other conditionals you specify a single parameter which is the name of a value in the template engine. Unlike the other tags Case does not expect a True/False boolean operation. It matches the value of the variable specified with a specific child tag and evaluates only that tag. If no child tag matches the value then Case will attempt to evaluate the “Else” child tag (if present). If no tag matches the value and there is no “Else” child tag then Case will generate no output at all.
WordPress specific conditional tags:
Kazoo Templated Content currently supports 11 other conditional tags written specifically for WordPress including: is_archive, is_page, is_post, is_frontpage, is_post, is_admin, is_singular, is_sticky, is_time, is_date, and is_home. New WordPress related tags are being added every day so that more of the WordPress API is available without the need to write PHP code.
Other considerations:
The If..Then…Else, Case and the WordPress specific tags belong to that special group of tags that define their own child structure. These tags evaluate inner content based upon a child tag structure they expect. A mistake often made is to write content directly under one one of these type tags and then wonder why it isn’t showing up.
