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

Smarty Cheat Sheet: HTML Tables

This document provides a cheat sheet for using Smarty, an open source PHP template engine. It includes sections on initializing Smarty, assigning variables, template tags like loops and logic, template directory structure, debugging templates, and more. Common modifiers, built-in functions, and caching are also summarized. The cheat sheet spans two pages and provides essential information on the main components and usage of Smarty.

Uploaded by

GoCatGo
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)
450 views1 page

Smarty Cheat Sheet: HTML Tables

This document provides a cheat sheet for using Smarty, an open source PHP template engine. It includes sections on initializing Smarty, assigning variables, template tags like loops and logic, template directory structure, debugging templates, and more. Common modifiers, built-in functions, and caching are also summarized. The cheat sheet spans two pages and provides essential information on the main components and usage of Smarty.

Uploaded by

GoCatGo
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

Smarty Cheat Sheet version 2.

0 (page 1)
Initializing Smarty
include(smarty/[Link]); $smarty = new smarty();

Hasin Hayder Zend Certified Engineer [Link]

Loop
{section name=id loop=$variable} element : {$variable[id]} {sectionelse} If supplied variable is empty, youll see it {/section} {foreach item=curItem from=$items} element : {$curItem} {foreachelse} If supplied variable is empty, youll see it {/foreach}

Logic
{if $var == condition} something to do {elseif $var == condition} something to do {else} something else to do {/if }
| | | | | | | | |

Directory Structure
your_script_folder - smarty | - libs | | [Link] - templates | [Link] + templates_c [atleast 0665] + configs my_other_scripts.php blah_blah_script.php

Assigning Variables
General Format $smarty->assign(var,value); Example $smarty->assign(who, world);

Sample Template
Filename : templates\[Link] Hello {$who}

Processing Objects
Access Object Methods {$object->method param1=val param2=val} Accesing Object Properties {$object->property} Assign method output to variable {$object->method param1=val param2=val} assign=storage_var} Output: {$storage_var}

Display Output
$smarty->display([Link]);

Common Modifiers
capitalize, count_characters, cat, count_paragraphs, count_sentences,count_words, date_format, default, escape, indent, lower, nl2br, regex_replace, replace, spacify, string_format, strip, strip_tags, truncate, upper, wordwrap

Debugging templates
Add this line at the top {debug}

Capturing output in php variable


Use Fetch() function $output = $smarty->fetch([Link]); echo $output;

Builtin Functions
capture, config_load, foreach, foreachelse, include, include_php, insert if, elseif, else, ldelim, rdelim, literal, php, section, sectionelse, strip

Passing objects to template


$my_class=new my_object(); $smarty->register_object( object, $my_class);

Processing indexed array


{section name=id loop=$array} Current Item : {$array[id]}<br/> {/section}

Custom Functions
assign, counter, cycle, debug, eval, fetch, html_checkboxes, html_image html_options, html_radios, html_select_date, html_select_time html_table, math, mailto, popup_init, popup, textformat

Using modifiers
General Format {$variable|modifier:parameter} Example {$name|count_characters}

Processing associated array


{$[Link]} {$[Link]} {$[Link]}

Generating Tables using html_table() function


{html_table loop=$data cols=4 table_attr='border="0"'} Or {html_table loop=$data cols=4 tr_attr=$tr} where $tr = array('bgcolor="#eeeeee"','bgcolor="#dddddd"'));

Use PHP Functions as Modifier


You can use PHP functions as modifier. Separate the parameters by : Example {$number|pow:2}

Passing associated array


$std=array(name=>shumi); $smarty->assign(student,$std);

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

Smarty Book : [Link]

Smarty Cheat Sheet version 2.0 (page 2)


Change Template Directory
$smarty->template_dir=dir path

Hasin Hayder Zend Certified Engineer [Link] Cache Lifetime index index_prev inde_next iteration first last rownum show total loop
// set the cache_lifetime for [Link] to 5 minutes $smarty->cache_lifetime = 300; $smarty->display('[Link]'); // set the cache_lifetime for [Link] to 1 hour $smarty->cache_lifetime = 3600; $smarty->display('[Link]');

Special Section Properties


Some special section properties reduce the pain of templating a lot. General Format $[Link] {section name=id loop=$cust} Current Iteration: $[Link] {/section}

Change Compile Directory


$smarty->compile_dir=dir path

Change Config Directory


$smarty->config_dir=new dir

Turn off PHP in templates


$smarty->php_handling = any of the followings SMARTY_PHP_PASSTHRU SMARTY_PHP_QUOTE SMARTY_PHP_REMOVE SMARTY_PHP_ALLOW

Special Foreach Properties


iteration, last, first, total, show

Clearing Cache
// clear out all cache files $smarty->clear_all_cache(); // clear only cache for [Link] $smarty->clear_cache('[Link]');

Smarty reserved variables


$[Link] $[Link] $[Link] $[Link] $[Link] $[Link] $[Link] $[Link] $[Link] $[Link] $[Link] $[Link] $[Link] $[Link] $[Link] $[Link] $[Link]

Session Variables with Smarty


{$[Link].var_name}

Using Stricter Security


$smarty->security = true;

GET Variables with Smarty


{$[Link].var_name}

Turn off PHP functions in IF block


$smarty->security_settings[if_fu ncs] = array(disallowed functions one by one as array element); Now set $smarty->security=true;

POST Variables with Smarty


{$[Link].var_name}

Caching
Caching is used to speed up a call to display() or fetch() by saving its output to a file. You can turn on caching by using $smarty->caching=true;

Using PHP functions in IF block


{if pow($number, 2) eq 100} You supplied 10 as number {/if}

Cookie Variables with Smarty


{$[Link].var_name}

Checking cached content


$smarty->caching = true; if(!$smarty->is_cached('[Link]')) { // No cache available, do variable assignments here. $contents = get_database_contents(); $smarty->assign($contents); }

Multiple Caching
$smarty->display('[Link]',$cach e_id))); if(!$smarty->is_cached('[Link]' ,$cache_id ) { $contents = get_database_contents(); $smarty->assign($contents); } $smarty->display('[Link]',$cach e_id);

Security Settings
You can use the following array for $smarty->security_settings PHP_HANDLING IF_FUNCS INCLUDE_ANY PHP_TAGS MODIFIER_FUNCS ALLOW_CONSTANTS

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

Smarty Book : [Link]

You might also like