Best viewed in anything

Using Conditionals in Templates

Conditionals are the door to having rich content on your pages without overloading your system. We see it all the time, numerous scripts and code heavy pages where the script or code is only needed for some pages. This is a result of template driven database sites.

Combining custom fields with conditionals is the road to a very capable and easy to use CMS and ExpressionEngine is designed to handle that with the greatest of ease.

Some Examples

You want to add a podcast with a player to your article. You also want enclosures in the RSS feed, but only for certain ones.
Here is the method, one of several with options.
Create a normal text (one line) field, lets call it mp3.
To have the player and download link in the article we don’t want anything to show, (nor be in the code), when the field is empty.

{if mp3}
<a href=”{mp3}”>Download todays podcast</a>.
Code for player -see Web Standards Embedding (also is under media menu).
{/if}

In the field for the mp3 one inserts the link to the file. This can be done using:
The file browser if it’s on the server.
Using the File extension to upload it if it’s under 10MB.
Any remote file.

Now, we may also need the javascript for proper embedding Flashobject. So we add the javascript to the head of the template. However, lets make it so that it isn’t on every page on the site, slowing things down, especially if there are a number of them.

Use the same principle in the head of the template:

{exp:weblog:entries limit=“1” weblog=“weblogname” disable=“member_data|trackbacks”}
{if mp3}
<script type=“text/javascript” etc
{/if}
{/exp:weblog entries}

This way when the field has content the scripts, player, and download link all show. When the field is empty no code nor scripts are written to the page.
Simple, powerful, and effective.

Here I have barely touched on the use of conditionals. This article will be added to showing many tricks with the EE CMS. Contact us for more information.

 


Shopping Cart Software - BKDesign - Reseller and Installer
Note: If you purchase from BKDesign we install the cart for free!
Constant Contact
Trusted Email Marketing
and Online Surveys

<< Back