Page 3 of 3

Posted: Sun Sep 10, 2006 1:56 pm
by mikealeonetti
You're right, embedded looping works just fine.

However, I tried looping from a member of the current foreach "value." That is, as in the example above, I told Smarty to foreach and put values into x and then I told Smarty to loop using x.value in the next line. Smarty did not like that.

Posted: Mon Sep 11, 2006 7:12 pm
by RobertGonzalez
What if you didn't put those values into x, just looped right there where it was?

Posted: Tue Sep 12, 2006 2:40 pm
by mikealeonetti
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:

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"
      ),
    )
  )
)
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.

Posted: Tue Sep 12, 2006 2:45 pm
by RobertGonzalez
I think this may have caused some issue --> ("SELECT * FROM menu_items WHERE parent_id={$menu_id}")

What I did was pull two complete arrays, then match keys in the Template loops. Never had a problem with it.

Posted: Thu Sep 21, 2006 6:29 pm
by AKA Panama Jack
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.
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. :D

Posted: Thu Sep 21, 2006 6:44 pm
by AKA Panama Jack
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}
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. :)

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.

Posted: Thu Sep 21, 2006 6:55 pm
by AKA Panama Jack
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.
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.

Posted: Thu Sep 21, 2006 7:35 pm
by alex.barylski
AKA Panama Jack wrote:
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}
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. :)

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 was wondering where you were will all his fuss going on :P

I understand that compilation phase only happens once...but and this is a big butt... :P 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 ;)

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 :)

Posted: Thu Sep 21, 2006 9:52 pm
by AKA Panama Jack
Hockey wrote:
AKA Panama Jack wrote:
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}
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. :)

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.
I was wondering where you were will all his fuss going on :P
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. :D
Hockey wrote:I understand that compilation phase only happens once...but and this is a big butt... :P 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 ;)
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.

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.
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...
Yes they compile once but they are definitely not useless. :)

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; ?>
Smarty/Template Lite version...

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}
I find the second one much easier to read and understand and so will anyone who is dealing only with templates and not the program code. Sure you can clean the first one up a little more but it is STILL a mess. You can train a person to create templates much faster and easier when using a template engine. They don't need to know anything about PHP. You just give them a list of the variables and what they contain and they can create a template. They don't have to learn PHP.

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.

Posted: Thu Sep 21, 2006 10:37 pm
by alex.barylski
In the hospital? Well I hope your feeling better :P and had fun at Disney World/Land???
Maybe for you it is easier but you will find template designers are usually NOT programmers.
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. :P

Uptime has nothing to do with template design :lol:

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 :P

But still for me personally, they don't justify the bloated use of a current day template engine...

Cheers :)

Posted: Thu Sep 21, 2006 10:50 pm
by RobertGonzalez
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.

Posted: Thu Sep 21, 2006 11:35 pm
by alex.barylski
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 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.

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 :P

Cheers :)