9

I used smarty in past, in x-cart, jobberbase and jomestate component of Joomla and some other Joomla components as well. I saw that things were very much tightly coupled in x-cart and many things seems to be more complicated than they should be in jobberbase and I had impression that this was because of smarty.

Now I am going to use Yii in a project, suggested by client. And Yii itself is MVC framework. So should I use smarty with it? I want to know that should I use smarty for it , I mean will there be some pros of using smarty in Yii? Or not? Or will it be just an overhead or will there be some cons of using smarty in it? I have understanding of smarty. But I think that is sometimes make things more complex. So I want to know from you guys, that is is just a dominant thought due to bad frameworks or there is some reality in that, so should I use smarty or is there some thing else better than that?

Hafiz
  • 4,187
  • 12
  • 58
  • 111

3 Answers3

11

As someone that used to use smarty before Yii I'd say there is no point.

Smarty was great because it prevented you from mixing logic and view in one file. MVC architecture defines that controllers (containing logic) and view should be separate anyway. I'd recommend that you write your views like you would in smarty templates, but just use php syntax. If you are using a good IDE (like netbeans) it will automatically indent your code too (see code formatting in netbeans).

Also have a look at Alternative syntax for control structures

Possible Opinion Of Yii creator

Yii itself is derived from Prado which is Qiang Xue previous project. Prado has a tempting language and Yii (out of the box) doesn't I'd assume that this means that Qiang believes a templating language is unnecessary.

Smarty Overhead

Smarty templates are complied to PHP once per change (if caching is enabled), this means that most of the overhead only happens for the first request, but I'd assume there is still some overhead in the extra function calls to check that the compiled view exists and this would affect every request. So there is an overhead to smarty, but it's probably negligible.

Hafiz
  • 4,187
  • 12
  • 58
  • 111
Olly Hicks
  • 1,104
  • 1
  • 9
  • 13
  • I understand alternate syntax but just want to konw that is there any advantage of smarty while using already MVC architecture? or will there be an overhead? – Hafiz Apr 21 '12 at 21:50
  • That depends entirely on what you would consider the advantage of smarty? – Olly Hicks Apr 22 '12 at 12:15
  • There is no difference between using smarty in a bespoke application and using it in a framework. Mixing views and logic is always bad practice IMO. But if your strict with yourself and other developers you don't need smarty to enforce that – Olly Hicks Apr 22 '12 at 12:34
  • You can disable the compile check using `$compile_check = false;`. It makes it very fast so that there is no overhead once created. Also when used with memcache, then Smarty/PHP can be very speedy. – Panama Jack Sep 02 '13 at 06:23
  • This is the accepted and the most voted answer?! Pure PHP templates does not help with user input escaping, template inheritance, does miss useful helpers/filters and the syntax is just ugly. The performance hit of a good templating engine is negligible. Why would anyone want to use PHP as a templating engine? – Petr Peller Feb 05 '14 at 13:29
2

Smarty is, like any template language, an expensive overhead.

As Rasmus Lerdorf said, PHP is already a template engine. Is there any reason to add another language with other controls structures, that you already have in PHP ?

Templates engines are only useful because they embed the cache logic. If you are not able to build a cache logic yourself, then learn it.

nichiporets
  • 431
  • 7
  • 18
  • 1
    You don't need to use a template engine to have cache logic in yii, there is a build in cache that you can activate – darkheir May 14 '13 at 08:17
1

Smarty would not be overhead if compiled to php.

I would only use Smarty when there is separate roles of developer and designer (who wants to use Smarty)

Other than that its just another language to learn.

Imre L
  • 6,159
  • 24
  • 32