Re: Overloading Tags with PHP functions

From: Date: Tue, 15 Jan 2002 23:07:17 +0000
Subject: Re: Overloading Tags with PHP functions
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-77176@lists.php.net to get a copy of this message
Kristian Köhntopp wrote:
The following code is slow and very ad-hoc. Still, I'd like you to have a look at it and think about it - I'll gather reviews and opinions and probably turn it into C at some later time.
Your suggestions hits my needs to almost 100%. People say that I tend to copy your ideas. This is not true actually. About a year ago you started to work with XML/XSLT. I also looked at XML/XSLT that days, bought a couple of books and decided not to use XSLT. XSLT - nice, but.... The first time I read about XSLT in depth I was missing some callbacks during the transformation process to perform certain actions. For example I needed a <phpcode /> container to be translated into a highlight_string() function call and - in certain cases an eval(). Using XSLT to do this I would have had to do the following: XML -> XSLT -> PHP -> HTML Really complicated. Guess I want this to be translated: <phpcode eval="true" title="print example"> <php $foo = 1; print "Silly example, usually I put 20+ lines here"; ?> </phpcode> Using XSLT my first step would be producing: <?php ob_start(); $foo = 1; print "Silly example, usually I put 20+ lines here"; ob_end(); $content = ob_get_content(); $content = highlight_source($content); $content = preg_replace("#f0f0f0", "#d0d0d0", $content); print $content; ?> or maybe: <?php phpcode("..."); ?> In the next step I would be executing this PHP script to generate an HTML output. If you've ever written XSL containing if-else (xsl:choose/xsl:when) and recursive template calls you know how much it hurts to write the required XSL file. If not, here's a simple example turning XML into PHP using XSLT: http://www.ulf-wendel.de/projekte/menu/xmlmenu.php [sorry, german] (http://www.ulf-wendel.de/projekte/menu/menu3.xsl) As you can see this is a lot of almost unreadable stuff to do the job. I decided not to use XSLT, but I used XML + preg_match()/callbacks + regular HTML templates. Adding content to my homepage is really easy. All I have to do was to fill out this XML fragment. <homepage> <title>...</title> <keyords>...</keywords> <content> XHTML goes here </content> </homepage> My framework adds all I need: menu, ... So, tell me about the advantage of using XSLT for HTML generation? I can't use my favourite HTML editor, generating HTML can't be done in one step. In my case XSL was no more but overhead. Not powerful enough to serve my needs. Why the hell do I want XML and custom tags? Rasmus might shake his head asking what's my point? Rasmus, the man that can't be seen without wireless LAN and a notebook, started to add "functions" to HTML many years ago. He wrote: <html> <head>
    <title>Joke of the day, <?php print date("Y-m-d", time()); ?></title>
... Maybe the syntax was slightly different, but that's roughly how it looked like. Using templates we've turned this into: <html> <head>
    <title>Joke of the day, {TODAY}</title>
... [IT, PHPLib, ...] or: <html> <head>
    <title>Joke of the day, <{$today}></title>
[customized Smarty] What we've done is wrapping a new syntax around PHP, hiding "<?php today(); ?>". The usual argument to use templates is the separation of design and logics. But these days we put lots of logic back into our templates using Smarty (or XSL). Anyway. Generally speaking we do no more but change the syntax of PHP. Having callback functions associated with customs xml-tags is just another version of the same game: <html> <head>
    <title>Joke of the day, <today></title>
... Why should one go this way? XML is just cool: - XML is a world-wide accepted standard for information interchange - XML structures our content - XML is ease to learn - XML can be transformed (into CSV, XML, ...) using XSLT - XML documents can be validated (well-formed, DTD, Schema) - XML is plain text which can be understood by every computer in this world. These are only a _few_ arguments for XML. Having IT[X], PHPLib, Smarty - whatever - templates I can't use XSLT to transform it into CSV file to be processed by a database. But I can use XML for this. Having an XML content files I can switch from PHP to Java without many hurts. For eample I could use (properitary) Xalan XSL extensions to emulate my PHP callbacks. Using XY-PHP-Template-Engine I would have to write a parser before I can concentrate on the main issue. More important: pixeldunker have no difficulties learning new tags. Most of them know XHTML. It's easy for them to understand what's going on here: <menu url="/contact" type="tree" /> <gtext label="send" /> Not only the webmonkey likes this syntax. Look at my first example: Do you preferr: <phpcode> .... </phpcode> Or do you want to type this: <?php phpcode(" wow .... escaping all double slashes ... puuh ...."); ?> I like the first one: it's to simple. Smarty: new, old ideas Some customers are requesting full-fledged "templates" without PHP. No problem with Smarty: if, else, functions, variables, ... But this is not why I like Smarty. Smarty compiles it's templates into native PHP-scripts which makes it damn fast (if compiling smarty's code itself plays no role). Compiling is no new idea, but it was new in the context of PHP and templates. Now that we think on another "template engine" I'd like to overtake the Smarty idea. I wan't a system that can: - transform XML using PHP callbacks on-the-fly and - compile XML into PHP The role of PEAR Roxen would not be so fascinating if there would not be RXML. RXML provides a set of built-in custom "HTML" tags, like <gtext />. All Joe Dolittle needs to do to generate an image or a pie chart on the fly is to run the Roxen installer script and use the approriate RXML tags. Having function callbacks for XML tags is just half the way. Cool feature, but not very helpful for Joe (also know as "customer"). It starts to be extremly useful if there's a set of PEAR (which comes with every PHP installation) classes that can be mapped to XML tags... Conclusion Kristian and I haven't talked on this issue for about one year. He once mentioned it on the regional sailing event "Kieler Woche" when he meet Thies, no more. Independely I came to a very similar wish: custom XHTML tags without the need for XSLT. XML is my favourite: it's easy, highly portable, an industrial standard. Custom tags can be understood by almost everybody. Roxen Users know about the power of them. The success of Roxen is closely related to the availablility of built-in RXML tags. Joe Dolittle can add <gtext> to his website and gets dynamically built images. Simple. Advanced users can take the power of XML and do whatever they want with Joe's content pages. Who's gonna implement it ;) Ulf

« previous php.dev (#77176) next »