The Lumber Mill - XSLT For Your Templates

27
FrOSCon Bastian Feder, Thomas Weinert "The Lumber Mill" Using XSLT For Your Templates

description

Ideas and Solutions for XSLT Templating, FrOSCon 2009

Transcript of The Lumber Mill - XSLT For Your Templates

Page 1: The Lumber Mill  - XSLT For Your Templates

FrOSCon

Bastian Feder, Thomas Weinert"The Lumber Mill"

Using XSLT For Your Templates

Page 2: The Lumber Mill  - XSLT For Your Templates

About Us● Application Developer

– PHP– XSLT/XPath– (some) JavaScript

● papaya CMS– PHP based Content Management System– uses XSLT for Templates

Page 3: The Lumber Mill  - XSLT For Your Templates

About You● Who are you?● Do you use XSLT?● How often do you use XSLT?● Do you use other template systems/languages?● Smarty?● TypoScript?● ...?

Page 4: The Lumber Mill  - XSLT For Your Templates

Template Systems● Bind variables● Layout logic

– Placeholders– Conditions– Loops– Callbacks

● Formatting● Data loading

Page 5: The Lumber Mill  - XSLT For Your Templates

XSL● XML

– Semantic variable tree● XPath

– Query language for XML trees● XSLT

– Transforming XML● Conditions● Loops● …

Page 6: The Lumber Mill  - XSLT For Your Templates

Comparison● PHP based

– Individual syntax– Individual tools– Individual

documentation– Different flavors

for different purpose

– Easy Syntax?

● XSLT– Standardized

syntax– Free and

commercial tools– Documentation of

the standard and the implementations

– One to rule them all

Page 7: The Lumber Mill  - XSLT For Your Templates

Nodes

Elements Text nodes Attributes* text() @*

node()

● texts/text[@ident='foo']/*● texts/text[@ident='foo']/text()● texts/text[@ident='foo']/node()● texts/text[@ident='foo']/@value

Page 8: The Lumber Mill  - XSLT For Your Templates

Template call/apply● <xsl:call-template/>

– Call templates by name● <xsl:apply-templates/>

– Template matching– Most exact pattern wins

● Current node● Parameters● Modes

Page 9: The Lumber Mill  - XSLT For Your Templates

Static Templates Files● Template files should not be writable by the

web server– Security– Tools

● Template files should not be generated from PHP

– Same reasons– Context

Page 10: The Lumber Mill  - XSLT For Your Templates

Collecting Output

Main (XML)

Part 1 (HTML)

Part 2 (XML)Part 1 (XML)

Part 2 (HTML)

Page (XML)

Page (HTML)

Page 11: The Lumber Mill  - XSLT For Your Templates

Caching Output

Main (XML)

Part 1 (HTML)

Part 2 (XML)Part 1 (XML)

Part 2 (HTML)

Page (XML)

Page (HTML)

Cache

Page 12: The Lumber Mill  - XSLT For Your Templates

Embedding Output● Escaping and CDATA-Sections

– CDATA is for readability and whitespaces● <xsl:copy-of select=“...“/>● disable-output-escaping=“yes“● Dangers

– HTML Injection

Page 13: The Lumber Mill  - XSLT For Your Templates

Import templates● Generic● Reusable● Encapsulation● Inheritance

Page 14: The Lumber Mill  - XSLT For Your Templates

Multiple Columns Template● Group items

– Calculate positions● Call item group templates● Call item generic template● Use itemType parameter to identify item

template● Call type specific template for item

Page 15: The Lumber Mill  - XSLT For Your Templates

XML Data Files● Wellformed XML● XPath: document()● URI

– Single loading

Page 16: The Lumber Mill  - XSLT For Your Templates

Language Templates● Phrases

– "layout texts"● Numbers● Date and Time

Page 17: The Lumber Mill  - XSLT For Your Templates

EXSLT● Community initiative to provide extensions to

XSLT– Common– Functions– Math, Random– Strings, Regular Expressions– Dates and Times– Sets– Dynamic

Page 18: The Lumber Mill  - XSLT For Your Templates

EXSLT: functions module● Make templates to functions

– func:function– func:result

● Namespace required– func– Own

Page 19: The Lumber Mill  - XSLT For Your Templates

PHP● Streamwrapper

– return XML data for document()● Callbacks

– call php source

Page 20: The Lumber Mill  - XSLT For Your Templates

Streamwrapper● PHP file functions use stream wrappers

– Internal: http, ftp, phar, …● Register own stream wrappers

– PHP class– Basic functions

● stream_open(), stream_read(), stream_tell(), stream_seek(), stream_eof(),

– url_stat()● No php source!

Page 21: The Lumber Mill  - XSLT For Your Templates

Callbacks● Register PHP functions for xsl

– registerPHPFunctions($restrict)– php::function('function', parameter, ...)– php::functionString('function', ...)

Page 22: The Lumber Mill  - XSLT For Your Templates

RTF Problem● Result Tree Fragment

– XML created in XSL– No CDATA– No node set

● XSLT 2.0● EXSLT

– node-set()

Page 23: The Lumber Mill  - XSLT For Your Templates

Other Templates● Repeat String● JavaScript Escaping

Page 24: The Lumber Mill  - XSLT For Your Templates

ext/xslcache● Process cache● Performance increase● Still evaluating

– Only some checks in php requests● Only static XSLT● Same library like ext/xsl

– eXSLT– PHP callbacks

Page 25: The Lumber Mill  - XSLT For Your Templates

Administration

● Single template● About 25% saved

Page 26: The Lumber Mill  - XSLT For Your Templates

Output

● Several template● About 90% saved

Page 27: The Lumber Mill  - XSLT For Your Templates

Links● http://www.a-basketful-of-papayas.net/

● http://www.w3.org/TR/xpath● http://www.w3.org/TR/xslt● http://www.dpawson.co.uk/xsl/sect2/sect21.html

● http://code.nytimes.com/projects/xslcache/