php templating systems
DESCRIPTION
TRANSCRIPT
PHP Templating Systems
April 26, 2011 NWO-PUG 1
E-mail: [email protected]: @dragonmantankIdenti.ca: dragonmantank
Who Are You and Why Are You In My House?
Chris Tankersley Doing PHP for 8 Years Lots of projects no one uses, and a
few that some do TL;DR
https://github.com/dragonmantank
April 26, 2011 NWO-PUG 2
What?
Templating Systems allow easy replacement of variable text, or to re-
use text in multiple places
April 26, 2011 NWO-PUG 3
PHP itself is actually a template system
Think about it
April 26, 2011 NWO-PUG 4
Why not just use PHP?
Not everyone wants to learn PHP Separation of Presentation and Logic
You can still use PHP, like we’ll see in a bit
April 26, 2011 NWO-PUG 5
Rolling Your OwnBecause re-inventing the bicycle is awesome
April 26, 2011 NWO-PUG 6
Search and Replace
Not as easy as it sounds Have to select a syntax How do you handle files? How does logic work?
April 26, 2011 NWO-PUG 7
Manipulate Those Strings
Template File – template.phtml
Get The File Contents
April 26, 2011 NWO-PUG 8
Manipulate Those Strings
Register some variables
Replace!
April 26, 2011 NWO-PUG 9
Congrats!
This is a template system It is just not a good template system
Inefficient Memory hog Doesn’t handle logic structures
April 26, 2011 NWO-PUG 10
Buying a BicycleBecause building one from scratch won’t be as good
April 26, 2011 NWO-PUG 11
What Do We Look For?
Easy syntax Logic Control Layered Templating Easy Integration Compilation Caching
April 26, 2011 NWO-PUG 12
Shoulders of Giants
Smarty One of the oldest templating systems
Twig Part of the symfony framework, but
standalone mustache.php
Great if you’re also doing JS templating Built-in systems in other frameworks
Not as portable most of the time
April 26, 2011 NWO-PUG 13
Smarty
April 26, 2011 NWO-PUG 14
Smarty Template
April 26, 2011 NWO-PUG 15
In PHP
April 26, 2011 NWO-PUG 16
Why Use Smarty?
Lots of people know it Supports Caching of templates Variable creation in the template
PHP has no idea what {$counter} is Markup is simple enough for
designers
April 26, 2011 NWO-PUG 17
Twig
April 26, 2011 NWO-PUG 18
Twig Template
April 26, 2011 NWO-PUG 19
In PHP
April 26, 2011 NWO-PUG 20
Why Use Twig?
More modern than Smarty Gateway drug into symfony
development
April 26, 2011 NWO-PUG 21
{{ mustache.php }}
April 26, 2011 NWO-PUG 22
Template File
April 26, 2011 NWO-PUG 23
In PHP
April 26, 2011 NWO-PUG 24
Why use mustache?
Cross-language Supports JS, PHP, Ruby, Python
Syntax is easy IDE Integration
April 26, 2011 NWO-PUG 25
Frameworks
April 26, 2011 NWO-PUG 26
Have to Output HTML Somehow
Zend Framework has Zend_View Symfony has Twig Lithium, CakePHP, Fuel all have one Most of the time these work great
April 26, 2011 NWO-PUG 27
Be Careful
Most of the framework templating systems are not portable (except Twig)
Most of the time hard to swap them out for something else
April 26, 2011 NWO-PUG 28
So what do we gain?Other than more hard drive space used
April 26, 2011 NWO-PUG 29
A few things…
Presentation and Business Logic are separated Only logic in the template is for output
We gained output caching We gained the ability to have non-
programmers work on our designs In the case of Twig/Smarty, there is
now a portable output renderer
April 26, 2011 NWO-PUG 30
Questions?
April 26, 2011 NWO-PUG 31
LinksWhere to Find Stuff I Talked About
April 26, 2011 NWO-PUG 32
Links
Smarty www.smarty.net
Twig www.twig-project.org
mustache.php https://github.com/bobthecow/
mustache.php
April 26, 2011 NWO-PUG 33