Smarty

637 views

Published on

Smarty is a template engine for PHP. This presentation explains the basics of Smarty templating.

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
637
On SlideShare
0
From Embeds
0
Number of Embeds
35
Actions
Shares
0
Downloads
28
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Smarty

  1. 1. Why use Smarty? PHP is a template engine Separation of logic ◦ Reduced overhead in templating
  2. 2. What Smarty is not? NOT an application development framework. NOT an MVC Smarty is the (V)iew component of your application.
  3. 3. index.php index.tplinclude(Smarty.class.php); <html>$smarty = new Smarty; <body> User Information:$smarty->assign(name, Name: {$name} „seth‟); Address: {$address} </body>$smarty->assign(address, 45th & Harris); </html>$smarty->display(index.tpl);
  4. 4. {assign} index.php$smarty->assign(name, „seth‟);$smarty->assign(address, 45th & Harris); index.tplHi. My name is {$name}.My address is {$address}.
  5. 5. {section} index.php$sangam = array(“prabha”,”lakshmi”);$smarty->assign(“sangam”,$sangam); index.tpl{section name=member loop=$sangam} {$sangam[member]}{/section}
  6. 6. {foreach} index.php$arr = array(9 => Tennis, 3 => Swimming, 8 => Coding);$smarty->assign(myArray, $arr); index.tpl<ul> {foreach from=$myArray key=k item=v}<li> {$k}: {$v}</li> {/foreach}</ul>
  7. 7. {foreach} index.php$arr = array(9 => Tennis, 3 => Swimming, 8 => Coding);$smarty->assign(myArray, $arr); index.tpl<ul> {foreach from=$myArray key=k item=v}<li> {$k}: {$v} <ul></li> <li>9: Tennis</li> {/foreach} <li>3: Swimming</li> <li>8: Coding</li></ul> </ul>
  8. 8. {foreach} index.php$smarty->assign(„efforts, array( array( „dev => 1, „mnt => 2, „qa => 3), array( „dev => „4, „mnt => „5, „qa => „6) ));
  9. 9. {foreach} index.php {foreach$smarty->assign(„efforts, array( name=outer array( item=effort „dev => 1, from=$efforts} „mnt => 2, {foreach „qa => 3), key=key array( item=item from=$effort} „dev => „4, „mnt => „5, {$key}: {$item} <br/> „qa => „6) {/foreach} )); {/foreach}
  10. 10. Qualifier Smarty Syntax PHP Equivalent == $a eq $b == != $a neq $b != > $a gt $b > < $a lt $b < >= $a ge $b >= <= $a le $b <= === $a === 0 === ! not $a ! % $a mod $b %Is [not] divisible by $a is not div by $b $a % $b == 0 Is [not] even $a is not even $a % 2 == 0 Is [not] even by $a is not even by $b ($a/$b)%2==0 Is [not] odd $a is not odd $a % 2 != 0 Is [not] odd by $a is not odd by $b ($a/$b)%2!=0
  11. 11. {if}{elseif}{else}{if $name eq Blog} {* do something *}{elseif $name eq $foo} {* do something else *}{/if}{if is_array($foo) and count($foo) > 0} {* do a foreach loop *}{/if}
  12. 12. {capture}{capture name=banner} {include file=get_banner.tpl}{/capture}{if $smarty.capture.banner ne } <div id="banner"> {$smarty.capture.banner} </div>{/if}
  13. 13. {literal}{literal} <script type="text/javascript"> alert(“Hello World!”); </script>{/literal}{literal} <style type="text/css"> .container{ display:none; } </style>{/literal}
  14. 14. {php}{php} global $foo, $bar; if($foo == $bar){ echo This will be sent to browser; } $this->assign(„flavor,„Chocolate);{/php}{$flavor} is my fav ice cream :-)
  15. 15. AND We are done…

×