Smarty Template

3,835 views

Published on

Smarty Template introduction

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

No Downloads
Views
Total views
3,835
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
79
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Smarty Template

  1. 1. Smarty Template Introduction Musavir Iftekhar
  2. 2. Installation <ul><li>Download form site.. www. smarty .net/ </li></ul><ul><li>Unzip and put smarty directory under your home directory </li></ul>
  3. 3. Using <ul><li>Better to create your own folders of templates else using default. </li></ul><ul><li><?php </li></ul><ul><li>define ( 'SMARTY_DIR', 'smarty/' ); </li></ul><ul><li>require_once ( SMARTY_DIR . 'Smarty.class.php '); </li></ul><ul><li>$smarty = new Smarty(); </li></ul><ul><li>$smarty->template_dir = './templates/'; </li></ul><ul><li>$smarty->compile_dir = './templates/compile/'; </li></ul><ul><li>$smarty->cache_dir = './templates/cache/'; </li></ul><ul><li>$smarty->caching = false; </li></ul><ul><li>?> </li></ul>
  4. 4. Common syntax <ul><li>$val = “some value”; </li></ul><ul><li>$smarty->assign(“smarty_sending_var”, $val); </li></ul><ul><li>$smarty->display(“page.tpl”);// page in template folder </li></ul><ul><li>------------------------------------------------------- </li></ul><ul><li>Page.tpl </li></ul><ul><li>{* these are comments of smarty *} </li></ul><ul><li>{$smarty_sending_var} // will show contents of sending values from the page </li></ul>
  5. 5. Variable Modifiers <ul><li>{$var|modifier} </li></ul><ul><li>{$title|capitalize} output: MYTITLE </li></ul><ul><li>{$title|count_words} output: 55 </li></ul><ul><li>{$dt, “%B %e, %Y”} output: December 22, 2005 </li></ul><ul><li>{$title|default:”some val”} output: if $title = null then some val </li></ul><ul><li>{$title|strip_tags} output: remove html tags from string </li></ul><ul><li>{$title|truncate:20} output: show reduce to 20 characters </li></ul>
  6. 6. Control Structures <ul><li>{if $var qualifier comprator} </li></ul><ul><li>Output </li></ul><ul><li>{elseif $var qualifier comprator} </li></ul><ul><li>Output </li></ul><ul><li>{else} </li></ul><ul><li>Output </li></ul><ul><li>{/if} </li></ul>
  7. 7. <ul><li>$my_array = array(‘val1’, ‘val2’, ‘val3’); </li></ul><ul><li>$smarty->assign(“array_val”, $my_array); </li></ul><ul><li>Handle in into template as </li></ul><ul><li>{foreach from=$my_array item=valout} </li></ul><ul><li>{$valout} </li></ul><ul><li>{/foreach} </li></ul><ul><li>If no value in array.. then </li></ul><ul><li>{foreach from=$my_array item=valout} </li></ul><ul><li>{$valout} </li></ul><ul><li>{foreachesles} </li></ul><ul><li>Output here </li></ul><ul><li>{/foreach} </li></ul>
  8. 8. Section <ul><li>$titles = arrray(‘a’, ‘b’, ‘c’); </li></ul><ul><li>$smarty->assign(“titles”, $titles); </li></ul><ul><li>$smarty->display(“titles.tpl”); </li></ul><ul><li>--------------------titles.tpl-------------------- </li></ul><ul><li>{section name=book loop=$titles} </li></ul><ul><li>{$titles[book]} </li></ul><ul><li>{/section} </li></ul><ul><li>Output:abc </li></ul>
  9. 9. <ul><li>$titles = arrray(‘title’=>’Eng’, ‘author’=>’Master’, ‘pub’=>’apex’); </li></ul><ul><li>$smarty->assign(“titles”, $titles); </li></ul><ul><li>$smarty->display(“titles.tpl”); </li></ul><ul><li>--------------------titles.tpl-------------------- </li></ul><ul><li>{section name=book loop=$titles} </li></ul><ul><li>{$titles[book].title} </li></ul><ul><li>{$titles[book].author} </li></ul><ul><li>{$titles[book].pub} </li></ul><ul><li>{/section} </li></ul><ul><li>Output:Eng Master apex </li></ul>
  10. 10. sectionelse <ul><li>$titles = arrray(‘a’, ‘b’, ‘c’); </li></ul><ul><li>$smarty->assign(“titles”, $titles); </li></ul><ul><li>$smarty->display(“titles.tpl”); </li></ul><ul><li>--------------------titles.tpl-------------------- </li></ul><ul><li>{section name=book loop=$titles} </li></ul><ul><li>{$titles[book]} </li></ul><ul><li>{sectionelse} </li></ul><ul><li>Output: if array = null </li></ul><ul><li>{/section} </li></ul><ul><li>Output:abc </li></ul>
  11. 11. Include <ul><li>{include file=“/usr/local/lib/pmnp/header.tpl”} </li></ul><ul><li>{* Execute some other Smarty statements here. *} </li></ul><ul><li>{include file=“/usr/local/temps/footer.tpl”} </li></ul><ul><li>{include file=“/local/header.tpl” title=“My home page”} </li></ul>
  12. 12. Insert <ul><li>Insert some advertisement page </li></ul><ul><li>function insert_banner(){ </li></ul><ul><li>} </li></ul><ul><li>------------------tpl file----------------------------- </li></ul><ul><li><img src=“imgs/{insert name=“banner”}.gif”/> </li></ul>
  13. 13. CSS with literal tag <ul><li>Insert css into tpl file directly </li></ul><ul><li><html><head> </li></ul><ul><li>{literal}<style type=“text/css”> </li></ul><ul><li>P{Margin: 5px;} </li></ul><ul><li></style> </li></ul><ul><li>{/literal} </li></ul><ul><li></head> </li></ul><ul><li>User Link to connect with outer css file </li></ul><ul><li><link style=“text/css” href=“mycss.css” /> </li></ul>
  14. 14. Caches <ul><li>$smarty->caching = 1 </li></ul>

×