Is it worth learning Smarty for PHP templating? [closed]
I'm building a simple 10-page site using an MVC-like architecture. Is it worth learning the Smarty templating engine?
Template engines add overhead.
You should only use a template engine if you need to have others create/edit templates and you do not want them to be able to use PHP.
If you are the sole maintainer, defiantly go with plain PHP. If your team can be trusted (they are all devs), stick with plain PHP. If you have designers that don't know PHP or, more importantly, letting them use PHP is dangerous, then use Smarty or another template engine.
That being said, you are wondering if you should learn Smarty. You'll need to learn Smarty if creating templates with Smarty. You don't really need to learn Smarty if you are just implementing the Smarty template engine.
Smarty is template engine easy to learn and use, but I strongly recommend learning and using Twig instead.
no need.
a 10-page site is easy to build. if you would like to use Smarty, you will have to spend a little more time to learn it at the beginning~
PHP is a decent templating language itself. Unless there is need for advanced features, custom solutions will only complicate what sounds like a fairly simple setup.
I never got around to looking into smarty. Others scared me away saying it was very complex. I use Twig for pretty much everything I do now, even small stuff. Even if it isn't worth it for a small site I think it's worth learning for the long term - getting a good grounding in template inheritance and blocks is absolutely worth it.
Check this out:
<?php echo htmlspecialchars(\$var, ENT_QUOTES, 'UTF-8') ?>
vs.
{{ var|escape }}
yummy:
{% for i in 0..10 %}
* {{ i }}
{% endfor %}
I used Smarty a long time ago, I would recommend you that it's no need to use Template Engine like that. Smarty slows down the whole system because its core source code is really complex, and somehow unnecessary. If I had a recommendation on Template Engine, I would say:
1) Use some basic and light Template Engine such as XTemplate. I'm not sure if it's still developing but it's really useful, everything is in one class file, ready to use. The syntax is also clear and sweet.
2) Write your own Template Engine, so that you can customize it on your own that best fits your current system.
But, however, for tiny and small project (less then 5-7 pages) in which you can have a control over everything, then using Template Engine is probably unnecessary.
A templating language is a good choice when you're dynamically generating HTML. Be it Smarty or something else (for PHP), Razor or Forms (for ASP.NET), or something entirely different (John Resig's client-side micro-templating framework, perhaps).
I agree with what is said above. For a simple site with 10 pages, use PHP on the MVC model or use a lightweight and fast template engine like RainTPL or Savant.
That test might be biased as it is published by the author of RainTPL. But anyway I benchmarked several template engines with the Apache ab utility and can confirm these results. RainTPL is fast.
精彩评论