Welcome Guest, Not a member yet? Register   Sign In
Which template engine do you use..?
#31

[eluser]Michael Wales[/eluser]
I've been playing with Python Web Framworks lately (primarily, Python and Turbogears). At first it was odd, but I can definitely see the benefits of using a template engine similar in nature to Kid, found within the Turbogears framework.

Essentially, it extends the XML namespace to include a few more tags, thus ensuring your code still validates while providing content for your designer to work with (in a WYSIWYG, like Dreamweaver).

Maybe a code example is the best explanation:
Code:
<div id="users">
  <p><img id="photo" src="default.png" py:attr="{'src':photo_url}" /></p>
  <p py:content="username">User's username would go here.</p>
</div>

This is a very basic example, but essentially - when the designer is working on the site he will see a default image for the user and the text "User's username would go here."

Since the dynamic work is done within the xHTML tag, the chances for the designer screwing things up is minimized - it's much more likely for a designer to delete the yellow "&lt;?" icon that appears in Dreamweaver.

I'm not really sure where I am going with this - just something I noticed and thought was neat. It would probably take me awhile to actually get used to it within a PHP environment.
#32

[eluser]adamp1[/eluser]
Problem I found with the CI parser (which I released a small fix for) was it didn't remove tags which were not replaced. This meant if you had conditional tags in a view file (I.E ones which where replaced sometimes but not others), they were left in.

Personally I used to use a template library but since I do the design as well find no reason to, so mark me down for another:

PURE PHP BAD BOY
#33

[eluser]DerekF[/eluser]
[quote author="dtrenz" date="1205264936"][quote author="Techie-Micheal" date="1205211255"]short tags are deprecated and I think CI is the only framework that rewrites them.[/quote]

where did you see that short_open_tag was deprecated?

you don't need to have CI or anything else rewrite them if you can just turn it on in php.ini[/quote]

Even if short_open_tag isn't deprecated (not only are they disabled by default, but I was pretty sure I read somewhere that there was a chance that PHP 6 was going to remove them altogether), it's generally a bad idea to use them because a) you can't depend on them being activated everywhere (less portability) and b) they are not usable if you use XML files at all. Personally, I find them ugly to use and therefore don't really make the code any cleaner or easier to understand when they're used (just MHO, of course).

[Edit] Sorry, should have clarified: short_open_tag is disabled by default in the "recommended" php.ini file.
#34

[eluser]dtrenz[/eluser]
[quote author="DerekF" date="1205268466"]
Even if short_open_tag isn't deprecated (not only are they disabled by default, but I was pretty sure I read somewhere that there was a chance that PHP 6 was going to remove them altogether), it's generally a bad idea to use them because a) you can't depend on them being activated everywhere (less portability) and b) they are not usable if you use XML files at all. Personally, I find them ugly to use and therefore don't really make the code any cleaner or easier to understand when they're used (just MHO, of course).

[Edit] Sorry, should have clarified: short_open_tag is disabled by default in the "recommended" php.ini file.[/quote]

a) I agree about portability, but if you are coding for a specific server environment that you control (as I do) then it is a non-issue.

b) I'm not familiar with any conflicts with XML unless you are trying to nest PHP in an xml file, which just sounds like trouble. I'm sure there a variety of work-arounds. I've never had problems working with XML with short_open_tag turned on.

Everyone is entitled to their own opinion, but I think most people would agree on which of these 2 is cleaner:

Code:
<h1>&lt;?=$page_title?&gt;</h1>
&lt;? if ($logged_in) : ?&gt;
<p>Welcome back, &lt;?=$username?&gt;</p>
&lt;? endif; ?&gt;

Code:
<h1>&lt;?php echo $page_title; ?&gt;</h1>
&lt;?php if ($logged_in) : ?&gt;
<p>Welcome back, &lt;?php echo $username; ?&gt;</p>
&lt;?php endif; ?&gt;
#35

[eluser]DerekF[/eluser]
[quote author="dtrenz" date="1205270137"][quote author="DerekF" date="1205268466"]
Even if short_open_tag isn't deprecated (not only are they disabled by default, but I was pretty sure I read somewhere that there was a chance that PHP 6 was going to remove them altogether), it's generally a bad idea to use them because a) you can't depend on them being activated everywhere (less portability) and b) they are not usable if you use XML files at all. Personally, I find them ugly to use and therefore don't really make the code any cleaner or easier to understand when they're used (just MHO, of course).

[Edit] Sorry, should have clarified: short_open_tag is disabled by default in the "recommended" php.ini file.[/quote]

a) I agree about portability, but if you are coding for a specific server environment that you control (as I do) then it is a non-issue.

b) I'm not familiar with any conflicts with XML unless you are trying to nest PHP in an xml file, which just sounds like trouble. I'm sure there a variety of work-arounds. I've never had problems working with XML with short_open_tag turned on.
[/quote]

I'm not sure what you mean by "nesting PHP in an XML file," but for example, the following code is perfectly valid:

Code:
&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
<message>
    &lt;?php foreach ($items as $item) { ?&gt;
        <item>&lt;?php echo $item['title'] ?&gt;</item>
    &lt;?php } ?&gt;
</message>

but is broken if short_open_tag is enabled. (Incidentally, I suppose this is an example of XML embedded in a PHP file.)

Quote:Everyone is entitled to their own opinion, but I think most people would agree on which of these 2 is cleaner:

Code:
<h1>&lt;?=$page_title?&gt;</h1>
&lt;? if ($logged_in) : ?&gt;
<p>Welcome back, &lt;?=$username?&gt;</p>
&lt;? endif; ?&gt;

Code:
<h1>&lt;?php echo $page_title; ?&gt;</h1>
&lt;?php if ($logged_in) : ?&gt;
<p>Welcome back, &lt;?php echo $username; ?&gt;</p>
&lt;?php endif; ?&gt;

How about a third:

Code:
<h1>&lt;?php echo $page_title; ?&gt;</h1>

&lt;?php
if ($logged_in) {
    echo '<p>Welcome back, '. $username .'</p>';
}
?&gt;

or maybe:

Code:
<h1>&lt;?php echo $page_title; ?&gt;</h1>

&lt;?php
if ($logged_in) {
    echo "<p>Welcome back, $username</p>";
}
?&gt;

My point being that what seems obviously clearer to you is not necessarily clearer to everyone else because neither of the code samples you provided suit me. (Again, this is a personal opinion, not trying to ruffle feathers here.)
#36

[eluser]DerekF[/eluser]
Whoa, anyhow, didn't mean to hijack this thread with a discussion of short_open_tag! Carry on...
#37

[eluser]dtrenz[/eluser]
[quote author="DerekF" date="1205271430"]
My point being that what seems obviously clearer to you is not necessarily clearer to everyone else because neither of the code samples you provided suit me. (Again, this is a personal opinion, not trying to ruffle feathers here.)[/quote]

I see your point (though I wouldn't even bother with curly braces), but then you're not really formatting your PHP in a template manner.

If one of the goals of a template (which I believe it should be) is to create simple designer-friendly presentation logic, it seems to me that a designer with limited to no knowledge of PHP would work better with the short_open_tag versions.

I, also, would like to apologize for hijacking this thread. Although, I think that coding presentation logic in PHP should be a major considerations when deciding whether to use a template engine or not, especially if you are concerned with overhead or performance optimization.

I'm curious if anyone has seen any benchmarking of various popular template systems? How expensive is something like Smarty, anyway?
#38

[eluser]DerekF[/eluser]
Quote:I see your point (though I wouldn’t even bother with curly braces), but then you’re not really formatting your PHP in a template manner.

The reason I included the curly quotes is because my text editor allows me to select related code blocks by double-clicking the curly braces. (This is also why I don't use alternate PHP syntax; my editor doesn't give me the same functionality when that syntax is used.) I guess the reason for my preference becomes apparent. ;-)

Templating engines are definitely not a one-size-fits-all solution since everyone has different requirements. If the goal is to prevent designers from screwing up or otherwise injecting arbitrary PHP code into templates, then something like Smarty (with its alternate, non-PHP syntax and protective features) makes sense. However, unless that's an absolute requirement (and I can understand why it may be), I've always been of the opinion that templating is best left to PHP or a "thin" templating engine like phpSavant. Although it requires the non-PHP-savvy designer to learn some basics of PHP (echo, loops and conditionals -- something the designer will have to learn anyway, regardless of whether you use a template engine or plain PHP), at least the language is already well-documented and with PHP being standardized (i.e., learning PHP as opposed to a proprietary templating language), it can benefit the designer more in the long run. Along the same lines, improving the readability of PHP code in your views/templates can go a long way to helping non-PHP people understand how things work.
#39

[eluser]louis w[/eluser]
Personally I'm not a big fan of if and endif. I prefer to enclose things in brackets, it helps to easily define a block of text. In fact I have hardly ever seen endif used until i started looking at these boards.
#40

[eluser]Techie-Micheal[/eluser]
[quote author="dtrenz" date="1205264936"][quote author="Techie-Micheal" date="1205211255"]short tags are deprecated and I think CI is the only framework that rewrites them.[/quote]

where did you see that short_open_tag was deprecated?

you don't need to have CI or anything else rewrite them if you can just turn it on in php.ini[/quote] http://en.wikibooks.org/wiki/Programming..._from_HTML among other places. It is also not something everybody enables. When I had my dedicated box, I didn't compile PHP with them enabled. Not something that should be used for these two reasons, IMO.




Theme © iAndrew 2016 - Forum software by © MyBB