Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Smarty sharing-2

Smarty Templates Engine

  • Be the first to comment

  • Be the first to like this

Smarty sharing-2

  1. 1. { smarty } ondo alfry simanjuntak - ondo@701search.com 701search pte ltd - singapore PHP TEMPLATE ENGINE
  2. 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
  3. 3. { smarty} ondo alfry simanjuntak - ondo@701search.com 701search pte ltd - singapore PHP TEMPLATE ENGINE Install and Start Download libraries from www.smarty.net, requires a web server running PHP 5.2 or greater. #index.php require_once('Smarty-3.1.18/libs/Smarty.class.php'); $smarty = new Smarty(); $smarty->template_dir = 'views'; $smarty->compile_dir = 'tmp'; $array = array ( 'Torres' => 28, 'Xavi' => 30, 'Lampard' => 32); $array ->assign('player', $array); $smarty->display('index.tpl'); #views/index.tpl {foreach from=$player key=k item=i} {$k} = {$i} <br /> {/foreach} #result Torres = 28 Xavi = 30 Lampard = 32
  4. 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 }
  5. 5. { smarty} ondo alfry simanjuntak - ondo@701search.com 701search pte ltd - singapore PHP TEMPLATE ENGINE Sample Codes #index.php $string = 'Hello world'; $date = '2014-04-15'; $array = array ( 'Torres' => 28, 'Xavi' => 30, 'Lampard' => 32); $array ->assign('hello', $string); $array ->assign('date', $date); $array ->assign('player', $array); #views/index.tpl {$hello} - {$date|date_format} {php} sha1('hello'); {/php} <table> {foreach from=$player key=k item=i} <tr style=”background: {cyle values='silver, gray'}”> <td>{$k}</td> = <td>{$i} </td> </tr> {/foreach} </table>
  6. 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. 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. 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. 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. 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. 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. 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. 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. 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’ );

×