1 / 33

Nirav Mehta Director, Radix Solutions

Nirav Mehta Director, Radix Solutions. Mission. Introduction to Smarty Smarty template language Hands-on example Brief about advance features Have fun!. What is Smarty?. Templates Developers Vs. Designers Separation of business logic and presentation logic Written entirely in PHP.

ella
Download Presentation

Nirav Mehta Director, Radix Solutions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Nirav Mehta Director, Radix Solutions

  2. Mission • Introduction to Smarty • Smarty template language • Hands-on example • Brief about advance features • Have fun!

  3. What is Smarty? • Templates • Developers Vs. Designers • Separation of business logic and presentation logic • Written entirely in PHP

  4. Hello There! – PHP File • index.php • include('Smarty.class.php'); • // create an object of Smarty • $smarty = new Smarty; • // you got to tell smarty what to display • $smarty->assign('greeting', 'I am in!'); • // go ahead, greet me! • $smarty->display('index.tpl');

  5. Hello There – Template File • index.tpl • {* Smarty *} • <html> • <head><title>Hello There!</title></head> • <body> • <p>My first Smarty page!</p> • <p>{$greeting}</p> • </body></html>

  6. So how does Smarty work? • php + tpl • template_c • php + template_c • HTML

  7. Basics • Comments • {* This is how you write comments *} • Functions • {include file=“header.tpl”} • Attributes to functions • {include file=“header.tpl” title=“My Page” css=“my.css”}

  8. Variables • Variable assignment • $smarty->assign(“smartyVar”, $phpVar); • Variables Usage • {$greeting} • {$row.firstName} • {$results.name.firstName} • {$row[0][0]} • {$person->firstName}

  9. Special Variables • Configuration Files • site.conf • title = “Sholay Revolutions” • css = “assets/main.css” • Accessing config variables in Smarty • {config_load file=“site.conf”} • {#title#} • {#css#} • Reserved Variables – get, post, request…

  10. Allright, but why Smarty? • It’s fast! • Compiles into PHP, so no parsing at runtime • Works great with accelerators, caches • Powerful template language – conditions, loops, functions, config files • Plugins • Brings world peace!

  11. OK, I am convinced! • Hmm.. Let’s move ahead. • Downloading Smarty: • http://smarty.php.net/

  12. Setting up Smarty • Get Smarty Library files • Smarty.class.php, Smarty_Compiler.class.php, Config_File.class.php, debug.tpl, /plugins/*.php • Supply absolute path to library directory • define('SMARTY_DIR','/usr/local/lib/php/Smarty/'); • require(SMARTY_DIR.'Smarty.class.php'); • Create Smarty instance of Smarty • $smarty = new Smarty; • You are in!

  13. Variable Modifiers • cat • strip • strip_tags • upper • lower • capitalize • nl2br

  14. Variable Modifiers - 2 • count_characters • count_words • count_sentences • count_paragraphs • default • escape • indent

  15. Variable Modifiers - 3 • date_format • string_format • regex_replace • replace • spacify • truncate • wordwrap

  16. Combining Modifiers • {$name|capitalize|spacify|truncate:45:"…"} • $smarty->assign("name", "Smarty is powerful, yet easy! "); • Will output: • S m a r t y I s P o w e r f u l , Y e t...

  17. Going round and round! • Looping constructs • include('Smarty.class.php'); • $smarty = new Smarty; • // Let's populate some data • $results[0][name] = "Jay"; • $results[0][email] = "jay@sholay.com"; • $results[1][name] = "Veeru"; • $results[1][email] = "veeru@sholay.com"; • $results[2][name] = "Gabbar"; • $results[2][email] = "gabbar@sholay.com"; • $smarty->assign('results', $results); • $smarty->display('index.tpl');

  18. Sections… • Section • {section name=item loop=$results} • name: {$results[item].name}<br> • email: {$results[item].email}<br> • {/section} • Section – name, loop, start, step, max, show

  19. Conditions Fulfilled! • if, elseif, else • Very flexible and powerful • Operators: • "eq", "ne","neq", "gt", "lt", "lte", "le", "gte" "ge","is even","is odd", "is not even","is not odd","not","mod","div by","even by","odd by","==","!=",">", "<","<=",">=" • Must have a matching /if

  20. Some more functions • capture • strip • literal • include • insert

  21. Thinking the unthinkable • php • include_php • When to use?

  22. Something more useful • cycle • {section name=name loop=$res} • <tr bgcolor="{cycle values="#eeeeee,#d0d0d0"}"> • <td>{$res[name]}</td> • </tr> • {/section} • fetch • {fetch file=http://www.sholay.com/basanti/ assign=“profile"} • {if $profile ne ""} <b>{$profile}</b> {/if}

  23. Handy tools • html_options • html_select_date • html_select_time • mailto

  24. Smarty does Caching • index.php • include('Smarty.class.php'); • $smarty = new Smarty; • $smarty->caching = true; • // If the page is not cached already, generate the content • if(!$smarty->is_cached('index.tpl')) • { • // Fetch up the content from a place of your liking! • $smarty->assign(‘person', array(‘jay‘, ‘veeru’)); • $smarty->assign(‘dialog', array(‘How many agents?', ‘Many Many!')); • } • $smarty->display('index.tpl');

  25. Are you a programmer? • Debugging • Filters - Pre / Post / Output • $compile_check & $force_compile • Smarty & Security

  26. Summary • What is Smarty? • Hello There! • How does it work? • Basics • Setting up • Variable Modifiers • Looping Constructs • Handy tools • Caching

  27. Smarty Has You? • Resources: • http://smarty.php.net • http://www.php.net • Slides • http://www.radixsolutions.info/preso • Nirav Mehta • nirav@radixsolutions.info

  28. Thank You! • Credits • LB2003 Team • Ullas Sahadevan • Q & A

More Related