PHPLD Template Tutorial – Part 2

The template files from Part 1 are written in a combination of HTML and Smarty. The Smarty engine takes variables assigned in the PHP code, interprets the data and spits out the web page; makes web development easier when the layout is separated from the coding.

I am working with PHPld v3.3 and it uses Smarty v2.6. The documentation can be found on Smarty’s website.

Here is a quick rundown of things you would see in the PHPld templates. All Smarty syntax begin and end with curly brackets. Content passed in from the PHP code to Smarty begin with a dollar sign.

{* *}
comments; does not appear in the source once the site is live

{if conditional} {/if}
{if conditional} {else} {/if}
{if conditional} {elseif conditional} {else} {/if}
a conditional statement; the ultimate philosophical question in code form: if something is true then you do this else if something else is true you do this else you do this if everything is false

to use constants defined in the PHP code, you need to prepend it with ‘$smarty.const.’

{include file="path/to/template.tpl" link="$variable"}
takes another template and drops it into the current template; optional link used to send variables in to be used by the included template

variable modifiers; modifies the format of the variable, make it upper case, make it lower case, trim off whitespace, etc

{php} {/php}
in some rare cases you may need to put PHP code in the template itself; but it’s frowned upon because the design and code should be kept separate

{literal} {/literal}
anything between the literal tags will not be interpreted by the smarty engine; mainly used for javascript such as google analytics or google adsense

{foreach from=$array key=$k item=$v} {/foreach}
loops through an array; example array in PHP would be $array = array("first"=>"John", "last"=>"Doe");; you would assign the PHP array to a Smarty variable and use foreach to loop through the array; on the first pass, $k = “first” and $v = “John”, on the second pass, $k = “last” and $v = “Doe”; used to display lists like the categories, links or articles

{assign var="variable name" value="variable value"}
assigns a value to a variable to be used later in the template

Example of Conditionals
{if condition1 and condition2} {/if}
if both condition1 and condition2 evaluate to true then execute the lines in-between

{if condition1 or condition2} {/if}
if one of the conditions evaluate to true then execute the lines in-between

{if value1 gt value2} {/if}
if value1 is greater than value2 then execute the lines in-between

{if value1 lt value2} {/if}
if value1 is less than value2 then execute the lines in-between

{if value1 eq value2} {/if}
if value1 is equal to value2 then execute the lines in-between

Example of Variable Modifiers

Strips double spaces, newlines and tabs with a single space by default or with a string entered after the colon

Encodes non-alphanumeric characters; defaults to HTML

You might see this in the code; trim is not a Smarty thing though, it’s a PHP function; it removes the white space before and after the variable

These are just a few of the ones I spotted doing a quick once over of the template files. There are many other commands available to review at Smarty’s website and probably a couple obscure ones I’ve missed in the templates. But, the above are the basics needed to get you where you want to go.

One thought on “PHPLD Template Tutorial – Part 2

  1. Pingback: PHPLD Template Tutorial – Part 3 | Frobie

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>