340 likes | 452 Views
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.
E N D
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
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');
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>
So how does Smarty work? • php + tpl • template_c • php + template_c • HTML
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”}
Variables • Variable assignment • $smarty->assign(“smartyVar”, $phpVar); • Variables Usage • {$greeting} • {$row.firstName} • {$results.name.firstName} • {$row[0][0]} • {$person->firstName}
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…
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!
OK, I am convinced! • Hmm.. Let’s move ahead. • Downloading Smarty: • http://smarty.php.net/
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!
Variable Modifiers • cat • strip • strip_tags • upper • lower • capitalize • nl2br
Variable Modifiers - 2 • count_characters • count_words • count_sentences • count_paragraphs • default • escape • indent
Variable Modifiers - 3 • date_format • string_format • regex_replace • replace • spacify • truncate • wordwrap
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...
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');
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
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
Some more functions • capture • strip • literal • include • insert
Thinking the unthinkable • php • include_php • When to use?
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}
Handy tools • html_options • html_select_date • html_select_time • mailto
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');
Are you a programmer? • Debugging • Filters - Pre / Post / Output • $compile_check & $force_compile • Smarty & Security
Summary • What is Smarty? • Hello There! • How does it work? • Basics • Setting up • Variable Modifiers • Looping Constructs • Handy tools • Caching
Smarty Has You? • Resources: • http://smarty.php.net • http://www.php.net • Slides • http://www.radixsolutions.info/preso • Nirav Mehta • nirav@radixsolutions.info
Thank You! • Credits • LB2003 Team • Ullas Sahadevan • Q & A