Smarty Cheat Sheet For Template Designers

ADVERTISEMENT

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

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go