how to use Smarty better with PHP?

前端 未结 11 1880
孤街浪徒
孤街浪徒 2021-01-02 17:50

I found that using Smarty with PHP, sometimes extra time will need to be used for

1) using quite different syntax than PHP itself
2) need to check small cases, b

11条回答
  •  余生分开走
    2021-01-02 18:06

    I like template engines and think they should be used, but in the particular case of Smarty, I think it's waste of time, because it's not a significant improvement over PHP as templating language:

    • The new syntax is still based around old concept of special tags inserted in random places in the document.
    • Because Smarty does not understand HTML's syntax/structure, it cannot not help you to create valid/well-formed HTML. Smarty's tags violate HTML's syntax, so once you add them, other standard tools can't help you either.
    • Smarty's output, just like in PHP, is insecure (unescaped) by default and you have to remember to add |escape everywhere where you output data in HTML.

    There's one particular PHP template engine that I've fallen in love with, which fixes all those problems: PHPTAL.

    It's still something new you have to learn, and it's a depenency for your application, but I think having XSS and ill-formedness problems solved makes it worth the trouble.

    PHPTAL just like Smarty is compiled once to PHP and cached, so performance is comparable to raw PHP.

提交回复
热议问题