Page 1 of 1

Creating DOM Template Engine - Problems, Ideas, Examples

Posted: Fri Jul 20, 2007 7:27 pm
by lpetrov
Here is an interesting article about the current template engines and what is the real meaning of "a good template engine":
http://blog.axisvista.com/index.php/200 ... ate-engine

Note: I changed the subject of the post, 10x to superdezign

Posted: Fri Jul 20, 2007 7:34 pm
by feyd
I only see one template engine mentioned. There are several more...

Two if you want to count the glancing pass of Smarty.

... or is this really an opportunity to sell "Spidex"?

Posted: Fri Jul 20, 2007 7:44 pm
by lpetrov
Argh, no...
1st of all this is ONLY a proof of concept (the Spidex class). It will never been sold, becouse it will be used in our framework.
The framework will not be sold too.

About the "only one" template engine that is in the article, yes that is only "Smarty", becouce it has the most developers.
There are several other template engines that are str_replace/eregi_replace(simple one) based, but that are NOT template engines.

If Spidex (as a POC) had a caching mechanism it is a real demonstration of a template engine that is better then PHP (php is already a template engine <- google this for more info). That is becouce it caches the HTML code, that doesnt require any other parsing like Smarty does.

There are some other template engines, but in most cases they arent so friendly as Smarty is (the syntax I mean).
Maybe one of the most I like was the HTML_Template_Flexy, but it isnt so flexible on implementing custom tags/namespaces, becouse it custom tags cant read attributes containing {vars}, also it isnt supported (last update was 11-2006).

Other XML-like template is patTemplate, but there is a problem that it cant support dynamic tag(namespace) handlers or $variable passing thru <ns:tag> attributes and that means that the code for the template will have alot of:

Code: Select all

<pat:variable name="testing>
That is a bit, long compared to PHP's <?=$testing?> or Smarty's {var..}

In few words...are there any other REAL template engines ? :)

Posted: Fri Jul 20, 2007 8:50 pm
by feyd
I used "sell" in terms of getting outside developers to use it, not in the money sense. Sorry.

Posted: Fri Jul 20, 2007 8:53 pm
by lpetrov
feyd wrote:I used "sell" in terms of getting outside developers to use it, not in the money sense. Sorry.
Like i said, this is Proof of concept, it maybe will not be supported with coding anymore, becouse it will be integrated in a private platform (framework). So in few words...it is an example of "Template Engine" (not string replacer like the current template engines are doing).

The idea of getting popularity of this post is to make a real discussion about php template engines (not the ones calling them self like that);

Posted: Sat Jul 21, 2007 4:35 am
by The Phoenix
lpetrov wrote:The idea of getting popularity of this post is to make a real discussion about php template engines (not the ones calling them self like that);
Well, thats the difficult part. The article does more to promote your choice in templating techniques than it does to compare existing php template engines.

But ignoring that issue for a moment, and focusing on your call for discussion, many people would argue with your definitions.

Your opinion that a string replacer isn't a template engine makes me wonder: How do you define a template engine?

You are using placeholders, those placeholders are replaced, and the output is presented. How is that different from a (complicated) string replacer?

The article makes many claims that are to be polite, very arguable:

- Allow php style tags, but be WYSIWIG friendly (Is there a WYSIWIG web editor that doesn't choke on php tags?)
- SimpleXML is not well documented - Google "Simplexml tutorial" - tons of documentation. Not to mention the php manual page for it is one of the largest and most commented on pages in the manual.
- Smarty has a template->php code conversion and that maybe is fast, but it isnt - But it can power parts of yahoo? Benchmarks? Do you use an accelerator? Its used on *thousands* of sites that run very fast.
- Doesn't your example use {} tags after you said you didn't want those?

I think the inconsistencies make it hard to understand what your intent was, and you make a number of claims that by themselves are hard to deal with. All together, its just confusing.

Posted: Sat Jul 21, 2007 5:11 am
by lpetrov
The Phoenix wrote: 1. Allow php style tags, but be WYSIWIG friendly (Is there a WYSIWIG web editor that doesn't choke on php tags?)
2. SimpleXML is not well documented - Google "Simplexml tutorial" - tons of documentation. Not to mention the php manual page for it is one of the largest and most commented on pages in the manual.
3. Smarty has a template->php code conversion and that maybe is fast, but it isnt - But it can power parts of yahoo? Benchmarks? Do you use an accelerator? Its used on *thousands* of sites that run very fast.
4. Doesn't your example use {} tags after you said you didn't want those?
So lets make exmplain a little:

1. PHP Style tags are good (for us developers), but for designers..most of them likes some IDEs that automates the coding process. This is why I needed WYSIWYG for the developers to help on core tasks in the templates an also <XML> tags, so I can integrate some TagEditor in our IDE for the Designers.

2. SimpleXml is documented, but is it simple, doing more complex operations requires importing it to DOM. Why to jungle between SimpleXml and DOM where we can use DOM ? (in few words, for my requirements.... DOM isthe one!)

3. A little question, what is faster:
- to do a file_get_contents("cachedTemplate.html");
- or to do an include("cachedFile.php")
? :)

4. My personaly..yes i hate them, but it seems that the designers loves them so the {} are also a part of the requirements.

Posted: Sat Jul 21, 2007 5:26 am
by The Phoenix
lpetrov wrote: 1. PHP Style tags are good (for us developers), but for designers..most of them likes some IDEs that automates the coding process. This is why I needed WYSIWYG for the developers to help on core tasks in the templates an also <XML> tags, so I can integrate some TagEditor in our IDE for the Designers.
Are there any WYSIWIG editors that support php tags? To my knowledge, they all choke on them. As a result, this requirement isn't realistic.

Put another way, what WYSIWIG editor are you using that lets you use php tags, xml tags AND {} tags?
lpetrov wrote:2. SimpleXml is documented, but is it simple, doing more complex operations requires importing it to DOM. Why to jungle between SimpleXml and DOM where we can use DOM ? (in few words, for my requirements.... DOM isthe one!)
Interesting. Can you cite an example of something you haven't been able to do in SimpleXML, but can in DOM?
lpetrov wrote:3. A little question, what is faster:
- to do a file_get_contents("cachedTemplate.html");
- or to do an include("cachedFile.php")
? :)
I asked first: Do you use an accelerator?
lpetrov wrote:4. My personaly..yes i hate them, but it seems that the designers loves them so the {} are also a part of the requirements.
That then makes Smarty a template engine by your definition, and one that meets most of your other requirements.

Posted: Sat Jul 21, 2007 7:59 am
by lpetrov
1. We use our own developed IDE. The PHP tags are parsed like they are in Dreamweaver, but that is not designers-friendly, but they will not be used at the templates. This is just an option. XML and Smarty tags are parsed as normal tags with an extra like 'generic properties-designer' tool like in Dreamweaver. This is useful.

2. There are alot of thinks that cant be done in SimpleXML examples can be found at: http://bg2.php.net/simplexml

3. Do I use an accelerator, no currently no, but enabling an accelerator will optimise the code of the POC alot.

4. My definition ? No, Smarty doesnt met the requirements.

Posted: Sat Jul 21, 2007 9:32 am
by superdezign
The article, to me, seems more like you were just in the mood to vent and share your development experience, and nothing about the real meaning of a good template engine. You just explain the things you felt were necessary and how you implemented them.

And what examples are you referring to in your SimpleXML link?

Posted: Sat Jul 21, 2007 9:47 am
by lpetrov
I was refering to:

Code: Select all

<?php
/**
* Pumps all child elements of second SimpleXML object into first one.
* 
* @param    object      $xml1   SimpleXML object
* @param    object      $xml2   SimpleXML object
* @return   void
*/
function simplexml_merge (SimpleXMLElement &$xml1, SimpleXMLElement $xml2)
{
    // convert SimpleXML objects into DOM ones
    $dom1 = new DomDocument();
    $dom2 = new DomDocument();
    $dom1->loadXML($xml1->asXML());
    $dom2->loadXML($xml2->asXML());

    // pull all child elements of second XML
    $xpath = new domXPath($dom2);
    $xpathQuery = $xpath->query('/*/*');
    for ($i = 0; $i < $xpathQuery->length; $i++)
    {
        // and pump them into first one
        $dom1->documentElement->appendChild(
            $dom1->importNode($xpathQuery->item($i), true));
    }
    $xml1 = simplexml_import_dom($dom1);
}

$xml1 = simplexml_load_string('<root><child>child 1</child></root>');
$xml2 = simplexml_load_string('<root><child>child 2</child></root>');
simplexml_merge($xml1, $xml2);
echo($xml1->asXml());
?>
So in few words, i just wanned to show to The Phoenix, that this cant be done using only SimpleXML.

About the article, yes maybe its more likely and try to share our development experience :)