php templating systems

33
PHP Templating Systems April 26, 2011 NWO-PUG 1 E-mail: [email protected] Twitter: @dragonmanta Identi.ca: dragonmanta

Upload: chris-tankersley

Post on 18-Nov-2014

4.581 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: PHP Templating Systems

PHP Templating Systems

April 26, 2011 NWO-PUG 1

E-mail: [email protected]: @dragonmantankIdenti.ca: dragonmantank

Page 2: PHP Templating Systems

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

Page 3: PHP Templating Systems

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

Page 4: PHP Templating Systems

Think about it

April 26, 2011 NWO-PUG 4

Page 5: PHP Templating Systems

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

Page 6: PHP Templating Systems

Rolling Your OwnBecause re-inventing the bicycle is awesome

April 26, 2011 NWO-PUG 6

Page 7: PHP Templating Systems

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

Page 8: PHP Templating Systems

Manipulate Those Strings

Template File – template.phtml

Get The File Contents

April 26, 2011 NWO-PUG 8

Page 9: PHP Templating Systems

Manipulate Those Strings

Register some variables

Replace!

April 26, 2011 NWO-PUG 9

Page 10: PHP Templating Systems

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

Page 11: PHP Templating Systems

Buying a BicycleBecause building one from scratch won’t be as good

April 26, 2011 NWO-PUG 11

Page 12: PHP Templating Systems

What Do We Look For?

Easy syntax Logic Control Layered Templating Easy Integration Compilation Caching

April 26, 2011 NWO-PUG 12

Page 13: PHP Templating Systems

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

Page 14: PHP Templating Systems

Smarty

April 26, 2011 NWO-PUG 14

Page 15: PHP Templating Systems

Smarty Template

April 26, 2011 NWO-PUG 15

Page 16: PHP Templating Systems

In PHP

April 26, 2011 NWO-PUG 16

Page 17: PHP Templating Systems

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

Page 18: PHP Templating Systems

Twig

April 26, 2011 NWO-PUG 18

Page 19: PHP Templating Systems

Twig Template

April 26, 2011 NWO-PUG 19

Page 20: PHP Templating Systems

In PHP

April 26, 2011 NWO-PUG 20

Page 21: PHP Templating Systems

Why Use Twig?

More modern than Smarty Gateway drug into symfony

development

April 26, 2011 NWO-PUG 21

Page 22: PHP Templating Systems

{{ mustache.php }}

April 26, 2011 NWO-PUG 22

Page 23: PHP Templating Systems

Template File

April 26, 2011 NWO-PUG 23

Page 24: PHP Templating Systems

In PHP

April 26, 2011 NWO-PUG 24

Page 25: PHP Templating Systems

Why use mustache?

Cross-language Supports JS, PHP, Ruby, Python

Syntax is easy IDE Integration

April 26, 2011 NWO-PUG 25

Page 26: PHP Templating Systems

Frameworks

April 26, 2011 NWO-PUG 26

Page 27: PHP Templating Systems

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

Page 28: PHP Templating Systems

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

Page 29: PHP Templating Systems

So what do we gain?Other than more hard drive space used

April 26, 2011 NWO-PUG 29

Page 30: PHP Templating Systems

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

Page 31: PHP Templating Systems

Questions?

April 26, 2011 NWO-PUG 31

Page 32: PHP Templating Systems

LinksWhere to Find Stuff I Talked About

April 26, 2011 NWO-PUG 32

Page 33: PHP Templating Systems

Links

Smarty www.smarty.net

Twig www.twig-project.org

mustache.php https://github.com/bobthecow/

mustache.php

April 26, 2011 NWO-PUG 33