Template Engine
Moderator: General Moderators
- mikealeonetti
- Forum Newbie
- Posts: 5
- Joined: Wed Sep 06, 2006 2:52 pm
- Location: Long Island
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- mikealeonetti
- Forum Newbie
- Posts: 5
- Joined: Wed Sep 06, 2006 2:52 pm
- Location: Long Island
I'm honestly not sure what you mean, but I suppose it wouldn't kill me to be less vague about what I was doing.
I had a menu on the main template that was something like this:
- Menu Item 1
+- Sub Item 1
+- Sub Item 2
+- Sub Item 3
- Menu Item 2
+- Sub Item 1
+- Sub Item 2
+- Sub Item 3
- Menu Item 3
+- Sub Item 1
+- Sub Item 2
+- Sub Item 3
It was a dynamic menu that could be edited in the backend and the menu was stored in MySQL. So I asked myself, "what would be the best way to get Smarty to construct the meny for me?" My best answer was "create a multi-dimensional array!"
So I did. I fetched the menu items first from MySQL (probably using something like "SELECT * FROM menu_items WHERE parent_id IS NULL") and pushed them into an array called $menu_items. Then I fetched all of the child item ("SELECT * FROM menu_items WHERE parent_id={$menu_id}") and then put each item (which itself became an array of values) as a subitem of $menu_items. So in the end, the array probably looked like:
And et cetera going right down the line. This was probably the best way I had figured to pass these type of values to Smarty. When Smarty didn't let me loop from the values of an already looping array that is when I had to consult Google. That is when I found it wouldn't let me do that (like six months ago or so). That is when I decided to make a template that allowed me to loop both in the code and in the template depending on which was better. So in the end to avoid havingn to put data into an array and then pass it to the template engine, I had to make a template engine that broke one file up into menu templates to allow calling of functions like "parse_template" again and again to be like "foreach" looping in the code.
Maybe I went about using Smarty the wrong way to begin with.
I had a menu on the main template that was something like this:
- Menu Item 1
+- Sub Item 1
+- Sub Item 2
+- Sub Item 3
- Menu Item 2
+- Sub Item 1
+- Sub Item 2
+- Sub Item 3
- Menu Item 3
+- Sub Item 1
+- Sub Item 2
+- Sub Item 3
It was a dynamic menu that could be edited in the backend and the menu was stored in MySQL. So I asked myself, "what would be the best way to get Smarty to construct the meny for me?" My best answer was "create a multi-dimensional array!"
So I did. I fetched the menu items first from MySQL (probably using something like "SELECT * FROM menu_items WHERE parent_id IS NULL") and pushed them into an array called $menu_items. Then I fetched all of the child item ("SELECT * FROM menu_items WHERE parent_id={$menu_id}") and then put each item (which itself became an array of values) as a subitem of $menu_items. So in the end, the array probably looked like:
Code: Select all
array
(
[0]=>array
(
"item_id"=>1,
"text"=>"Menu Item 1",
"url"=>"http://www.page.com/url1",
"sub_menu_items"=>array(
(
[0]=>array
(
"item_id"=>2,
"text"=>"Sub Item 1",
"url"=>"http://www.page.com/url2"
),
[1]=>array
(
"item_id"=>3,
"text"=>"Sub Item 1",
"url"=>"http://www.page.com/url3"
),
)
)
)Maybe I went about using Smarty the wrong way to begin with.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
That's one of the many reasons I have Template Lite out there. Smarty is nice but man is it bloated with code that isn't needed. It does use some modularization but not enough. It is compatible with most of the features of Smarty plus I have added quite a few new template functions. The default install for Template Lite has more functions for the template files than Smarty with more coming each release.mikealeonetti wrote:There are too many template systems out there, and the newer ones just get blown away by smarty. Wish we could get them all together at one domain and give them all a sub-domain so they'd be easier to choose for people.
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
Actually, with Smarty and Template Lite there is ZERO compilation after the first time the template file is accessed. The file is only compiled once and will not be compiled again unless the original file is altered. Basically the same thing as your example.Hockey wrote:It's fast, there is ZERO compilation and it's more extendable/flexible due to the fact it's entirely done in PHP, so your not limited to obscure keywords like Smarty's {section}
And you are definately right about Smarty's {section} function. It is obscure and the syntax is HORRIBLE. That is why Template Lite has the {for} function which is similar to the PHP for function. With the latest release of Template Lite (September 1, 2006) I added support for the {while} function and it is almost identical to how the PHP while function operates. Plus there are a number of new functions with more coming.
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
Actually you can do almost exactly the same thing using Smarty or Template Lite. The main difference is how you format the multi-dimentional array. You can't use brackets [] if you want to use multi-dimentional arrays. You have to use {$myvariable.element1.element2.element3} which would be similar to $myvariable[$element1][$element2][$element3] in PHP.mikealeonetti wrote:And I tried to do something similar with Smarty, but it wouldn't let me. I spent a lot of time searching google for "multidimensional arrays smarty" but I found only plugins.
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
I was wondering where you were will all his fuss going onAKA Panama Jack wrote:Actually, with Smarty and Template Lite there is ZERO compilation after the first time the template file is accessed. The file is only compiled once and will not be compiled again unless the original file is altered. Basically the same thing as your example.Hockey wrote:It's fast, there is ZERO compilation and it's more extendable/flexible due to the fact it's entirely done in PHP, so your not limited to obscure keywords like Smarty's {section}
And you are definately right about Smarty's {section} function. It is obscure and the syntax is HORRIBLE. That is why Template Lite has the {for} function which is similar to the PHP for function. With the latest release of Template Lite (September 1, 2006) I added support for the {while} function and it is almost identical to how the PHP while function operates. Plus there are a number of new functions with more coming.
I understand that compilation phase only happens once...but and this is a big butt...
Thats the beauty of PHP templates...and is what I was advocating, not the fact that Smarty templates only compile once...it's useless...an un-nesscary step IMHO
You only need to boot your computer once and ideally in theory it could stay running forever, however they usually get turned off, but if we discovered a way to perpetuate a computers uptime, with zero cost ontop of what we pay in terms of mechanical failure, power usage, etc, don't you think most people would prefer the latter over having to remember to boot their computer every once in a while.
Yes you can argue it "hardly" compiles...which is true and I am aware of that, but it still compiles, which for all intents and purpose is pointless...as PHP templates are just as easy to follow and more powerful....and don't compile...
Simply put, current generation template engines simply don't offer enough for me to bother using them over native PHP template syntax.
Cheers
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
Well, I spent a week and a half in the hospital. Then about a week at home doing nothing. Then another week and a half on vacation at Disney World. Yes, Disney World.Hockey wrote:I was wondering where you were will all his fuss going onAKA Panama Jack wrote:Actually, with Smarty and Template Lite there is ZERO compilation after the first time the template file is accessed. The file is only compiled once and will not be compiled again unless the original file is altered. Basically the same thing as your example.Hockey wrote:It's fast, there is ZERO compilation and it's more extendable/flexible due to the fact it's entirely done in PHP, so your not limited to obscure keywords like Smarty's {section}
And you are definitely right about Smarty's {section} function. It is obscure and the syntax is HORRIBLE. That is why Template Lite has the {for} function which is similar to the PHP for function. With the latest release of Template Lite (September 1, 2006) I added support for the {while} function and it is almost identical to how the PHP while function operates. Plus there are a number of new functions with more coming.
Well, if your templates are changing frequently then you are not using templates PROPERLY. I think you will find that the vast majority of sites that use template engines the templates rarely change. If you create your templates properly you can change things just by using dynamic includes inside the main template file that will load other templates inside the main template based upon what has been passed by the main PHP program. I have done this and you really never have to change a template that has been compiled.Hockey wrote:I understand that compilation phase only happens once...but and this is a big butt...it still happens, so if your templates change frequently...that's a hit on performance, whereas PHP native templates don't compile at all - they don't need to
If you have ever run across a site that frequently changes what is in their template files then you have run across some very bad programming practices regarding templates.
Yes they compile once but they are definitely not useless.Hockey wrote:Thats the beauty of PHP templates...and is what I was advocating, not the fact that Smarty templates only compile once...it's useless...an un-necessary step IMHO
You only need to boot your computer once and ideally in theory it could stay running forever, however they usually get turned off, but if we discovered a way to perpetuate a computers uptime, with zero cost ontop of what we pay in terms of mechanical failure, power usage, etc, don't you think most people would prefer the latter over having to remember to boot their computer every once in a while.
Yes you can argue it "hardly" compiles...which is true and I am aware of that, but it still compiles, which for all intents and purpose is pointless...as PHP templates are just as easy to follow and more powerful....and don't compile...
The one thing I have noticed with your method, and yes I used it frequently years ago, it is considerably HARDER to follow. You have a nasty mix of PHP and HTML code in your template file making it look more of a MESS.
PHP based templating...
Code: Select all
<?php if ($this->_vars['total_closed'] != $this->_vars['totalgames']): ?>
<td width="134">
<input type="image" name="login" src="templates/<?php echo $this->_vars['templatename']; ?>images/login.gif" value="<?php echo $this->_vars['l_login_title']; ?>">
</td>
<?php endif; ?>Code: Select all
{if $total_closed != $totalgames}
<td width="134">
<input type="image" name="login" src="templates/{$templatename}images/login.gif" value="{$l_login_title}">
</td>
{/if}Maybe for you it is easier but you will find template designers are usually NOT programmers.
And I don't know what you are getting at with UPTIME. What does using a template engine have to do with a servers uptime or rebooting? When a template is compiled is stays in the compile directory. You reboot the server the template is NOT recompiled. The already compiled one stored in the compiled directory is used. The only time a template will be recompiled is if the original template file is modified.
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
In the hospital? Well I hope your feeling better
and had fun at Disney World/Land???

Uptime has nothing to do with template design
It was purely for analogy...to make light of why I find template engines pointless...
I realize they only compile once...but the fact is, the only advantage they offer is completely subjective and thats the syntax clarity, nothing more, nothing less...
Being a subjective point, there is at least a 50/50 chance that template engines are pointless.
There is no hard proof or technical reason to use Smarty over PHP native templates was my point.
Even if there only compiled once, how much code (SLOC) does it take to emulate a quasi compiler???
100 lines??? 200 lines maybe even 50???
The point is, I could randomly inject for loops nested inside a IF statements through out my code which "only" executed when the file TIMESTAMP was a prime number...that would seem pointless wouldn't it???
Your theory on properly constructed templates not changing??? I dunno, again very subjective...
I update my web site frequently, layout included, changing menu items, navigation, etc, adding, removing, constantly tweaking...lately I've been converting slowly over to XHTML/CSS designs, so again a big change in terms of template layout. Depends on how fresh people want or keep their site wouldn't you say???
The truth is the *driving* force behind template engines is seperating content from presentation and PHP native templates do that just fine, without the overhead of compilation, etc...
So yes Smarty and your own might offer neat little filters here and there, some of which I argue are bad design anyways, and you may be able to skip a few lines by using an OPTION tag repeater or whatever their called
But still for me personally, they don't justify the bloated use of a current day template engine...
Cheers
Perhaps, but I think thats a matter of opinion. Most designers I've worked with have at least a semi-working understanding of how PHP works, so I'm sure it wouldn't bother them, in fact I know it doesn't.Maybe for you it is easier but you will find template designers are usually NOT programmers.
Uptime has nothing to do with template design
It was purely for analogy...to make light of why I find template engines pointless...
I realize they only compile once...but the fact is, the only advantage they offer is completely subjective and thats the syntax clarity, nothing more, nothing less...
Being a subjective point, there is at least a 50/50 chance that template engines are pointless.
There is no hard proof or technical reason to use Smarty over PHP native templates was my point.
Even if there only compiled once, how much code (SLOC) does it take to emulate a quasi compiler???
100 lines??? 200 lines maybe even 50???
The point is, I could randomly inject for loops nested inside a IF statements through out my code which "only" executed when the file TIMESTAMP was a prime number...that would seem pointless wouldn't it???
Your theory on properly constructed templates not changing??? I dunno, again very subjective...
I update my web site frequently, layout included, changing menu items, navigation, etc, adding, removing, constantly tweaking...lately I've been converting slowly over to XHTML/CSS designs, so again a big change in terms of template layout. Depends on how fresh people want or keep their site wouldn't you say???
The truth is the *driving* force behind template engines is seperating content from presentation and PHP native templates do that just fine, without the overhead of compilation, etc...
So yes Smarty and your own might offer neat little filters here and there, some of which I argue are bad design anyways, and you may be able to skip a few lines by using an OPTION tag repeater or whatever their called
But still for me personally, they don't justify the bloated use of a current day template engine...
Cheers
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
The performance hit is minimal...I'll agree, but it's nonetheless a performance hit and the slower the machine or the larger the site codebase grows, the more noticable the hit becomes. Obviously on a slick as oil fast server, TL or Smarty for that matter wouldn't bottleneck.Everah wrote:The funny thing is that even when using a template parser, my apps still run insanely fast. I have an app that processes three templates, all kinds of conditionals within the templates and a bunch of other things going on and it parses in about .2 seconds. That, to me, is an acceptable time frame.
The point is...I'm anal I guess. I can't personally justify the costs of something so trivial when you can reduce those costs by using a *very* similar technique for (arguably) only the sake of clarity.
It's like using double quotes through your code if you don't need to...for the sake of clarity??? What if the string doesn't even have interpolated values??? Do you still use double quotes?
How about caching the value of a count() and using a variable instead of count inside a for loop, etc???
I see these as just as important as following good design, etc...
Coding conventions to me, are equally important, whereas most of you coughed at the idea...abstraction is something I strive for 110% of the time, again, other arguments I've had few could see where I was coming from.
Proper software development (in my experience) comes from experience. Bloat, mixed coding conventions, nonstandard coding practices, poor documentation, lack of comments, etc...
They all add or retract from a solid software system, so I am anal about everything not just one thing...I guess is what I'm saying.
If you can live with knowing your application is only mildly (variably) affected by this library or that and this library or that is only providing trivial benefits easily acheived by other techniques, then so be it.
Consider the following: I can find 50 developers/designers that all agree PHP native template syntax is just as easy to read as Smarty. I can even boast the fact that using PHP prevents any one from having to learn a new syntax, reducing the learning curve. On the flip side, I can also find 50 developers/designers who say Smarty syntax is waaaaaay easier than PHP native syntax. Fabulous, so we have a 50/50 split of opinion.
What I can absolutely promise you however, without a dought, 110% gaurantee, is that using Smarty or TL as a library instead of PHP native template syntax absolutely adds more bloat, more maintenance, more parsing and possibly execution overhead to your entire application as it's likely integrated into every page request.
Using that logic, I figured the choice was easy, but obviously not, going on the amount of rejection or objection I have received on the arguments I have made.
Meh, no biggie...I don't mind competitive advantages
Cheers