01/31/11

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

{$smarty.const.PHPCONSTANT}
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|modifier1|modifier2|etc}
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

{$variable|strip}
{$variable|strip:'replace'}
Strips double spaces, newlines and tabs with a single space by default or with a string entered after the colon

{$variable|escape}
Encodes non-alphanumeric characters; defaults to HTML

{$variable|trim}
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.

01/30/11

PHPLD Template Tutorial – Part 1

First, we look at the directory structure of a template and what each template file do. Each PHPld template generally contains the same files. I am not sure what some of the template files do though; they are probably for the pay-for-links version.

TemplateName/ – name of your template

images/ – store images
style/ – store css scripts
add_reciprocal.tpl –
article.tpl – article content page
article_search.tpl – article listing on search pages
articlelink.tpl – individual link block on browse pages
author.tpl – author profile page
banned_submit.tpl – user banned from submitting links message
category_search.tpl – category listing on search pages
category_select.tpl –
category_select_article.tpl –
category_tree.tpl –
contact.tpl – contact us page
detail.tpl – link detail page
footer.tpl – footer module
header.tpl – header module
index.html – unauthorized access page
left_side.tpl – left column content
link.tpl – listing of links in search pages
login.tpl – login form module
main.tpl – main layout of browse pages
page.tpl – not sure
pagerank.tpl – not sure
payment.tpl – not sure
paypal.tpl – not sure
profile.tpl – registration and account pages
readme.txt – template info
right_side.tpl – right column content
rss2.tpl – rss page
rules.tpl – article submission rules
screenshot.png – screenshot seen in admin
search.tpl – advanced search page
submit.tpl – link submit form
submit_article.tpl – article submit form
top_bar.tpl – menu
unauthorized.tpl – error messages