Why use a templating engine with a framework?

后端 未结 6 1253
迷失自我
迷失自我 2021-02-05 02:56

I recently discovered the PHP framework Kohana (which is awesome) and was reading a thread about using it in conjunction with a templating engine such as Smarty or Twig. My ques

相关标签:
6条回答
  • 2021-02-05 03:20

    Firstly, raw PHP is NOT a templating engine.

    Next, MVC framework defines that the framework is split into Model, View and Control. It does not mean that the framework has a template engine, though the View may be integrated with a native or external template engine. Thus the framework may or may not have a templating engine.

    Thirdly, View != template. View is only referring to how data is displayed - there is no template involved usually, whereas template engine refers to a piece of code able to put data nicely into templates - which you can greatly reduce the need to modify all files when you can modify them in the templates

    Lastly, framework users may prefer to use a more common templating engine such as Smarty over the native template engine in the framework. You do not need to learn the new tags in the native template engine.

    0 讨论(0)
  • 2021-02-05 03:24

    MVC implements the management of PHP templates, if you work in a team with web designers could be better to use HTML templates. Smarty and Twig are famous and good. Anyway just choose the template engine you feel more comfy with.

    0 讨论(0)
  • 2021-02-05 03:33

    One big reason why you would want a separate template engine is because raw PHP is a bit too much for the presentation of your site. If it's just you making your site, and you have a good idea about how the site's templates aught to fit together, then this isn't really a downside, but for larger projects, it gets in the way.

    If your project has outgrown a single developer, or if you want to add designer even before that, PHP is probably too hard a language to express the presentation in. Purpose built template languages are at the advantage because they are simple, and don't give you so-much rope as to hang yourself.

    Larger projects, even when they don't require much input from multiple developers, can make the free-form of plain PHP a bit unwieldy. the purpose built template engine provides (or enforces) a basic structure to how each template fits with the rest.

    0 讨论(0)
  • 2021-02-05 03:34

    Unless you allow for short tags,

    {$foo}
    

    is much more readable than

    <?php echo $foo; ?>
    

    Multiplied over a large project, it adds up.

    0 讨论(0)
  • 2021-02-05 03:35

    I have two really good reasons I can think of for doing this...

    1. Creating markup that is repeated throughout the site in a consistent format is easier, plus you can update it later without a lot of grepping.
    2. If this is for a real company, the people determining content aren't likely to be really familiar with HTML, much less PHP. Having a simple template language that keeps styles looking the right way and generates valid markup without much code knowledge is really handy.
    0 讨论(0)
  • 2021-02-05 03:42

    Mauris has already covered why MVC != template engine, but I would like to add that the more powerful the template engine is, the cleaner and more concise your templates are. This makes it especially easy for people who are not familiar with PHP to edit them (e.g.: if a designer/front end developer needs to edit the HTML). In general, MVC's don't boast this sort of functionality.

    Take a look at this example from Smarty. I've never used the engine and answering this question is the first time that I've ever seen its markup, but I can already tell exactly what its doing.

    Further, the amount of code that you have to write is noticeably less since Smarty and other engines take care of mundane things for you like alternate row colors and alternate content for empty data sets in the first example, and silly things like formatting <select> lists in this example.

    0 讨论(0)
提交回复
热议问题