2. { smarty}
ondo alfry simanjuntak - ondo@701search.com
701search pte ltd - singapore
PHP TEMPLATE ENGINE
give some me reasons to love you
Extremely Fast
Efficient
Smart
Extensible
Configurable
Customizable
Built-in Caching Support
Plugin Architecture
Custom cache handling
Arbitrary Template Sources
4. { smarty}
ondo alfry simanjuntak - ondo@701search.com
701search pte ltd - singapore
PHP TEMPLATE ENGINE
Modifiers
Offers neat capability to filtering output within templates. And allows logic layout
to reside inside template rather than being apart of application logic.
{ can applied to any $variables }
{ autoloaded }
{ all php-functions can be used as modifiers implicitly }
6. { smarty}
ondo alfry simanjuntak - ondo@701search.com
701search pte ltd - singapore
PHP TEMPLATE ENGINE
Modifiers
#index.php
$smarty ->assign( ‘address’, $_POST[‘address’]);
$smarty->display(‘index.tpl’);
#views/index.tpl
<input type=‘text’ name=‘address’ value=‘{$address|escape|default:”Your address”}’>
Smarty provides very convenient default modifier, which is particularly
useful for populating forms
7. { smarty}
ondo alfry simanjuntak - ondo@701search.com
701search pte ltd - singapore
PHP TEMPLATE ENGINE
Merging Templates
#views/index.tpl
{include file=‘header.tpl’ val=‘One’ val2=‘Two’}
{include file=‘footer.tpl’}
Allow templates to references another templates and attributes that
specified in include tag will be made available as smarty variables in
include template
8. { smarty}
ondo alfry simanjuntak - ondo@701search.com
701search pte ltd - singapore
PHP TEMPLATE ENGINE
Array Iterations
#index.php
$smarty ->result(‘res’, pg_query($db, ‘SELECT * FROM …’))
#views/index.tpl
{foreach from=$res item=row}
{foreach from=$row key=column item=value}
{$column}: {$value} <br />
{/foreach}
{/foreach}
Entire array structure can be output inside smarty, without involving
the logic portion of the code
9. { smarty}
ondo alfry simanjuntak - ondo@701search.com
701search pte ltd - singapore
PHP TEMPLATE ENGINE
Conditional Expressions
{if $name eq 'Fred'}
Welcome Sir.
{elseif $name eq 'Wilma'}
Welcome Ma'am.
{else}
Welcome, whatever you are.
{/if}
{* an example with "or" logic *}
{if $name eq 'Fred' or $name eq 'Wilma'} ... {/if}
{* same as above *}
{if $name == 'Fred' || $name == 'Wilma'} ... {/if}
{* parenthesis are allowed *}
{if ( $amount < 0 or $amount > 1000 ) and $volume >= #minVolAmt#} ... {/if}
Support all operators found in PHP natively ==, ===, !=, !==, <, >,…
10. { smarty}
ondo alfry simanjuntak - ondo@701search.com
701search pte ltd - singapore
PHP TEMPLATE ENGINE
Capturing Output
#views/index.tpl
{capture name=admin_opts}
{if $admin ne “”}….{/if}
{/capture}
{foreach item=value from=$val}
{$value}
{if $smarty.capture.admin_opts ne “”}
{$smarty.capture.admin_opts}
{/if}
{/foreach}
Allow to capture output from a block of code in a buffer, and
use it later
11. { smarty}
ondo alfry simanjuntak - ondo@701search.com
701search pte ltd - singapore
PHP TEMPLATE ENGINE
Smarty & Javascript
#views/header.tpl
{literal}
<script type=“text/javascript”>
window.location = domain;
</script>
{/literal>
{literal} tag to prevent smarty for intercepting javascript logic
12. { smarty}
ondo alfry simanjuntak - ondo@701search.com
701search pte ltd - singapore
PHP TEMPLATE ENGINE
White-space cleanup
#views/index.tpl
{strip}
<table border=“0”>
<tr>
<td>
content
</td>
</tr>
</table>
{/strip>
#result
<table border=“0”><tr><td>content</td></tr></table>
{strip} tag to remove white space that generated by output
13. { smarty}
ondo alfry simanjuntak - ondo@701search.com
701search pte ltd - singapore
PHP TEMPLATE ENGINE
Caching
$cache_lifetime
Offer a tool for caching generated text, caching mechanism
controlled via a series of smarty object properties
Provide own cached mechanism (function)
$cache_id
$cache_handler_func $cache_modified_check
Cached data storage directory Cached data duration
Support the If-Modified-Since browser upload header
14. { smarty}
ondo alfry simanjuntak - ondo@701search.com
701search pte ltd - singapore
PHP TEMPLATE ENGINE
Cache Settings
Be sure cache storage directory is writeable by web server
$smarty->cache_dir = ‘./cache’ ;
$smarty->cache_lifetime = 600;
$smarty->cache_modified_check = true;
$smarty->caching = true;
if(! $smarty->is_cached( ‘index.tpl’ ) ) {
/*run queries, assign vars, etc …*/
}
$smarty->display( ‘cache.tpl’ );