Hacker News new | ask | show | jobs
by zdw 5014 days ago
Everyone probably knows this, but PHP's Smarty template engine (http://www.smarty.net) is really awesome.

I've yet to see a better one in any language - it has things like Unix-like pipe chaining of filters, user defined functions, and intelligent caching built in.

I reduced the number of lines in one codebase I worked on by 70% through use of Smarty.

6 comments

PHP is itself a template engine.

Bolting a template engine on top of that makes no sense whatsoever.

There is one feature of Smarty that is worth having in my opinion, that is the template inheritance. Basically similar to .NET master pages, Rails layouts and I think Django is built the same way. Much cleaner rather than including header/footer files.

Though I don't like having to use the Smarty psuedo-language, I do like the organization of code that it provides.

PHP is not a HTML template engine. You have to run all your content through library functions to encode and prevent it from being interpreted as markup. That's essentially making your own half-baked template engine from scratch in PHP, and it would be just as easy in any sane language.
Seriously? PHP "was" a template engine. But that was before 1998 when no one knew what was a good idea or a bad idea when it came to building web apps.
I hate PHP and don't even want to talk about it but I did skim through the docs for Smarty. The rationale is that PHP sucks as a template engine, so why not create a new one and use PHP for the model-controller/whatever.

Maybe that seems perverse, but you have stuff like Node.js in existence that oddly enough solves some problems quite nicely, so maybe it works for PHP guys. I'll never see the appeal compared to other languages though.

edit: That being said, the final result looks kind of horrible to me.

Completely unrelated: I'm not easily offended. Right now I'm not offended at all, in case anyone would interpet this post to mean so. I'm all OK. Really.

But that said: I'm severely puzzled about your choice of HN username.

Breivik is a fairly common last name. There are actually 8 men in Norway who have Anders as their first name, and Breivik as their last name.[1]

[1] http://www.ssb.no/navn/sok.cgi?lang=n&fornavn=anders&...

It could just be his name, that coincide with the more famous one. It can happen.
I work on a legacy code base with pretty much everything in SMARTY templates (including CSS and JS files!) and it's really aweful. It may not be SMARTY's fault, but most of what's going on could be just as easily done with PHP code than with SMARTY tags, include variable insertion (<?=$var?>) and control logic (if ($var) : // endif;).

Now we've actually moved everything to the client side using Ember and Handlebars templates. JS doesn't have any built-in templating like PHP (which is basically just a template engine), so Handlebars makes it really useful to write up HTML code blocks that get variable insertion and data-binding on the client side.

  > include variable insertion (<?=$var?>)
No disable your short tags and any nontrivial template will become ugly as hell, unless you are on php 5.4+ which not many are.
I enjoy using Hiccup more than any template engine I've used previously. Before I met (Clojure and) Hiccup[1], I would have said the same regarding Coffeekup[2].

But what's really amazing is when you combine Hiccup with Enlive[3]. Enlive can give you a headache when you first start working with it, but then there's an almost magical "aha!" moment, and not long after that you'll never want to look back. By "combine" I mean using Hiccup and metadata from your db (or whatever) to generate empty, logic-less views and then using Enlive to bind in your data.

Combine those steps with your own macros and HOFs, look for some choice places to memoize function calls, and now you're cooking with gas.

[1] https://github.com/weavejester/hiccup

[2] https://github.com/mauricemach/coffeekup

[3] https://github.com/cgrand/enlive

Nobody mentioned twig yet, so I will: http://twig.sensiolabs.org

And seriously, check Symfony 2 out.

I think that twig got its root in the django templating engine (which is python based), because of the resemblance. It's very good indeed. I'm not sure if the original had support for filters though.
Those features are a persuasive argument against smarty as a template language.

One of the worst flaws of php is that it makes it easy to mix up code and the view, which is ok if you're writing a single page, and terribly dangerous if you're writing anything more complex.

I've experienced this myself writing templates for things like phpbb (thankfully not something I do frequently) - every time the forum updates, the templates have to be patched again - they even have a weird patching system to try to work around the issue with code in views.

A better template language would actually limit the amount you can do in your view files to the bare minimum - something like mustache comes to mind. If you want to keep the views maintainable, easy to swap out, and easy to add alternatives to like json or XML as well as HTML, the aim should be to reduce the lines in your codebase by putting the filters, caching and logic around your views in helpers rather than inside your views.

@zdw, I actually think twig is nicer :) It's almost identical to jinja.