0% found this document useful (0 votes)
11 views1 page

Smarty Template Design Guide

This document provides a cheat sheet for using Smarty, an open source PHP templating engine, outlining its basic syntax and functions for initializing Smarty, assigning and displaying variables, looping and conditional logic, built-in and custom functions, and its directory structure. It also covers modifiers, reserved variables, executing PHP in templates, and passing objects and arrays to templates.

Uploaded by

Tomas Garcia
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views1 page

Smarty Template Design Guide

This document provides a cheat sheet for using Smarty, an open source PHP templating engine, outlining its basic syntax and functions for initializing Smarty, assigning and displaying variables, looping and conditional logic, built-in and custom functions, and its directory structure. It also covers modifiers, reserved variables, executing PHP in templates, and passing objects and arrays to templates.

Uploaded by

Tomas Garcia
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Hasin Hayder

Smarty Cheat Sheet for Template Designers [Link]


hasin@[Link]

Initializing Smarty Loop Logic Directory Structure

include(”smarty/[Link]”); - your_script_folder
$smarty = new smarty();
{section name=id loop=$variable} {if $var == condition} | - smarty
something to do | | + libs
element : {$variable[id]} | | [Link]
Assigning Variables {/section} {elseif $var == condition} | - templates
something to do | | [Link]
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\[Link] 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(”[Link]”); 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(”[Link]”); 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}
{[Link]} $[Link] $[Link] $[Link]
{[Link]} $[Link] $[Link] $[Link] Execute PHP inside template
{$[Link]}
$[Link] $[Link] $[Link]
Passing associated array $[Link] $[Link] {php}
$[Link] $[Link] for($i=0;$i<10;$i++)
echo “Hello”;
$student=array(”name”=>”shumi”); $[Link] $[Link] {/php}
$smarty->assign(”student”,$std); $[Link] $[Link]

Smarty website : [Link]


Icon Courtesy : Tango Project ([Link] Smarty Book : [Link]

You might also like