You are on page 1of 1

Hasin Hayder

Smarty Cheat Sheet for Template Designers http://hasin.wordpress.com


hasin@somewherein.net

Initializing Smarty Loop Logic Directory Structure

include(”smarty/Smarty.class.php”); - your_script_folder
$smarty = new smarty();
{section name=id loop=$variable} {if $var == condition} | - smarty
something to do | | + libs
element : {$variable[id]} | | Smarty.class.php
Assigning Variables {/section} {elseif $var == condition} | - templates
something to do | | greet.tpl
General Format {else} | + templates_c [must be 0777]
$smarty->assign(”var”,”value”); {foreach item=curItem from=$items} | + configs
Example element : {$curItem} something else to do
| my_other_scripts.php
$smarty->assign(”who”, “world”);
{/foreach} {/if } | blah_blah_script.php
Sample Template Processing objects
Common Modifiers
Filename : templates\greet.tpl Access Object Methods
Hello {$who} {object->method
param1=”val”
capitalize, count_characters, cat, count_paragraphs,
Display Output param2=”val”}
count_sentences,count_words, date_format, default, escape, indent,
$smarty->display(”greet.tpl”); lower, nl2br, regex_replace, replace, spacify, string_format, strip, Accesing Object Properties
strip_tags, truncate, upper, wordwrap {object->property}
Debugging templates Assign method output to variable
Add this line at the top Builtin Functions {object->method
{debug} param1=”val”
param2=”val”}
Capturing output in php variable assign=”storage_var”}
capture, config_load, foreach, foreachelse, include, include_php, insert
if, elseif, else, ldelim, rdelim, literal, php, section, sectionelse, strip Output: {$storage_var}
Use Fetch() function
$output = Passing objects to template
$smarty->fetch(”greet.tpl”); Custom Functions
echo $output;
$my_class=new my_object();
$smarty->register_object(
Processing indexed array “object”, $my_class);
assign, counter, cycle, debug, eval, fetch, html_checkboxes, html_image
{section name=id loop=$array} html_options, html_radios, html_select_date, html_select_time Using modifiers
Current Item : {$array[id]}<br/> html_table, math, mailto, popup_init, popup, textformat
{/section} General Format
Processing associated array
Smarty reserved variables {$variable | modifier:parameter}
Example
{$name|count_characters}
{student.name} $smarty.get $smarty.now $smarty.version
{student.roll} $smarty.post $smarty.const $smarty.ldelim Execute PHP inside template
{$student.age}
$smarty.session $smarty.capture $smarty.rdelim
Passing associated array $smarty.session $smarty.config {php}
$smarty.env $smarty.section for($i=0;$i<10;$i++)
echo “Hello”;
$student=array(”name”=>”shumi”); $smarty.request $smarty.foreach {/php}
$smarty->assign(”student”,$std); $smarty.env $smarty.template

Smarty website : http://smarty.php.net


Icon Courtesy : Tango Project (http://tango-project.org) Smarty Book : http://packtpub.com/smarty/book

You might also like