Why use a templating engine with a framework?
Asked Answered
C

6

29

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 question is why bother? Surely an MVC framework, by definition, is a templating engine. Even "raw" PHP is, arguably, a templating engine. What possible benefits are there of adding another level of abstraction on top of what's already present in a framework such as Kohana?

EDIT - I realise that an MVC framework isn't the same thing as a templating engine, but surely the V part does the same job? Perhaps a better way of phrasing things would be; why add an templating engine on top of the V part of an MVC framework?

Cerell answered 5/11, 2009 at 2:41 Comment(0)
M
18

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.
Marilou answered 5/11, 2009 at 3:32 Comment(0)
V
18

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.

Viviennevivify answered 5/11, 2009 at 2:47 Comment(7)
So you're saying that using a templating engine on top of a framework is worthwhile when you already know the templating engine's syntax? Is that the only +ve?Cerell
Also, please can you expand on your comment about MCV != templating engine, as that's really the point of my quesion. Surely the View part of MCV is a template?Cerell
View does not mean template. View is only referring to how data is displayed, whereas template engine refers to a piece of code able to put data nicely into templates.Viviennevivify
But in defining a View, you have defined a template, no? My confusion comes from manner in which Kohana handles the View, and way Smarty works. Syntax aside they (ostensibly) work in the same way.Cerell
like I said, defining a view may not define a template. A template is a file that can be used across multiple pages. Each page is a different view.Viviennevivify
A view may not define a template, but it can. So why use a template engine?Cerell
Well, it's really up to personal preference at the end of the day.Viviennevivify
M
18

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.
Marilou answered 5/11, 2009 at 3:32 Comment(0)
L
9

Unless you allow for short tags,

{$foo}

is much more readable than

<?php echo $foo; ?>

Multiplied over a large project, it adds up.

Livvyy answered 5/11, 2009 at 2:46 Comment(4)
That makes sense, simply reducing keystrokes can be a godsend! :)Cerell
A better comparison is the leanest possible escaping: {$user_data|escape} or {$user_data} vs <?php h($user_data);?>Unseen
With short tags on it's just <?=$foo?>.Mireille
Yes, but every time I have suggested using short tags in php, the php community breaks out the pitchforks and torches. Which is why I prefaced my answer with "Unless you allow for short tags". In any case, I left php a long time ago for more readable frameworks.Livvyy
G
6

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.

Gruesome answered 5/11, 2009 at 2:49 Comment(4)
I see your point, but I wouldn't consider raw PHP to be too much for presentation purposes; I'm quite happy to risk hanging myself with PHP spaghetti!Cerell
@MattW Just throwing it out there, but that's an incredibly un-hireable practice and most people don't want to work with someone who does that kind of thing.Kessler
eh?! Knowing a scripting language and using it, rather than relying on a potentially unnecessary abstraction layer is an un-hireable practice... This comment sums things up quite well; talkphp.com/general/4001-template-engine.html#post22329Cerell
If you're coding a project and you're the only one who's going to touch the code, then good discipline may be all you'll need. With multiple developers, you have to deal to the lowest common denominator of discipline. Now, multiply that by an open source project, and the lowest common denominator gets really really low... ...which bodes ill for a project using pure php.Unseen
K
2

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.

Kessler answered 5/11, 2009 at 3:28 Comment(0)
A
1

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.

Awlwort answered 26/2, 2011 at 22:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.