Part of the EllisLab Network

Blog & News

Lisa Wess
Director of Community Services

Dissecting the Default Templates: Conditional Variable Pairs

So, we’ve now been introduced to many of the core concepts in ExpressionEngine, including parameters, variables, paths, assigned variables, and variable pairs.  Now it’s time to begin our adventures into conditionals.

Conditionals are a powerful concept that allow you to show data based on the existence of some other value stored in the system.  You can create conditionals for any variable in ExpressionEngine; however, there are some variables that ExpressionEngine gives you to make things easier.  This includes testing to see if comments are allowed, and if trackbacks are allowed.  And so we reach the next few lines of our template.

Here they are:

{if allow_comments}
({comment_total}) <a href="{url_title_path="{my_template_group}/comments"}">Comments</a> ï
{
/if}

{if allow_trackbacks}
({trackback_total}) <a href="{trackback_path="{my_template_group}/trackbacks"}">Trackbacks</a> ï
{
/if}

Now, we see in here many things that we recognize.  We see the url_title_path= and trackback_path= which we recognize as a form of path variable; we also see {my_template_group} which we know is an assigned variable, and of course, {comment_total} and {trackback_total} which are standard variables for the weblog entries tag.

However, here is this new thing, we don’t see {if} as a variable in the weblog entries’ variable list, plus we also notice that there is a space separating the if and the allow_comments; we know that variables are always one item, with no spaces.  So, how about we search for the whole shebang?  Head to the docs now and search for “if allow_comments”, and we find the documentation on conditional variables for the weblog entries tag.  The first two in the list are the ones we’re working with, perfect!

We can see from that documentation that {if allow_comments} opens the conditional, and we can then put anything in between; it doesn’t have to be what’s in the default template, it could simply say, “Hey, respond to me!”.  We also see that there is a closing tag: {/if} - making this a variable pair: it contains data between the opening and closing tag.

So, here we have gotten a slight taste of pre-created conditionals made available to you in ExpressionEngine.  Next time we’ll get into paginating your entries list; and that will end the beginning exploration of the weblog entries tag, so that we can move on to some other features that the default template offers.