Smarty Template Engine

948 views

Published on

Smarty template moturunda sık kullanılan özellikleri ve Smart 3 ile birlikte gelen template inheritance örneklerini içeren bir sunum.

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

  • Be the first to like this

No Downloads
Views
Total views
948
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
14
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Smarty Template Engine

  1. 1. Smarty Mustafa Kırımlı
  2. 2. Smarty Basics {* this is a comment *} {* this multiline smarty comment is not sent to browser *} {* The header file with the main logo and stuff *} {include file='header.tpl'} {* Dev note: the $includeFile var is assigned in foo.php script *} <!-- Displays main content block --> {include file=$includeFile}
  3. 3. Smarty Access Variables {$foo} <-- displaying a simple variable (non array/object) {$foo[4]} <-- display the 5th element of a zero-indexed array {$foo.bar} <-- display the "bar" key value of an array, similar to PHP $foo['bar'] {$foo.$bar} <-- display variable key value of an array, similar to PHP $foo[$bar] {$foo->bar} <-- display the object property "bar" {$foo->bar()} <-- display the return value of object method "bar" {#foo#} <-- display the config file variable "foo" {$smarty.config.foo} <-- synonym for {#foo#} {$foo[bar]} <-- syntax only valid in a section loop, see {section} {assign var=foo value='baa'}{$foo} <-- displays "baa", see {assign}
  4. 4. Smarty Access Variables {$foo.bar.baz} {$foo.$bar.$baz} {$foo[4].baz} {$foo[4].$baz} {$foo.bar.baz[4]} {$foo->bar($baz,2,$bar)} <-- passing parameters {"foo"} <-- static values are allowed PHP-like syntax, alternative to "dot" syntax: {$foo[1]} // normal access {$foo['bar']} {$foo['bar'][1]} {$foo[$x+$x]} // index may contain any expression {$foo[$bar[1]]} // nested index {$foo[section_name]} // smarty {section} access, not array access!
  5. 5. Smarty PHP Variables {* display the server variable "SERVER_NAME" ($_SERVER['SERVER_NAME'])*} {$smarty.server.SERVER_NAME} Object chaining: {$object->method1($x)->method2($y)} Direct PHP function access: {time()} <?php define('MY_CONST_VAL','CHERRIES'); // the constant defined in php ?> {$smarty.const.MY_CONST_VAL}
  6. 6. Smarty Superglobals {* display value of page from URL ($_GET) http://www.example.com/index.php?page=foo *} {$smarty.get.page} {* display the variable "page" from a form ($_POST['page']) *} {$smarty.post.page} {* display the value of the cookie "username" ($_COOKIE['username']) *} {$smarty.cookies.username} {* display the server variable "SERVER_NAME" ($_SERVER['SERVER_NAME'])*} {$smarty.server.SERVER_NAME} {* display the system environment variable "PATH" *} {$smarty.env.PATH} {* display the php session variable "id" ($_SESSION['id']) *} {$smarty.session.id} {* display the variable "username" from merged get/post/cookies/server/env *} {$smarty.request.username}
  7. 7. Smarty {literal} <script> // the following braces are ignored by Smarty // since they are surrounded by whitespace function foobar { alert('foobar!'); } // this one will need literal escapement {literal} function bazzy {alert('foobar!');} {/literal} </script>
  8. 8. Smarty {php} {* this template includes a {php} block that assign's the variable $varX *} {php} global $foo, $bar; if($foo == $bar){ echo 'This will be sent to browser'; } // assign a variable to Smarty $this->assign('varX','Toffee'); {/php} {* output the variable *} <strong>{$varX}</strong> is my fav ice cream :-)
  9. 9. Smarty |replace <?php $smarty->assign('articleTitle', "Child's Stool Great for Use in Garden."); ?> Smarty: {$articleTitle} {$articleTitle|replace:'Garden':'Vineyard'} {$articleTitle|replace:' ':' '} Output: Child's Stool Great for Use in Garden. Child's Stool Great for Use in Vineyard. Child's Stool Great for Use in Garden.
  10. 10. Smarty |truncate <?php $smarty->assign('articleTitle', 'Two Sisters Reunite after Eighteen Years at Checkout Counter.'); ?> {$articleTitle} {$articleTitle|truncate} {$articleTitle|truncate:30} {$articleTitle|truncate:30:""} {$articleTitle|truncate:30:"---"} {$articleTitle|truncate:30:"":true} {$articleTitle|truncate:30:"...":true} {$articleTitle|truncate:30:'..':true:true} Two Sisters Reunite after Eighteen Years at Checkout Counter. Two Sisters Reunite after Eighteen Years at Checkout Counter. Two Sisters Reunite after... Two Sisters Reunite after Two Sisters Reunite after--Two Sisters Reunite after Eigh Two Sisters Reunite after E... Two Sisters Re..ckout Counter.
  11. 11. Smarty |upper|lower {$articleTitle} {$articleTitle|lower} {$articleTitle} {$articleTitle|upper}
  12. 12. Smarty {html_table} <?php $smarty->assign( 'data', array(1,2,3,4,5,6,7,8,9) ); $smarty->assign( 'tr', array('bgcolor="#eeeeee"','bgcolor="#dddddd"') ); $smarty->display('index.tpl'); ?> {**** Example One ****} {html_table loop=$data} <table border="1"> <tbody> <tr><td>1</td><td>2</td><td>3</td></tr> <tr><td>4</td><td>5</td><td>6</td></tr> <tr><td>7</td><td>8</td><td>9</td></tr> </tbody> </table>
  13. 13. Smarty {html_table} {**** Example Two ****} {html_table loop=$data cols=4 table_attr='border="0"'} <table border="0"> <tbody> <tr><td>1</td><td>2</td><td>3</td><td>4</td></tr> <tr><td>5</td><td>6</td><td>7</td><td>8</td></tr> <tr><td>9</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr> </tbody> </table>
  14. 14. Smarty {html_table} {**** Example Three ****} {html_table loop=$data cols="first,second,third,fourth" tr_attr=$tr} <table border="1"> <thead> <tr> <th>first</th><th>second</th><th>third</th><th>fourth</th> </tr> </thead> <tbody> <tr bgcolor="#eeeeee"><td>1</td><td>2</td><td>3</td><td>4</td></tr> <tr bgcolor="#dddddd"><td>5</td><td>6</td><td>7</td><td>8</td></tr> <tr bgcolor="#eeeeee"><td>9</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr> </tbody> </table>
  15. 15. Smarty {html_options} <?php $smarty->assign('myOptions', array( 1800 => 'Joe Schmoe', 9904 => 'Jack Smith', 2003 => 'Charlie Brown') ); $smarty->assign('mySelect', 9904); ?> Smarty: {html_options name=foo options=$myOptions selected=$mySelect} Output: <select name="foo"> <option value="1800">Joe Schmoe</option> <option value="9904" selected="selected">Jack Smith</option> <option value="2003">Charlie Brown</option> </select>
  16. 16. Template Inheritance (layout.tpl) <html> <head> <title>{block name=title}Default Page Title{/block}</title> {block name=head}{/block} </head> <body> {block name=body}{/block} </body> </html>
  17. 17. Template Inheritance (onecolumn.tpl) {extends file='layout.tpl'} {block name=head} <link href="/css/site.css" rel="stylesheet" type="text/css"/> <script src="/js/site.js"></script> {/block}
  18. 18. Template Inheritance (homepage.tpl) {extends file=onecolumn.tpl'} {block name=title}My Page Title{/block} {block name=head} <link href="/css/homepage.css" rel="stylesheet" type="text/css"/> <script src="/js/homepage.js"></script> {/block} {block name=body}My HTML Page Body goes here{/block} $smarty->display(homepage.tpl');
  19. 19. Template Inheritance {append} {extends file=onecolumn.tpl'} {block name=title}My Page Title{/block} {block name=head append} <link href="/css/homepage.css" rel="stylesheet" type="text/css"/> <script src="/js/homepage.js"></script> {/block} {block name=body}My HTML Page Body goes here{/block} $smarty->display(homepage.tpl');

×