SlideShare una empresa de Scribd logo
1 de 19
Descargar para leer sin conexión
Smarty
Mustafa Kırımlı
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}
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}
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!
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}
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}
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>
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 :-)
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.
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.
Smarty |upper|lower
{$articleTitle}
{$articleTitle|lower}
{$articleTitle}
{$articleTitle|upper}
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>
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>
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>
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>
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>
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}
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');
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');

Más contenido relacionado

La actualidad más candente

PHP and MySQL PHP Written as a set of CGI binaries in C in ...
PHP and MySQL PHP Written as a set of CGI binaries in C in ...PHP and MySQL PHP Written as a set of CGI binaries in C in ...
PHP and MySQL PHP Written as a set of CGI binaries in C in ...
webhostingguy
 
Total World Domination with i18n (es)
Total World Domination with i18n (es)Total World Domination with i18n (es)
Total World Domination with i18n (es)
Zé Fontainhas
 
Short Intro to PHP and MySQL
Short Intro to PHP and MySQLShort Intro to PHP and MySQL
Short Intro to PHP and MySQL
Jussi Pohjolainen
 
News of the Symfony2 World
News of the Symfony2 WorldNews of the Symfony2 World
News of the Symfony2 World
Fabien Potencier
 

La actualidad más candente (20)

Intro to PHP
Intro to PHPIntro to PHP
Intro to PHP
 
Intro to php
Intro to phpIntro to php
Intro to php
 
Web technology html5 php_mysql
Web technology html5 php_mysqlWeb technology html5 php_mysql
Web technology html5 php_mysql
 
Sa
SaSa
Sa
 
Zero to SOLID
Zero to SOLIDZero to SOLID
Zero to SOLID
 
My cool new Slideshow!
My cool new Slideshow!My cool new Slideshow!
My cool new Slideshow!
 
slidesharenew1
slidesharenew1slidesharenew1
slidesharenew1
 
PHP and MySQL PHP Written as a set of CGI binaries in C in ...
PHP and MySQL PHP Written as a set of CGI binaries in C in ...PHP and MySQL PHP Written as a set of CGI binaries in C in ...
PHP and MySQL PHP Written as a set of CGI binaries in C in ...
 
PHP Workshop Notes
PHP Workshop NotesPHP Workshop Notes
PHP Workshop Notes
 
Php with my sql
Php with my sqlPhp with my sql
Php with my sql
 
Total World Domination with i18n (es)
Total World Domination with i18n (es)Total World Domination with i18n (es)
Total World Domination with i18n (es)
 
Introducation to php for beginners
Introducation to php for beginners Introducation to php for beginners
Introducation to php for beginners
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
Intermediate PHP
Intermediate PHPIntermediate PHP
Intermediate PHP
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
Short Intro to PHP and MySQL
Short Intro to PHP and MySQLShort Intro to PHP and MySQL
Short Intro to PHP and MySQL
 
07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboards07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboards
 
News of the Symfony2 World
News of the Symfony2 WorldNews of the Symfony2 World
News of the Symfony2 World
 
Refactoring using Codeception
Refactoring using CodeceptionRefactoring using Codeception
Refactoring using Codeception
 
PHP for hacks
PHP for hacksPHP for hacks
PHP for hacks
 

Destacado

Destacado (8)

Smarty + PHP
Smarty + PHPSmarty + PHP
Smarty + PHP
 
jQuery für Anfänger
jQuery für AnfängerjQuery für Anfänger
jQuery für Anfänger
 
Smarty 2
Smarty 2Smarty 2
Smarty 2
 
How to Use PHP Smarty Template
How to Use PHP Smarty TemplateHow to Use PHP Smarty Template
How to Use PHP Smarty Template
 
Smarty
SmartySmarty
Smarty
 
Deploying PHP applications with Phing
Deploying PHP applications with PhingDeploying PHP applications with Phing
Deploying PHP applications with Phing
 
Framework Auswahlkriterin, PHP Unconference 2009 in Hamburg
Framework Auswahlkriterin, PHP Unconference 2009 in Hamburg Framework Auswahlkriterin, PHP Unconference 2009 in Hamburg
Framework Auswahlkriterin, PHP Unconference 2009 in Hamburg
 
Building and deploying PHP applications with Phing
Building and deploying PHP applications with PhingBuilding and deploying PHP applications with Phing
Building and deploying PHP applications with Phing
 

Similar a Smarty Template Engine

Be RESTful (Symfony Camp 2008)
Be RESTful (Symfony Camp 2008)Be RESTful (Symfony Camp 2008)
Be RESTful (Symfony Camp 2008)
Fabien Potencier
 
Alessandro sf 2010
Alessandro sf 2010Alessandro sf 2010
Alessandro sf 2010
Puppet
 
PHP 5.3 And PHP 6 A Look Ahead
PHP 5.3 And PHP 6 A Look AheadPHP 5.3 And PHP 6 A Look Ahead
PHP 5.3 And PHP 6 A Look Ahead
thinkphp
 
Aura Project for PHP
Aura Project for PHPAura Project for PHP
Aura Project for PHP
Hari K T
 
vfsStream - effective filesystem mocking
vfsStream - effective filesystem mocking vfsStream - effective filesystem mocking
vfsStream - effective filesystem mocking
Sebastian Marek
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
Seri Moth
 
C A S Sample Php
C A S Sample PhpC A S Sample Php
C A S Sample Php
JH Lee
 
SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Development
jsmith92
 

Similar a Smarty Template Engine (20)

Be RESTful (Symfony Camp 2008)
Be RESTful (Symfony Camp 2008)Be RESTful (Symfony Camp 2008)
Be RESTful (Symfony Camp 2008)
 
Alessandro sf 2010
Alessandro sf 2010Alessandro sf 2010
Alessandro sf 2010
 
PHP 5.3 And PHP 6 A Look Ahead
PHP 5.3 And PHP 6 A Look AheadPHP 5.3 And PHP 6 A Look Ahead
PHP 5.3 And PHP 6 A Look Ahead
 
Nashvile Symfony Routes Presentation
Nashvile Symfony Routes PresentationNashvile Symfony Routes Presentation
Nashvile Symfony Routes Presentation
 
The Origin of Lithium
The Origin of LithiumThe Origin of Lithium
The Origin of Lithium
 
Aura Project for PHP
Aura Project for PHPAura Project for PHP
Aura Project for PHP
 
Smarty 3 overview
Smarty 3 overviewSmarty 3 overview
Smarty 3 overview
 
Puppet Camp LA 2015: Basic Puppet Module Design (Beginner)
Puppet Camp LA  2015: Basic Puppet Module Design (Beginner)Puppet Camp LA  2015: Basic Puppet Module Design (Beginner)
Puppet Camp LA 2015: Basic Puppet Module Design (Beginner)
 
Puppetcamp module design talk
Puppetcamp module design talkPuppetcamp module design talk
Puppetcamp module design talk
 
vfsStream - effective filesystem mocking
vfsStream - effective filesystem mocking vfsStream - effective filesystem mocking
vfsStream - effective filesystem mocking
 
Extbase and Beyond
Extbase and BeyondExtbase and Beyond
Extbase and Beyond
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
 
Introducing CakeEntity
Introducing CakeEntityIntroducing CakeEntity
Introducing CakeEntity
 
dcs plus Catalogue 2015
dcs plus Catalogue 2015dcs plus Catalogue 2015
dcs plus Catalogue 2015
 
C A S Sample Php
C A S Sample PhpC A S Sample Php
C A S Sample Php
 
Get into the FLOW with Extbase
Get into the FLOW with ExtbaseGet into the FLOW with Extbase
Get into the FLOW with Extbase
 
SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Development
 
Advanced symfony Techniques
Advanced symfony TechniquesAdvanced symfony Techniques
Advanced symfony Techniques
 
ReUse Your (Puppet) Modules!
ReUse Your (Puppet) Modules!ReUse Your (Puppet) Modules!
ReUse Your (Puppet) Modules!
 
Introduction in php part 2
Introduction in php part 2Introduction in php part 2
Introduction in php part 2
 

Más de Mustafa Kırımlı (8)

Compute @ edge
Compute @ edgeCompute @ edge
Compute @ edge
 
Why and Way of Using Docker & Kubernetes
Why and Way of Using Docker & KubernetesWhy and Way of Using Docker & Kubernetes
Why and Way of Using Docker & Kubernetes
 
Running kubernetes
Running kubernetesRunning kubernetes
Running kubernetes
 
Spot instances on kubernetes
Spot instances on kubernetesSpot instances on kubernetes
Spot instances on kubernetes
 
Opsiyonlar
OpsiyonlarOpsiyonlar
Opsiyonlar
 
PHP Dosya Yükleme
PHP Dosya YüklemePHP Dosya Yükleme
PHP Dosya Yükleme
 
HTML ve CSS
HTML ve CSSHTML ve CSS
HTML ve CSS
 
Http Yaşam Döngüsü
Http Yaşam DöngüsüHttp Yaşam Döngüsü
Http Yaşam Döngüsü
 

Último

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 

Smarty Template Engine

  • 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. 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. 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. 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. 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. 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. 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. 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. 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.
  • 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. 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. 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. 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. 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. 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. 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. 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');