Custom XML templating with PHP
4th July 2003
Dynamic XML conversion using the SAX parser and a stack is the best new article I’ve seen on PHPBuilder in a very long time. It introduces several interesting ideas. The first is something I’ve been thinking about for a while now: using PHP’s output buffering to implement a kind of templating system so that all of the scripts in a system just have to generate an intermediate content type, then the output buffering function adds on the HTML framework and finalises the page. The second idea is ingenious: invent XML elements to represent specific behaviours, then handle them with a SAX before the page is displayed. Here’s some example code from the article:
<doc title="Pizza menu" bgcolor="lightblue"> <bigheadline> Pizza Palace - Our Menu for <dayofweek /> </bigheadline> <br /><br /> <b>Buon appetito!!!</b> <br /><br /> <nicebox bordercolor="green"> <product id="0" /><br /> <product id="1" /><br /> <product id="2" /><br /> <product id="3" /><br /> <product id="4" /><br /> </nicebox> </doc>
The tutorial describes a full implementation to make use of the custom tags demonstrated above. It’s advocating a full CMS where pages are described using a custom XML format, but the idea could be used in a whole load of different ways. I touched on this approach with my Form Validation prototype but this really shows how much further the concept can be taken. Best of all, the performance hit should be pretty minimal due to the absurd speed of PHP’s XML functions.
More recent articles
- Talking Large Language Models with Rooftop Ruby - 29th September 2023
- Weeknotes: Embeddings, more embeddings and Datasette Cloud - 17th September 2023
- Build an image search engine with llm-clip, chat with models with llm chat - 12th September 2023
- LLM now provides tools for working with embeddings - 4th September 2023
- Datasette 1.0a4 and 1.0a5, plus weeknotes - 30th August 2023
- Making Large Language Models work for you - 27th August 2023
- Datasette Cloud, Datasette 1.0a3, llm-mlc and more - 16th August 2023
- How I make annotated presentations - 6th August 2023
- Weeknotes: Plugins for LLM, sqlite-utils and Datasette - 5th August 2023
- Catching up on the weird world of LLMs - 3rd August 2023