SlideShare a Scribd company logo
Smarty
Why use Smarty?
 PHP is a template engine
 Separation of logic
◦ Reduced overhead in templating
What Smarty is not?
 NOT an application development
framework.
 NOT an MVC
 Smarty is the (V)iew component of
your application.
index.php index.tpl
include('Smarty.class.php');
$smarty = new Smarty;
$smarty->assign('name',
‘seth’);
$smarty->assign('address',
'45th & Harris');
$smarty->display('index.tpl');
<html>
<body>
User Information:
Name: {$name}
Address: {$address}
</body>
</html>
{assign}
index.php
$smarty->assign('name', ‘seth’);
$smarty->assign('address', '45th &
Harris');
index.tpl
Hi. My name is {$name}.
My address is {$address}.
{section}
index.php
$sangam = array(“prabha”,”lakshmi”);
$smarty->assign(“sangam”,$sangam);
index.tpl
{section name=member loop=$sangam}
{$sangam[member]}
{/section}
{foreach}
index.php
$arr = array(9 => 'Tennis', 3 => 'Swimming', 8 =>
'Coding');
$smarty->assign('myArray', $arr);
index.tpl
<ul>
{foreach from=$myArray key=k item=v}
<li>
{$k}: {$v}
</li>
{/foreach}
</ul>
{foreach}
index.php
$arr = array(9 => 'Tennis', 3 => 'Swimming', 8 =>
'Coding');
$smarty->assign('myArray', $arr);
index.tpl
<ul>
{foreach from=$myArray key=k item=v}
<li>
{$k}: {$v}
</li>
{/foreach}
</ul>
<ul>
<li>9: Tennis</li>
<li>3: Swimming</li>
<li>8: Coding</li>
</ul>
{foreach}
index.php
$smarty->assign(‘efforts', array(
array(
‘dev' => '1',
‘mnt' => '2',
‘qa' => '3'),
array(
‘dev' => ‘4',
‘mnt' => ‘5',
‘qa' => ‘6')
));
{foreach}
index.php
$smarty->assign(‘efforts', array(
array(
‘dev' => '1',
‘mnt' => '2',
‘qa' => '3'),
array(
‘dev' => ‘4',
‘mnt' => ‘5',
‘qa' => ‘6')
));
{foreach
name=outer
item=effort
from=$efforts}
{foreach
key=key
item=item
from=$effort}
{$key}: {$item} <br/>
{/foreach}
{/foreach}
Qualifier Smarty Syntax PHP Equivalent
== $a eq $b ==
!= $a neq $b !=
> $a gt $b >
< $a lt $b <
>= $a ge $b >=
<= $a le $b <=
=== $a === 0 ===
! not $a !
% $a mod $b %
Is [not] divisible by $a is not div by $b $a % $b == 0
Is [not] even $a is not even $a % 2 == 0
Is [not] even by $a is not even by $b ($a/$b)%2==0
Is [not] odd $a is not odd $a % 2 != 0
Is [not] odd by $a is not odd by $b ($a/$b)%2!=0
{if}{elseif}{else}
{if $name eq 'Blog'}
{* do something *}
{elseif $name eq $foo}
{* do something else *}
{/if}
{if is_array($foo) and count($foo) > 0}
{* do a foreach loop *}
{/if}
{capture}
{capture name=banner}
{include file='get_banner.tpl'}
{/capture}
{if $smarty.capture.banner ne ''}
<div id="banner">
{$smarty.capture.banner}
</div>
{/if}
{literal}
{literal}
<script type="text/javascript">
alert(“Hello World!”);
</script>
{/literal}
{literal}
<style type="text/css">
.container{
display:none;
}
</style>
{/literal}
{php}
{php}
global $foo, $bar;
if($foo == $bar){
echo 'This will be sent to browser';
}
$this->assign(‘flavor',‘Chocolate');
{/php}
{$flavor} is my fav ice cream :-)
AND
 We are done…

More Related Content

PPTX
Mootools selectors
PPTX
Let's write secure drupal code!
PDF
画像Hacks
PDF
WordCamp Portland 2018: PHP for WordPress
PDF
Shortcodes In-Depth
PDF
Make your own wp cli command in 10min
PDF
PHPUnit でよりよくテストを書くために
Mootools selectors
Let's write secure drupal code!
画像Hacks
WordCamp Portland 2018: PHP for WordPress
Shortcodes In-Depth
Make your own wp cli command in 10min
PHPUnit でよりよくテストを書くために

What's hot (20)

PDF
Difference between mysql_fetch_array and mysql_fetch_assoc in PHP
PPTX
4. CodeIgniter Greeting
PPSX
WordPress Theme Design and Development Workshop - Day 3
PPT
Oop php 5
PDF
Little Brown Bat by Elaine
PDF
Learning Perl 6 (NPW 2007)
PDF
WordPress: From Antispambot to Zeroize
ZIP
TerminalでTwitter
PPTX
6. CodeIgniter copy2
PPSX
XQDT - XQuery Getting Momentum in Eclipse
ZIP
Drupal Development (Part 2)
PDF
var, let in SIL
PDF
PHP and Rich Internet Applications
PDF
Gilette 3 final ad
DOCX
PPTX
Wp query
PDF
jQuery: out with the old, in with the new
PDF
Migrare da symfony 1 a Symfony2
PDF
Smelling your code
PPTX
5. hello popescu
Difference between mysql_fetch_array and mysql_fetch_assoc in PHP
4. CodeIgniter Greeting
WordPress Theme Design and Development Workshop - Day 3
Oop php 5
Little Brown Bat by Elaine
Learning Perl 6 (NPW 2007)
WordPress: From Antispambot to Zeroize
TerminalでTwitter
6. CodeIgniter copy2
XQDT - XQuery Getting Momentum in Eclipse
Drupal Development (Part 2)
var, let in SIL
PHP and Rich Internet Applications
Gilette 3 final ad
Wp query
jQuery: out with the old, in with the new
Migrare da symfony 1 a Symfony2
Smelling your code
5. hello popescu
Ad

Viewers also liked (7)

PPTX
Smartt presentation
PDF
PHPTAL with CakePHP
PDF
Smarty Template Engine
PDF
Opsiyonlar
PDF
Ad

Similar to Smarty (20)

PPT
Smarty sharing-2
PDF
Mike King - Storytelling by Numbers MKTFEST 2014
PDF
Storytelling By Numbers
PPT
Synapseindia reviews sharing intro cakephp
KEY
PHP security audits
PDF
Php reference sheet
DOCX
Smarty 3 overview
PDF
Building Testable PHP Applications
KEY
Geek Moot '09 -- Smarty 101
PPTX
Quick beginner to Lower-Advanced guide/tutorial in PHP
PDF
Php tips-and-tricks4128
PPTX
php is the most important programming language
PDF
Getting started with php
ODP
Google Cloud Challenge - PHP - DevFest GDG-Cairo
PPTX
PHP PPT FILE
PPTX
Loop
PDF
Web 8 | Introduction to PHP
PPT
Lecture n
PPT
Language literacy
PDF
PHP tips and tricks
Smarty sharing-2
Mike King - Storytelling by Numbers MKTFEST 2014
Storytelling By Numbers
Synapseindia reviews sharing intro cakephp
PHP security audits
Php reference sheet
Smarty 3 overview
Building Testable PHP Applications
Geek Moot '09 -- Smarty 101
Quick beginner to Lower-Advanced guide/tutorial in PHP
Php tips-and-tricks4128
php is the most important programming language
Getting started with php
Google Cloud Challenge - PHP - DevFest GDG-Cairo
PHP PPT FILE
Loop
Web 8 | Introduction to PHP
Lecture n
Language literacy
PHP tips and tricks

Recently uploaded (20)

PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Classroom Observation Tools for Teachers
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
RMMM.pdf make it easy to upload and study
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Business Ethics Teaching Materials for college
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
master seminar digital applications in india
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Institutional Correction lecture only . . .
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Classroom Observation Tools for Teachers
TR - Agricultural Crops Production NC III.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Supply Chain Operations Speaking Notes -ICLT Program
RMMM.pdf make it easy to upload and study
human mycosis Human fungal infections are called human mycosis..pptx
Business Ethics Teaching Materials for college
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Complications of Minimal Access Surgery at WLH
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
master seminar digital applications in india
Insiders guide to clinical Medicine.pdf
Final Presentation General Medicine 03-08-2024.pptx
Microbial disease of the cardiovascular and lymphatic systems
Institutional Correction lecture only . . .
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student

Smarty