SlideShare uma empresa Scribd logo
1 de 19
Baixar para ler offline
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');

Mais conteúdo relacionado

Mais procurados

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
 

Mais procurados (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
 

Destaque

Destaque (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
 

Semelhante 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
 

Semelhante 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
 

Mais 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

+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@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Último (20)

+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...
 
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
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
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...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
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
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 

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