views:

394

answers:

5

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 MCV 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 MCV 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 MCV framework?

+3  A: 

Unless you allow for short tags,

{$foo}

is much more readable than

<?php echo $foo; ?>

Multiplied over a large project, it adds up.

DGM
That makes sense, simply reducing keystrokes can be a godsend! :)
MatW
+9  A: 

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.

thephpdeveloper
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?
MatW
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?
MatW
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.
thephpdeveloper
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.
MatW
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.
thephpdeveloper
A view may not define a template, but it can. So why use a template engine?
MatW
Well, it's really up to personal preference at the end of the day.
thephpdeveloper
+4  A: 

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.

TokenMacGuy
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!
MatW
@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.
Justin Johnson
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; http://www.talkphp.com/general/4001-template-engine.html#post22329
MatW
+1  A: 

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.

Justin Johnson
Decoupling the front-end dev is a good reason. +1
MatW
+5  A: 

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.
coreyward