Perch CMS Template Engine

A presentation at RDG Geek Night in September 2013 in Reading, UK by Drew McLellan

Slide 1

Slide 1

-HOW WE WROTE A- TEMPLATE ENGINE Drew McLellan Perch CMS grabaperch.com RDG Geek Night 10th September 2013

Slide 2

Slide 2

L I G H T W E I G H T PHP CMS FOR SMALL WEBSITES

Slide 3

Slide 3

WE NEEDED A REALLY GOOD TEMPLATE LANGUAGE

Slide 4

Slide 4

-SO- WHAT WAS ALREADY OUT THERE?

Slide 5

Slide 5

SMARTY (NOT SO SMART)

Slide 6

Slide 6

TWIG (NOT SO MATURE)

Slide 7

Slide 7

JUST PHP -WITH A- DIFFERENT SYNTAX

Slide 8

Slide 8

!<perch:content !!!type=”text” !!!id=”heading” !!!label=”Main heading” !!!required=”true” />

Slide 9

Slide 9

<h2><perch:content id="heading" type="text" label="Heading" required="true" title="true" /></h2> <p class="date"><perch:content id="date" type="date" label="Date" format="%d %B %Y" /></p> <perch:content id="body" type="textarea" label="Body" textile="true" editor="markitup" required="true" />

Slide 10

Slide 10

HTML IS LIKE XML RIGHT?

Slide 11

Slide 11

<img src="<perch:content id="photo" type="image" label="Photo" />" />

Slide 12

Slide 12

xkcd.com/208

Slide 13

Slide 13

stackoverflow.com/questions/1732348/

Slide 14

Slide 14

! <perch:content type=”text” ! ! id=”heading” label=”Main heading” ! ! required=”true” /> #perch:content[^]*/>#

Slide 15

Slide 15

$Tag = new PerchXMLTag( '<perch:content id="foo" bar="baz" />'); echo $Tag->bar; // baz

Slide 16

Slide 16

#([a-z-])="([^"])"#

Slide 17

Slide 17

#([a-z-]+)=["]([^"\ \](?:\\.[^"\\ ])*)["]#

Slide 18

Slide 18

-WAIT- REGULAR EXPRESSIONS ARE SLOW, RIGHT?

Slide 19

Slide 19

REGEX ARE SLOW UNLESS YOU NEED THEM

Slide 20

Slide 20

! <perch:if exists=”foo”> ! ! ... loadsamarkup ... ! </perch:if> #(perch:if[^]>)(((?! perch:if).))</perch:if>#

Slide 21

Slide 21

-WE NEEDED- A NEW WAY -TO PARSE- TAG PAIRS

Slide 22

Slide 22

<perch:if exists=”a”> ! <perch:if exists=”b”> ! ! <perch:if exists=”c”> ! ! ! Hello, world. ! ! </perch:if> ! </perch:if> <perch:else /> ! <perch:if exists=”d”> ! ! Goodbye, cruel world. ! </perch:if> </perch:if>

Slide 23

Slide 23

<perch:if exists=”a”> ! <perch:if exists=”b”> ! ! <perch:if exists=”c”> ! ! ! Hello, world. ! ! </perch:if> ! </perch:if> <perch:else /> ! <perch:if exists=”d”> ! ! Goodbye, cruel world. ! </perch:if> </perch:if>

Slide 24

Slide 24

<perch:if exists=”a”> ! <perch:if exists=”b”> ! ! <perch:if exists=”c”> ! ! ! Hello, world. ! ! </perch:if> ! </perch:if> <perch:else /> ! <perch:if exists=”d”> ! ! Goodbye, cruel world. ! </perch:if> </perch:if>

Slide 25

Slide 25

<perch:if exists=”a”> ! <perch:if exists=”b”> ! ! <perch:if exists=”c”> ! ! ! Hello, world. ! ! </perch:if> ! </perch:if> <perch:else /> ! <perch:if exists=”d”> ! ! Goodbye, cruel world. ! </perch:if> </perch:if>

Slide 26

Slide 26

<perch:if exists=”a”> ! <perch:if exists=”b”> ! ! <perch:if exists=”c”> ! ! ! Hello, world. ! ! </perch:if> ! </perch:if> <perch:else /> ! <perch:if exists=”d”> ! ! Goodbye, cruel world. ! </perch:if> </perch:if>

Slide 27

Slide 27

<perch:if exists=”a”> ! <perch:if exists=”b”> ! ! <perch:if exists=”c”> ! ! ! Hello, world. ! ! </perch:if> ! </perch:if> <perch:else /> ! <perch:if exists=”d”> ! ! Goodbye, cruel world. ! </perch:if> </perch:if>

Slide 28

Slide 28

<perch:if exists=”a”> ! <perch:if exists=”b”> ! ! ! ! ! Hello, world. ! ! ! </perch:if> <perch:else /> ! <perch:if exists=”d”> ! ! Goodbye, cruel world. ! </perch:if> </perch:if>

Slide 29

Slide 29

<perch:if exists=”a”> ! <perch:if exists=”b”> ! ! ! ! ! Hello, world. ! ! ! </perch:if> <perch:else /> ! <perch:if exists=”d”> ! ! Goodbye, cruel world. ! </perch:if> </perch:if>

Slide 30

Slide 30

CLOSING TAGS -ARE - EASY

Slide 31

Slide 31

NO MORE WINDOWS CRASHES

Slide 32

Slide 32

MAYBE - WE WILL - REWRITE IT

Slide 33

Slide 33

THANK YOU Perch is at grabaperch.com On most things, I’ am @drewm