SlideShare ist ein Scribd-Unternehmen logo
1 von 49
Smarty 101
What? Why? How?


Ted Kulp - Shift Refresh, Inc.
What?
What?
Arguably the most widely used PHP templating
system
What?
Arguably the most widely used PHP templating
system

Created by Andrei Zmievski
What?
Arguably the most widely used PHP templating
system

Created by Andrei Zmievski

Tightly integrated into the CMSMS core
What?
Arguably the most widely used PHP templating
system

Created by Andrei Zmievski

Tightly integrated into the CMSMS core

Seamlessly used on all page, module and other
templates throughout the system
What?
Arguably the most widely used PHP templating
system

Created by Andrei Zmievski

Tightly integrated into the CMSMS core

Seamlessly used on all page, module and other
templates throughout the system

Released under the LGPL -- basically means it’s
pretty liberally licensed
Why?
Why?
Separates the display logic cleanly from the
controller logic
Why?
Separates the display logic cleanly from the
controller logic

Much safer by not allowing full range of PHP
functionality in a template
Why?
Separates the display logic cleanly from the
controller logic

Much safer by not allowing full range of PHP
functionality in a template

Allows for many tricks to make complicated
display easier
Why?
Separates the display logic cleanly from the
controller logic

Much safer by not allowing full range of PHP
functionality in a template

Allows for many tricks to make complicated
display easier

Get a lot of web-friendly functionality for free
Why?
Separates the display logic cleanly from the
controller logic

Much safer by not allowing full range of PHP
functionality in a template

Allows for many tricks to make complicated
display easier

Get a lot of web-friendly functionality for free

And mainly...
Why?
                             Ugly                               Not So Much
<html>                                                       <html>
<head>                                                       <head>
<title><?php echo $title; ?></title>                         <title>{$title}</title>
<?php cms_display_stylesheet() ?>                            {stylesheet}
<?php cms_display_metadata() ?>                              {metdata}
</head>                                                      </head>
<body>                                                       <body>
<div id=”body”>                                              <div id=”body”>
<?php cms_display_content(‘content_en’) ?>                   {content}
</div>                                                       </div>
<div id=”footer”>                                            <div id=”footer”>
<?php printf(‘%m %d %Y’, cms_page_data(‘date_modiïŹed’)) ?>   {modiïŹed_date|cms_date_format}
</div>                                                       </div>
</body>                                                      </body>
</html>                                                      </html>




                      It’s just plain easier to read
Absolute musts
Absolute musts


Literal tags
Absolute musts


Literal tags

Getting available variables
Absolute musts


Literal tags

Getting available variables

ModiïŹers
Literal Tags
Literal Tags

Escapes javascript
Literal Tags

Escapes javascript

Escapes CSS
Literal Tags

Escapes javascript

Escapes CSS

Really... escapes anything with { or } in it.
Smarty gets confused
Literal Tags

Escapes javascript

Escapes CSS

Really... escapes anything with { or } in it.
Smarty gets confused

So literal tags basically have Smarty ignore
everything between
Literal Tags
<script type="text/javascript">
/ Get all of the tabs and add an onmouseover
 /
/ event listener to each tab
 /
var tabs = getElementsByClass('tab',
    document.getElementById('featuresarea-tabs'),'li') ;
for(i=0; i<tabs.length; i++)
{
! var this_tab = ! document.getElementById(tabs[i].id);
! this_tab.onmouseover = function(){
! ! showtab(this.id);
! ! document.getElementById(this.id).className += " selected";
! };
}!
//]]>
</script>
Literal Tags
{literal}
<script type="text/javascript">
/ Get all of the tabs and add an onmouseover
 /
/ event listener to each tab
 /
var tabs = getElementsByClass('tab',
     document.getElementById('featuresarea-tabs'),'li') ;
for(i=0; i<tabs.length; i++)
{
! var this_tab = ! document.getElementById(tabs[i].id);
! this_tab.onmouseover = function(){
! ! showtab(this.id);
! ! document.getElementById(this.id).className += " selected";
! };
}!
//]]>
</script>
{/literal}
The Immortal Question



 How do I know what variables are available
 to me in my template?
Answer

{get_template_vars} !!!!!!!!

Gives you all the variables that are available
to that template. It’s a must have.

Know It

Use It

Love It
{get_template_vars}
On a regular page, outputs something like:
SCRIPT_NAME = /1.6.x/index.php
app_name = CMS
sitename = CMS Made Simple Site
lang =
encoding = utf-8
gCms = Object
cgsimple = Object
content_obj = Object
content_id = 69
page = get_template_vars
page_id = get_template_vars
page_name = get_template_vars
etc.
{get_template_vars}
On a regular page, outputs something like:
SCRIPT_NAME = /1.6.x/index.php
app_name = CMS
sitename = CMS Made Simple Site
lang =
encoding = utf-8
gCms = Object
cgsimple = Object
content_obj = Object
content_id = 69
page = get_template_vars
page_id = get_template_vars
page_name = get_template_vars
etc.                               Which means you can use:
                                          {$page_name}
                                  in this template and get the
                                           page’s name.
ModiïŹers
ModiïŹers
Take output and modiïŹes it directly in
Smarty.
ModiïŹers
Take output and modiïŹes it directly in
Smarty.

Allows multiple modiïŹers to be chained.
ModiïŹers
Take output and modiïŹes it directly in
Smarty.

Allows multiple modiïŹers to be chained.

Format:   {$variable|modiïŹer_function:extra:parameters}
ModiïŹers
Take output and modiïŹes it directly in
Smarty.

Allows multiple modiïŹers to be chained.

Format:   {$variable|modiïŹer_function:extra:parameters}


Chaining:   {$variable|modiïŹer_function|another_one:with:params}
ModiïŹers
Take output and modiïŹes it directly in
Smarty.

Allows multiple modiïŹers to be chained.

Format:   {$variable|modiïŹer_function:extra:parameters}


Chaining:   {$variable|modiïŹer_function|another_one:with:params}


Smarty comes with a lot of nice modiïŹers.
See chapters 5 and 6 for some examples.
Examples
Examples
{$title|upper} -- Convert the string to upper
case
Examples
{$title|upper} -- Convert the string to upper
case

{$title|truncate:40:’...’} -- Truncate the string at
40 characters and put an ellipsis on it
Examples
{$title|upper} -- Convert the string to upper
case

{$title|truncate:40:’...’} -- Truncate the string at
40 characters and put an ellipsis on it

{$smarty.now|date_format:”%Y/%m/%d”} -- Get
the current date and give it a nice formatting
Examples
{$title|upper} -- Convert the string to upper
case

{$title|truncate:40:’...’} -- Truncate the string at
40 characters and put an ellipsis on it

{$smarty.now|date_format:”%Y/%m/%d”} -- Get
the current date and give it a nice formatting

{$variable|var_dump} -- Any PHP function will
work
Tricks and Examples


{capture}

{cycle}

{mailto}
{capture}

Allows you capture output of smarty tags
and variables and used it elsewhere

Useful for testing if something has output
data

Allows you to get around issues where path
of execution isn’t correct
{capture} Example

Div should only show if there is content

  {capture name=outp}{content block=‘sideblock’|trim}{/capture}
  {if $smarty.capture.outp}
     <div id=”sideblock”>
       {$smarty.capture.outp}
     </div>
  {/if}
{cycle}


Used to alternate a set of values

Useful for alternating classes - ex.

  Alternating table rows

  Multiple columns
{cycle} Example

Split display of items into 2 columns

      {foreach from=$values item=‘the_item’}
        <div class=”{cycle values=”col1,col2”}”>
          {$the_item}
        </div>
      {/foreach}
{escape}


Encodes a variable in various formats -- ex.

  Encode an email address so that it’s not
  easily scraped by a spam bot

  Encode output to make sure it’s valid xhtml
{escape} example

Make a legible email address more difïŹcult
         to read via the source.

       {$user.email|escape:”hexentity”}

 (Converts email@domain.com to %62%64, etc.)
Resources


http://smarty.net/manual/en (Please read
chapters 3 and 4 at a minimum!!!)

http://wiki.cmsmadesimple.org
Thank you!

Weitere Àhnliche Inhalte

Was ist angesagt?

ĐĄĐžŃŃ‚Đ”ĐŒĐ° Ń€Đ”ĐœĐŽĐ”Ń€ĐžĐœĐłĐ° ĐČ Magento
ĐĄĐžŃŃ‚Đ”ĐŒĐ° Ń€Đ”ĐœĐŽĐ”Ń€ĐžĐœĐłĐ° ĐČ MagentoĐĄĐžŃŃ‚Đ”ĐŒĐ° Ń€Đ”ĐœĐŽĐ”Ń€ĐžĐœĐłĐ° ĐČ Magento
ĐĄĐžŃŃ‚Đ”ĐŒĐ° Ń€Đ”ĐœĐŽĐ”Ń€ĐžĐœĐłĐ° ĐČ MagentoMagecom Ukraine
 
PHP security audits
PHP security auditsPHP security audits
PHP security auditsDamien Seguy
 
Introduzione JQuery
Introduzione JQueryIntroduzione JQuery
Introduzione JQueryorestJump
 
Maintainable JavaScript 2012
Maintainable JavaScript 2012Maintainable JavaScript 2012
Maintainable JavaScript 2012Nicholas Zakas
 
jQuery in 15 minutes
jQuery in 15 minutesjQuery in 15 minutes
jQuery in 15 minutesSimon Willison
 
Php Tutorial | Introduction Demo | Basics
 Php Tutorial | Introduction Demo | Basics Php Tutorial | Introduction Demo | Basics
Php Tutorial | Introduction Demo | BasicsShubham Kumar Singh
 
ć‰ç«ŻæŠ‚èż°
ć‰ç«ŻæŠ‚èż°ć‰ç«ŻæŠ‚èż°
ć‰ç«ŻæŠ‚èż°Ethan Zhang
 
jQuery Plugin Creation
jQuery Plugin CreationjQuery Plugin Creation
jQuery Plugin Creationbenalman
 
Phphacku iitd
Phphacku iitdPhphacku iitd
Phphacku iitdSorabh Jain
 
Let's write secure Drupal code! - DrupalCamp Oslo, 2018
Let's write secure Drupal code! - DrupalCamp Oslo, 2018Let's write secure Drupal code! - DrupalCamp Oslo, 2018
Let's write secure Drupal code! - DrupalCamp Oslo, 2018BalĂĄzs TatĂĄr
 
jQuery Anti-Patterns for Performance & Compression
jQuery Anti-Patterns for Performance & CompressionjQuery Anti-Patterns for Performance & Compression
jQuery Anti-Patterns for Performance & CompressionPaul Irish
 
jQuery Essentials
jQuery EssentialsjQuery Essentials
jQuery EssentialsMarc Grabanski
 
Plugin jQuery, Design Patterns
Plugin jQuery, Design PatternsPlugin jQuery, Design Patterns
Plugin jQuery, Design PatternsRobert Casanova
 
Make your own wp cli command in 10min
Make your own wp cli command in 10minMake your own wp cli command in 10min
Make your own wp cli command in 10minIvelina Dimova
 
Curso Symfony - Clase 2
Curso Symfony - Clase 2Curso Symfony - Clase 2
Curso Symfony - Clase 2Javier Eguiluz
 

Was ist angesagt? (15)

ĐĄĐžŃŃ‚Đ”ĐŒĐ° Ń€Đ”ĐœĐŽĐ”Ń€ĐžĐœĐłĐ° ĐČ Magento
ĐĄĐžŃŃ‚Đ”ĐŒĐ° Ń€Đ”ĐœĐŽĐ”Ń€ĐžĐœĐłĐ° ĐČ MagentoĐĄĐžŃŃ‚Đ”ĐŒĐ° Ń€Đ”ĐœĐŽĐ”Ń€ĐžĐœĐłĐ° ĐČ Magento
ĐĄĐžŃŃ‚Đ”ĐŒĐ° Ń€Đ”ĐœĐŽĐ”Ń€ĐžĐœĐłĐ° ĐČ Magento
 
PHP security audits
PHP security auditsPHP security audits
PHP security audits
 
Introduzione JQuery
Introduzione JQueryIntroduzione JQuery
Introduzione JQuery
 
Maintainable JavaScript 2012
Maintainable JavaScript 2012Maintainable JavaScript 2012
Maintainable JavaScript 2012
 
jQuery in 15 minutes
jQuery in 15 minutesjQuery in 15 minutes
jQuery in 15 minutes
 
Php Tutorial | Introduction Demo | Basics
 Php Tutorial | Introduction Demo | Basics Php Tutorial | Introduction Demo | Basics
Php Tutorial | Introduction Demo | Basics
 
ć‰ç«ŻæŠ‚èż°
ć‰ç«ŻæŠ‚èż°ć‰ç«ŻæŠ‚èż°
ć‰ç«ŻæŠ‚èż°
 
jQuery Plugin Creation
jQuery Plugin CreationjQuery Plugin Creation
jQuery Plugin Creation
 
Phphacku iitd
Phphacku iitdPhphacku iitd
Phphacku iitd
 
Let's write secure Drupal code! - DrupalCamp Oslo, 2018
Let's write secure Drupal code! - DrupalCamp Oslo, 2018Let's write secure Drupal code! - DrupalCamp Oslo, 2018
Let's write secure Drupal code! - DrupalCamp Oslo, 2018
 
jQuery Anti-Patterns for Performance & Compression
jQuery Anti-Patterns for Performance & CompressionjQuery Anti-Patterns for Performance & Compression
jQuery Anti-Patterns for Performance & Compression
 
jQuery Essentials
jQuery EssentialsjQuery Essentials
jQuery Essentials
 
Plugin jQuery, Design Patterns
Plugin jQuery, Design PatternsPlugin jQuery, Design Patterns
Plugin jQuery, Design Patterns
 
Make your own wp cli command in 10min
Make your own wp cli command in 10minMake your own wp cli command in 10min
Make your own wp cli command in 10min
 
Curso Symfony - Clase 2
Curso Symfony - Clase 2Curso Symfony - Clase 2
Curso Symfony - Clase 2
 

Andere mochten auch

Telecommunication system
Telecommunication systemTelecommunication system
Telecommunication systemJamilah Abbas
 
ì„žì…˜ í•˜ìŽìžŹí‚č
ì„žì…˜ í•˜ìŽìžŹí‚čì„žì…˜ í•˜ìŽìžŹí‚č
ì„žì…˜ í•˜ìŽìžŹí‚čYu Yongwoo
 
Apache Web Server Architecture Chaitanya Kulkarni
Apache Web Server Architecture Chaitanya KulkarniApache Web Server Architecture Chaitanya Kulkarni
Apache Web Server Architecture Chaitanya Kulkarniwebhostingguy
 
Web (HTTP) request to response life cycle
Web (HTTP) request to response life cycleWeb (HTTP) request to response life cycle
Web (HTTP) request to response life cycleGopakumar Kunduveetil
 
Testing RESTful web services with REST Assured
Testing RESTful web services with REST AssuredTesting RESTful web services with REST Assured
Testing RESTful web services with REST AssuredBas Dijkstra
 
Web Cookies
Web CookiesWeb Cookies
Web Cookiesapwebco
 
Web Server Technologies I: HTTP & Getting Started
Web Server Technologies I: HTTP & Getting StartedWeb Server Technologies I: HTTP & Getting Started
Web Server Technologies I: HTTP & Getting StartedPort80 Software
 
Penetration testing
Penetration testingPenetration testing
Penetration testingAmmar WK
 
Hacking A Web Site And Secure Web Server Techniques Used
Hacking A Web Site And Secure Web Server Techniques UsedHacking A Web Site And Secure Web Server Techniques Used
Hacking A Web Site And Secure Web Server Techniques UsedSiddharth Bhattacharya
 
Sessions and cookies
Sessions and cookiesSessions and cookies
Sessions and cookieswww.netgains.org
 
Cookie and session
Cookie and sessionCookie and session
Cookie and sessionAashish Ghale
 
Cookies and sessions
Cookies and sessionsCookies and sessions
Cookies and sessionsLena Petsenchuk
 
Hacking With Nmap - Scanning Techniques
Hacking With Nmap - Scanning TechniquesHacking With Nmap - Scanning Techniques
Hacking With Nmap - Scanning Techniquesamiable_indian
 
Basics of telecommunication and networking
Basics of telecommunication and networkingBasics of telecommunication and networking
Basics of telecommunication and networkingMilan Padariya
 

Andere mochten auch (20)

Telecommunication system
Telecommunication systemTelecommunication system
Telecommunication system
 
Cmsms, open source & business model
Cmsms, open source & business modelCmsms, open source & business model
Cmsms, open source & business model
 
ì„žì…˜ í•˜ìŽìžŹí‚č
ì„žì…˜ í•˜ìŽìžŹí‚čì„žì…˜ í•˜ìŽìžŹí‚č
ì„žì…˜ í•˜ìŽìžŹí‚č
 
Apache Web Server Architecture Chaitanya Kulkarni
Apache Web Server Architecture Chaitanya KulkarniApache Web Server Architecture Chaitanya Kulkarni
Apache Web Server Architecture Chaitanya Kulkarni
 
Web (HTTP) request to response life cycle
Web (HTTP) request to response life cycleWeb (HTTP) request to response life cycle
Web (HTTP) request to response life cycle
 
Testing RESTful web services with REST Assured
Testing RESTful web services with REST AssuredTesting RESTful web services with REST Assured
Testing RESTful web services with REST Assured
 
Web Cookies
Web CookiesWeb Cookies
Web Cookies
 
Nmap scripting engine
Nmap scripting engineNmap scripting engine
Nmap scripting engine
 
Web Server Technologies I: HTTP & Getting Started
Web Server Technologies I: HTTP & Getting StartedWeb Server Technologies I: HTTP & Getting Started
Web Server Technologies I: HTTP & Getting Started
 
Smarty sharing-2
Smarty sharing-2Smarty sharing-2
Smarty sharing-2
 
Penetration testing
Penetration testingPenetration testing
Penetration testing
 
Hacking A Web Site And Secure Web Server Techniques Used
Hacking A Web Site And Secure Web Server Techniques UsedHacking A Web Site And Secure Web Server Techniques Used
Hacking A Web Site And Secure Web Server Techniques Used
 
Sessions and cookies
Sessions and cookiesSessions and cookies
Sessions and cookies
 
Cookie and session
Cookie and sessionCookie and session
Cookie and session
 
Web Server Hardening
Web Server HardeningWeb Server Hardening
Web Server Hardening
 
Mvc architecture
Mvc architectureMvc architecture
Mvc architecture
 
Cookies and sessions
Cookies and sessionsCookies and sessions
Cookies and sessions
 
Hacking With Nmap - Scanning Techniques
Hacking With Nmap - Scanning TechniquesHacking With Nmap - Scanning Techniques
Hacking With Nmap - Scanning Techniques
 
REST & RESTful Web Services
REST & RESTful Web ServicesREST & RESTful Web Services
REST & RESTful Web Services
 
Basics of telecommunication and networking
Basics of telecommunication and networkingBasics of telecommunication and networking
Basics of telecommunication and networking
 

Ähnlich wie Geek Moot '09 -- Smarty 101

Use Symfony2 components inside WordPress
Use Symfony2 components inside WordPress Use Symfony2 components inside WordPress
Use Symfony2 components inside WordPress Maurizio Pelizzone
 
Intro To Mvc Development In Php
Intro To Mvc Development In PhpIntro To Mvc Development In Php
Intro To Mvc Development In Phpfunkatron
 
PHPConf-TW 2012 # Twig
PHPConf-TW 2012 # TwigPHPConf-TW 2012 # Twig
PHPConf-TW 2012 # TwigWake Liu
 
Building Potent WordPress Websites
Building Potent WordPress WebsitesBuilding Potent WordPress Websites
Building Potent WordPress WebsitesKyle Cearley
 
Practical HTML5: Using It Today
Practical HTML5: Using It TodayPractical HTML5: Using It Today
Practical HTML5: Using It TodayDoris Chen
 
AEM Sightly Template Language
AEM Sightly Template LanguageAEM Sightly Template Language
AEM Sightly Template LanguageGabriel Walt
 
[Bristol WordPress] Supercharging WordPress Development
[Bristol WordPress] Supercharging WordPress Development[Bristol WordPress] Supercharging WordPress Development
[Bristol WordPress] Supercharging WordPress DevelopmentAdam Tomat
 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2fishwarter
 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2fishwarter
 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2fishwarter
 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2fishwarter
 
Jquery tutorial
Jquery tutorialJquery tutorial
Jquery tutorialBui Kiet
 
Introduccion a HTML5
Introduccion a HTML5Introduccion a HTML5
Introduccion a HTML5Pablo Garaizar
 
TurboGears2 Pluggable Applications
TurboGears2 Pluggable ApplicationsTurboGears2 Pluggable Applications
TurboGears2 Pluggable ApplicationsAlessandro Molina
 
Asp.net MVC - Course 2
Asp.net MVC - Course 2Asp.net MVC - Course 2
Asp.net MVC - Course 2erdemergin
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalystdwm042
 
Apostrophe (improved Paris edition)
Apostrophe (improved Paris edition)Apostrophe (improved Paris edition)
Apostrophe (improved Paris edition)tompunk
 
WordPress Theme Design and Development Workshop - Day 3
WordPress Theme Design and Development Workshop - Day 3WordPress Theme Design and Development Workshop - Day 3
WordPress Theme Design and Development Workshop - Day 3Mizanur Rahaman Mizan
 
6 introduction-php-mvc-cakephp-m6-views-slides
6 introduction-php-mvc-cakephp-m6-views-slides6 introduction-php-mvc-cakephp-m6-views-slides
6 introduction-php-mvc-cakephp-m6-views-slidesMasterCode.vn
 

Ähnlich wie Geek Moot '09 -- Smarty 101 (20)

Use Symfony2 components inside WordPress
Use Symfony2 components inside WordPress Use Symfony2 components inside WordPress
Use Symfony2 components inside WordPress
 
Intro To Mvc Development In Php
Intro To Mvc Development In PhpIntro To Mvc Development In Php
Intro To Mvc Development In Php
 
PHPConf-TW 2012 # Twig
PHPConf-TW 2012 # TwigPHPConf-TW 2012 # Twig
PHPConf-TW 2012 # Twig
 
Building Potent WordPress Websites
Building Potent WordPress WebsitesBuilding Potent WordPress Websites
Building Potent WordPress Websites
 
Practical HTML5: Using It Today
Practical HTML5: Using It TodayPractical HTML5: Using It Today
Practical HTML5: Using It Today
 
AEM Sightly Template Language
AEM Sightly Template LanguageAEM Sightly Template Language
AEM Sightly Template Language
 
[Bristol WordPress] Supercharging WordPress Development
[Bristol WordPress] Supercharging WordPress Development[Bristol WordPress] Supercharging WordPress Development
[Bristol WordPress] Supercharging WordPress Development
 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2
 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2
 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2
 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2
 
Jquery tutorial
Jquery tutorialJquery tutorial
Jquery tutorial
 
Introduccion a HTML5
Introduccion a HTML5Introduccion a HTML5
Introduccion a HTML5
 
html5
html5html5
html5
 
TurboGears2 Pluggable Applications
TurboGears2 Pluggable ApplicationsTurboGears2 Pluggable Applications
TurboGears2 Pluggable Applications
 
Asp.net MVC - Course 2
Asp.net MVC - Course 2Asp.net MVC - Course 2
Asp.net MVC - Course 2
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalyst
 
Apostrophe (improved Paris edition)
Apostrophe (improved Paris edition)Apostrophe (improved Paris edition)
Apostrophe (improved Paris edition)
 
WordPress Theme Design and Development Workshop - Day 3
WordPress Theme Design and Development Workshop - Day 3WordPress Theme Design and Development Workshop - Day 3
WordPress Theme Design and Development Workshop - Day 3
 
6 introduction-php-mvc-cakephp-m6-views-slides
6 introduction-php-mvc-cakephp-m6-views-slides6 introduction-php-mvc-cakephp-m6-views-slides
6 introduction-php-mvc-cakephp-m6-views-slides
 

KĂŒrzlich hochgeladen

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 TerraformAndrey Devyatkin
 
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 DiscoveryTrustArc
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
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 educationjfdjdjcjdnsjd
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
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 WorkerThousandEyes
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
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, ...Angeliki Cooney
 
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 connectorsNanddeep Nachan
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
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 DevelopersWSO2
 

KĂŒrzlich hochgeladen (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...
 
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
 
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
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
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
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
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
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
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, ...
 
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
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
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
 

Geek Moot '09 -- Smarty 101

  • 1. Smarty 101 What? Why? How? Ted Kulp - Shift Refresh, Inc.
  • 3. What? Arguably the most widely used PHP templating system
  • 4. What? Arguably the most widely used PHP templating system Created by Andrei Zmievski
  • 5. What? Arguably the most widely used PHP templating system Created by Andrei Zmievski Tightly integrated into the CMSMS core
  • 6. What? Arguably the most widely used PHP templating system Created by Andrei Zmievski Tightly integrated into the CMSMS core Seamlessly used on all page, module and other templates throughout the system
  • 7. What? Arguably the most widely used PHP templating system Created by Andrei Zmievski Tightly integrated into the CMSMS core Seamlessly used on all page, module and other templates throughout the system Released under the LGPL -- basically means it’s pretty liberally licensed
  • 9. Why? Separates the display logic cleanly from the controller logic
  • 10. Why? Separates the display logic cleanly from the controller logic Much safer by not allowing full range of PHP functionality in a template
  • 11. Why? Separates the display logic cleanly from the controller logic Much safer by not allowing full range of PHP functionality in a template Allows for many tricks to make complicated display easier
  • 12. Why? Separates the display logic cleanly from the controller logic Much safer by not allowing full range of PHP functionality in a template Allows for many tricks to make complicated display easier Get a lot of web-friendly functionality for free
  • 13. Why? Separates the display logic cleanly from the controller logic Much safer by not allowing full range of PHP functionality in a template Allows for many tricks to make complicated display easier Get a lot of web-friendly functionality for free And mainly...
  • 14. Why? Ugly Not So Much <html> <html> <head> <head> <title><?php echo $title; ?></title> <title>{$title}</title> <?php cms_display_stylesheet() ?> {stylesheet} <?php cms_display_metadata() ?> {metdata} </head> </head> <body> <body> <div id=”body”> <div id=”body”> <?php cms_display_content(‘content_en’) ?> {content} </div> </div> <div id=”footer”> <div id=”footer”> <?php printf(‘%m %d %Y’, cms_page_data(‘date_modiïŹed’)) ?> {modiïŹed_date|cms_date_format} </div> </div> </body> </body> </html> </html> It’s just plain easier to read
  • 17. Absolute musts Literal tags Getting available variables
  • 18. Absolute musts Literal tags Getting available variables ModiïŹers
  • 22. Literal Tags Escapes javascript Escapes CSS Really... escapes anything with { or } in it. Smarty gets confused
  • 23. Literal Tags Escapes javascript Escapes CSS Really... escapes anything with { or } in it. Smarty gets confused So literal tags basically have Smarty ignore everything between
  • 24. Literal Tags <script type="text/javascript"> / Get all of the tabs and add an onmouseover / / event listener to each tab / var tabs = getElementsByClass('tab', document.getElementById('featuresarea-tabs'),'li') ; for(i=0; i<tabs.length; i++) { ! var this_tab = ! document.getElementById(tabs[i].id); ! this_tab.onmouseover = function(){ ! ! showtab(this.id); ! ! document.getElementById(this.id).className += " selected"; ! }; }! //]]> </script>
  • 25. Literal Tags {literal} <script type="text/javascript"> / Get all of the tabs and add an onmouseover / / event listener to each tab / var tabs = getElementsByClass('tab', document.getElementById('featuresarea-tabs'),'li') ; for(i=0; i<tabs.length; i++) { ! var this_tab = ! document.getElementById(tabs[i].id); ! this_tab.onmouseover = function(){ ! ! showtab(this.id); ! ! document.getElementById(this.id).className += " selected"; ! }; }! //]]> </script> {/literal}
  • 26. The Immortal Question How do I know what variables are available to me in my template?
  • 27. Answer {get_template_vars} !!!!!!!! Gives you all the variables that are available to that template. It’s a must have. Know It Use It Love It
  • 28. {get_template_vars} On a regular page, outputs something like: SCRIPT_NAME = /1.6.x/index.php app_name = CMS sitename = CMS Made Simple Site lang = encoding = utf-8 gCms = Object cgsimple = Object content_obj = Object content_id = 69 page = get_template_vars page_id = get_template_vars page_name = get_template_vars etc.
  • 29. {get_template_vars} On a regular page, outputs something like: SCRIPT_NAME = /1.6.x/index.php app_name = CMS sitename = CMS Made Simple Site lang = encoding = utf-8 gCms = Object cgsimple = Object content_obj = Object content_id = 69 page = get_template_vars page_id = get_template_vars page_name = get_template_vars etc. Which means you can use: {$page_name} in this template and get the page’s name.
  • 31. ModiïŹers Take output and modiïŹes it directly in Smarty.
  • 32. ModiïŹers Take output and modiïŹes it directly in Smarty. Allows multiple modiïŹers to be chained.
  • 33. ModiïŹers Take output and modiïŹes it directly in Smarty. Allows multiple modiïŹers to be chained. Format: {$variable|modiïŹer_function:extra:parameters}
  • 34. ModiïŹers Take output and modiïŹes it directly in Smarty. Allows multiple modiïŹers to be chained. Format: {$variable|modiïŹer_function:extra:parameters} Chaining: {$variable|modiïŹer_function|another_one:with:params}
  • 35. ModiïŹers Take output and modiïŹes it directly in Smarty. Allows multiple modiïŹers to be chained. Format: {$variable|modiïŹer_function:extra:parameters} Chaining: {$variable|modiïŹer_function|another_one:with:params} Smarty comes with a lot of nice modiïŹers. See chapters 5 and 6 for some examples.
  • 37. Examples {$title|upper} -- Convert the string to upper case
  • 38. Examples {$title|upper} -- Convert the string to upper case {$title|truncate:40:’...’} -- Truncate the string at 40 characters and put an ellipsis on it
  • 39. Examples {$title|upper} -- Convert the string to upper case {$title|truncate:40:’...’} -- Truncate the string at 40 characters and put an ellipsis on it {$smarty.now|date_format:”%Y/%m/%d”} -- Get the current date and give it a nice formatting
  • 40. Examples {$title|upper} -- Convert the string to upper case {$title|truncate:40:’...’} -- Truncate the string at 40 characters and put an ellipsis on it {$smarty.now|date_format:”%Y/%m/%d”} -- Get the current date and give it a nice formatting {$variable|var_dump} -- Any PHP function will work
  • 42. {capture} Allows you capture output of smarty tags and variables and used it elsewhere Useful for testing if something has output data Allows you to get around issues where path of execution isn’t correct
  • 43. {capture} Example Div should only show if there is content {capture name=outp}{content block=‘sideblock’|trim}{/capture} {if $smarty.capture.outp} <div id=”sideblock”> {$smarty.capture.outp} </div> {/if}
  • 44. {cycle} Used to alternate a set of values Useful for alternating classes - ex. Alternating table rows Multiple columns
  • 45. {cycle} Example Split display of items into 2 columns {foreach from=$values item=‘the_item’} <div class=”{cycle values=”col1,col2”}”> {$the_item} </div> {/foreach}
  • 46. {escape} Encodes a variable in various formats -- ex. Encode an email address so that it’s not easily scraped by a spam bot Encode output to make sure it’s valid xhtml
  • 47. {escape} example Make a legible email address more difïŹcult to read via the source. {$user.email|escape:”hexentity”} (Converts email@domain.com to %62%64, etc.)
  • 48. Resources http://smarty.net/manual/en (Please read chapters 3 and 4 at a minimum!!!) http://wiki.cmsmadesimple.org