Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I have a different complaint: I think it makes sense to have a template system for PHP because PHP is such a poor template system. :)


I generally like PHP but am not a very advanced user, so I'm interested: what shortcomings do you see?


There are two major problems for me: one is that markup filled with "<?=$varname;?>" is harder to parse than markup filled with "{varname}"; for a designer, those extra characters are totally extraneous, and they're not even balanced the way all other kinds of special markup are in HTML or XML.

Secondly, there's nothing in PHP that guides one away from packing more and more functionality into the template instead of the template's caller; it's easy to find snippets of PHP that do something "cool" online, and it's hard to explain to your designer why they shouldn't use them.

So, too verbose and less maintainable, is what it boils down to.


I'm not sure why you believe that it is necessary to do stuff like <?=$varname;?>. Certainly, a competent PHP programmer can use file operations and str_replace to do templating; it's pretty much what I do right now. The template for a dog-related site I'm working on looks like this:

<div class="dog" id="dog_{{__id__}}">

   <h1 class="name">{{__name__}}</h1>

   <p class="sex">{{__sex__}}</p>

   <p class="breed">{{__breed__}}</p>

   <p class="images">{{__pictures__}}</p>

   <div class="description">{{__description__}}</div>
</div>

(The formatting appears to be lost; accept my apologies)

That said, I am also working on fixing a restaurant site that a friend programmed, and it is littered with the type of disgusting syntax you mention. It's the difference between a designer who does snippets of PHP and a programmer.


You're not arguing against me, but with me. ;)

The article suggested that it was obviously a bad idea to write a templating engine in PHP, since PHP is already in the category of "templating engine". As you point out, it's easy to implement a template system in PHP that's better to use than raw PHP would be.


Thanks! I won't start using Smarty because of that, but those sound like valid reasons for bigger projects.



eru, your answer is too generic - I could have googled that myself. I'm interested specifically in the shortcomings of PHP as a templating system.


OK. E.g. PHP does not check whether your templates are valid HTML (or even just half-way sane tag soup) at all.

And you need to put a $ in front of every variable. Seems like someone did not know how to write a parser..


Do other template engines, e.g. Smarty, check HTML validity? I tried looking at the docs and it wasn't apparent.


I would guess the PLT webserver can do so. At least it will statically avoid unclosed tags, since it's based on s-expressions.

http://docs.plt-scheme.org/web-server/run_ss.html#%28part._i...


Now go to google.com and view source, you'll see it couldn't have been output by the PLT webserver. :-)


Yes. However they optimize for speed and have the resources to test their pages in every browser.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: