Object-oriented template engine

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
Domovoy
Forum Newbie
Posts: 5
Joined: Wed Nov 24, 2004 8:43 am

Object-oriented template engine

Post by Domovoy »

Does anyone know a template engine that meets the following requirements?

1. It should compile templates to php-files

2. It should be object-oriented

3. It should work it PHP5

4. It should have simple templates - something like the following:



Code: Select all

<TR collection="products" class="ProductsList"> 
<TD id="firstName">Vasja</TD><TD id="lastName">Pupkin</TD> 
</TR>
that should be compiled by the template engine to the following:

Code: Select all

<?php 
foreach ($page->getProducts() as $item0001) { 
?> 
<TR class="ProductsList"> 
<TD><?=$item0001->getFirstName();?></TD><TD><?=$item0001->getLastName();?> 
</TR> 
<? 
} 
?>
It the moment I think that Flexy is the closest one. Am I wrong?
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

There a million of them out there....

Template Power
Smarty
Yapter

To name just a couple.
Domovoy
Forum Newbie
Posts: 5
Joined: Wed Nov 24, 2004 8:43 am

Post by Domovoy »

[quote="neophyte"]There a million of them out there....

Template Power
Smarty
Yapter

To name just a couple.[/quote]


I don't know "Template Power" and "Yapter", but "Smarty":
- is not "object-oriented" (try to work with objects instead of regular variables)
- is not a PHP5 template engine (should be modified to work with PHP5)
- has a little bit complex templates (what is the difference between Smarty tags and PHP tags? It is almost the same)

So, I think that it is easier to teach designers the PHP language instead of Smarty. At least there will be no the compilation step.

I will take a look at "Template Power" and "Yapter". Thank you.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Not sure about php5 support but there's also WACT - which comes with unit tests.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

Domovoy wrote:"Smarty":
- is not "object-oriented" (try to work with objects instead of regular variables)
Lets get more specific - what do you want to do with the objects? Accessing them is easy as pie. ( http://smarty.php.net/manual/en/languag ... bjects.php )
Domovoy wrote: - is not a PHP5 template engine (should be modified to work with PHP5)
I have it running under PHP5 just fine - what problems do you have with it on PHP5? (Again, please be more specific)
Domovoy wrote: - has a little bit complex templates (what is the difference between Smarty tags and PHP tags? It is almost the same)
Smarty *can* be complex - or it can be very very simple: Its entirely up to the implementor as to what they need.

In some cases, its simple replacement (so whats the difference between that and str_replace). In other cases, its a fairly simple loop (so whats the difference between that and just hardcoding html in the loop), and in yet others, its a fairly complex combination of loops, if/else and other logic handlers.

In all three cases, the difference is simple: You seperate out OUTPUT from PROCESSING. Its an architectural difference that can make a huge impact on maintainability, debugging, and in some cases, even in optimization.
Domovoy wrote: So, I think that it is easier to teach designers the PHP language instead of Smarty. At least there will be no the compilation step.
On the contrary - at its simplest, smarty is a very simple thing - just variable replacement. At its most complex, it doesnt even come *close* to PHP.

As to the compilation step - that happens behind the scenes, and is easy to redo and undo.. I think you are mistaking it as being similar to compiling C or other languages - its not.

I think you may have some misunderstandings about Smarty that could be fixed with some more experience or discussion.
Domovoy
Forum Newbie
Posts: 5
Joined: Wed Nov 24, 2004 8:43 am

Post by Domovoy »

Roja wrote:
Domovoy wrote:"Smarty":
- is not "object-oriented" (try to work with objects instead of regular variables)
Lets get more specific - what do you want to do with the objects? Accessing them is easy as pie. ( http://smarty.php.net/manual/en/languag ... bjects.php )
How about the following?

Code: Select all

&#123;section name=user loop=$users&#125;
&lt;TR&gt;
&lt;TD&gt;Membership: &#123;$users&#1111;user]-&gt;membership-&gt;name&#125;&lt;/TD&gt;
&lt;TD&gt;Registered: &#123;$users&#1111;user]-&gt;membership-&gt;date&#125;&lt;/TD&gt;
...
&lt;/TR&gt;
&#123;/section&#125;
So, you have to write the full path variables in Smarty.

Now see the following:

Code: Select all

&lt;TR collection="users"&gt;
&lt;SPAN scope="membership"&gt;
&lt;TD id="name"&gt;Some name&lt;/TD&gt;
&lt;TD id="date"&gt;Some date&lt;/TD&gt;
....
&lt;/SPAN&gt;
&lt;/TR&gt;
As you see, the second example (a template engine I'm looking for) is easier for understanding and modifying. Moreover, you can open the second code in a browser.

Roja wrote:
Domovoy wrote: - is not a PHP5 template engine (should be modified to work with PHP5)
I have it running under PHP5 just fine - what problems do you have with it on PHP5? (Again, please be more specific)
Just enable "E_STRICT" option in php.ini. Smarty isn't designed for PHP5 for now.

Roja wrote:
Domovoy wrote: - has a little bit complex templates (what is the difference between Smarty tags and PHP tags? It is almost the same)
Smarty *can* be complex - or it can be very very simple: Its entirely up to the implementor as to what they need.

In some cases, its simple replacement (so whats the difference between that and str_replace). In other cases, its a fairly simple loop (so whats the difference between that and just hardcoding html in the loop), and in yet others, its a fairly complex combination of loops, if/else and other logic handlers.

In all three cases, the difference is simple: You seperate out OUTPUT from PROCESSING. Its an architectural difference that can make a huge impact on maintainability, debugging, and in some cases, even in optimization.
I can do it in PHP:

Code: Select all

<?php
/*
* PROCESSING
*/
$db = new MySQL();
$db->connect(.......);
$users = $db->getUsers(.......);
?>


<!-- OUTPUT -->
<HTML>
<HEAD>
...
</HEAD>
<BODY>
...
<? foreach($users as $user): ?>
<TR>
<TD>Name:</TD><TD><?=$user['name']?></TD>
</TR>
<TR>
<TD>Login:</TD><TD><?=$user['login']?></TD>
</TR>
<? endfor; ?>
<BODY>
</HTML>
And what is the difference in the example above? Why I should use Smarty?

Roja wrote:
Domovoy wrote: So, I think that it is easier to teach designers the PHP language instead of Smarty. At least there will be no the compilation step.
On the contrary - at its simplest, smarty is a very simple thing - just variable replacement. At its most complex, it doesnt even come *close* to PHP.

As to the compilation step - that happens behind the scenes, and is easy to redo and undo.. I think you are mistaking it as being similar to compiling C or other languages - its not.
No, I meant the "behind the scenes" compilation (i.e. template to PHP). Even if a template was compiled, Smarty will check this every time a customer opens your site. And if there are "many" templates it will slow down your site.

Roja wrote: I think you may have some misunderstandings about Smarty that could be fixed with some more experience or discussion.
Actually, I have 2-years experience with Smarty. And now I see that there is no real difference between Smarty and PHP itself. Therefore I'm trying to find a better solution (template engine).
Domovoy
Forum Newbie
Posts: 5
Joined: Wed Nov 24, 2004 8:43 am

Post by Domovoy »

McGruff wrote:Not sure about php5 support but there's also WACT - which comes with unit tests.
It appears as it's a really interesting one. For example, it has "Widgets" (http://wact.sourceforge.net/index.php/Widgets).
Probably it isn't the same as I'm looking for, but it has interesting features.

Thank you for the link.
Domovoy
Forum Newbie
Posts: 5
Joined: Wed Nov 24, 2004 8:43 am

Post by Domovoy »

Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

Domovoy wrote: As you see, the second example (a template engine I'm looking for) is easier for understanding and modifying. Moreover, you can open the second code in a browser.
Ah. Well, thats quite a bit different then most template systems I've seen. Now I understand better what you were looking for - good luck in finding it.
Domovoy wrote: Just enable "E_STRICT" option in php.ini. Smarty isn't designed for PHP5 for now.
E_STRICT doesnt determine whether something will *run* in PHP5 - it helps clean up issues that could be *improved* for PHP5:

"A new error level, E_STRICT has been introduced to PHP 5 by Andi Gutmans. It will be switched off by default, and is for "purists can use to make sure that there scripts are using the latest and greatest suggested method of coding (according to what we decide)." " - http://www.zend.com/zend/week/week162.php

I'll say it again - Smarty runs perfectly well under PHP5, and with E_ALL (not purist-mode), it shows no warnings. You'll find that the vast majority of large php apps and libraries all fit in this category at this point (Serendipity, Adodb, phpmailer, phpbb...).

Methinks you have an unreasonable expectation for existing codebases - PHP5 has only been out for four months, and Smarty has almost 10,000 lines of code. The fact that it works - flawlessly - in PHP5 is impressive enough.
Domovoy wrote: And what is the difference in the example above? Why I should use Smarty?
Multiple reasons:

- The files arent seperate. (Arguably, you could easily make them so)
- The template you used cant be opened by an editor without errors - with Smarty's templates, dreamweaver does just fine.
- You dont get caching for free

Like with any discussion about template engines - there is a fine line between using it and using PHP, which I already said. You clearly want a template engine already, so I dont see much need to defend the choice of using a template engine in general. :)
Domovoy wrote: No, I meant the "behind the scenes" compilation (i.e. template to PHP). Even if a template was compiled, Smarty will check this every time a customer opens your site. And if there are "many" templates it will slow down your site.
If you seperate output from processing, there will always be an overhead, period. To include the file, to check it, what have you. In my testing, I show the overall overhead of having smarty doing templates as low as 0.02% and at its highest at roughly 0.64%. When I compare it against doing simple file inclusion, the numbers are even lower.

Arguably, thats different from "No overhead", but its trivial. Yahoo uses it and they scale just fine.. My experiences are no different. :)
Domovoy wrote: Actually, I have 2-years experience with Smarty. And now I see that there is no real difference between Smarty and PHP itself. Therefore I'm trying to find a better solution (template engine).
Ah. Well, we'll have to agree to disagree on that point. There are huge differences in my opinion.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Interesting to hear some test results with Smarty - much lower than I'd have thought with a wild guess.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

McGruff wrote:Interesting to hear some test results with Smarty - much lower than I'd have thought with a wild guess.
Being balanced, I don't honestly use the really crazy-complex stuff in Smarty. By and large, I use three pieces of functionality: Section, If/else, and variable substitution. That has been sufficient to power roughly 5,000 lines of template code in three projects so far, which isnt too bad. (Thats something like 4-5% the code in all my projects right now)

I'd definitely agree that the sample size there is *small*, and that YMMV. But for those three functions, yeah, I was fairly surprised at the low overhead myself.

Others have done tests and found varied results (some faster, some slower), here's a few from google. Some agree, some wildly disagree. You be the judge for your particular situation:

http://www.webmedic.net/webify_bench/
http://www.massassi.com/bTemplate/benchmarks/
http://www.smartphp.net/content/smartte ... speed.html

Obviously, each draws completely different conclusions. Like I said - YMMV, but in my testing, and for what I use it for, its amazingly low-overhead.
Post Reply