Date post: | 27-Nov-2014 |
Category: |
Technology |
Upload: | thomas-weinert |
View: | 1,580 times |
Download: | 1 times |
FrOSCon
Bastian Feder, Thomas Weinert"The Lumber Mill"
Using XSLT For Your Templates
About Us● Application Developer
– PHP– XSLT/XPath– (some) JavaScript
● papaya CMS– PHP based Content Management System– uses XSLT for 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?● ...?
Template Systems● Bind variables● Layout logic
– Placeholders– Conditions– Loops– Callbacks
● Formatting● Data loading
XSL● XML
– Semantic variable tree● XPath
– Query language for XML trees● XSLT
– Transforming XML● Conditions● Loops● …
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
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
Template call/apply● <xsl:call-template/>
– Call templates by name● <xsl:apply-templates/>
– Template matching– Most exact pattern wins
● Current node● Parameters● Modes
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
Collecting Output
Main (XML)
Part 1 (HTML)
Part 2 (XML)Part 1 (XML)
Part 2 (HTML)
Page (XML)
Page (HTML)
Caching Output
Main (XML)
Part 1 (HTML)
Part 2 (XML)Part 1 (XML)
Part 2 (HTML)
Page (XML)
Page (HTML)
Cache
Embedding Output● Escaping and CDATA-Sections
– CDATA is for readability and whitespaces● <xsl:copy-of select=“...“/>● disable-output-escaping=“yes“● Dangers
– HTML Injection
Import templates● Generic● Reusable● Encapsulation● Inheritance
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
XML Data Files● Wellformed XML● XPath: document()● URI
– Single loading
Language Templates● Phrases
– "layout texts"● Numbers● Date and Time
EXSLT● Community initiative to provide extensions to
XSLT– Common– Functions– Math, Random– Strings, Regular Expressions– Dates and Times– Sets– Dynamic
EXSLT: functions module● Make templates to functions
– func:function– func:result
● Namespace required– func– Own
PHP● Streamwrapper
– return XML data for document()● Callbacks
– call php source
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!
Callbacks● Register PHP functions for xsl
– registerPHPFunctions($restrict)– php::function('function', parameter, ...)– php::functionString('function', ...)
RTF Problem● Result Tree Fragment
– XML created in XSL– No CDATA– No node set
● XSLT 2.0● EXSLT
– node-set()
Other Templates● Repeat String● JavaScript Escaping
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
Administration
● Single template● About 25% saved
Output
● Several template● About 90% saved
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/