DEV Community

theoretician
theoretician

Posted on

Would you recommend using Template Engine for PHP?

Interested to know the pros and cons of using PHP Template engines.

Top comments (12)

Collapse
 
ssmusoke profile image
Stephen Senkomago Musoke

I would suggest you go for Twig, if I remember well you can build your templates using the Twig syntax or even PHP

But go for templates as they are reusable (can have different parts built together), inheritable (you can have a master template and keep overriding different parts) and performant (as good as PHP code)

No need to have HTML, CSS or JS in your controllers

PS: The templates are supported in your regular IDE like PhpStorm, Atom, Visual Studio code with autocomplete etc

Collapse
 
theoretician profile image
theoretician

Thank you, Stephan, for the thoughts!

Collapse
 
nsbx profile image
Nicolas Bondoux

Clearly yes ! (and a framework if you don't use one)
I use Symfony and twig since >1 years and this is so cool and clean.
For my job i also work with java and i deplore the poor quality of templates engines :(
Twig is very simple and very powerful, you can use custom function and filter, this is a little tricky at the first but when you know how to use it you earn so many time.

Collapse
 
theoretician profile image
theoretician

Thanks, I'll definitely gonna use twig.

Collapse
 
laurentiu-dancu profile image
Laurentiu Dancu

I use Twig a lot. I'd say a pro would be that your templates will be waaay neater than pure PHP while the obvious con is that you have to learn yet another language.
If you want to give it a shot, it works wonders with Symfony. Look it up.

Collapse
 
theoretician profile image
theoretician

Sure, Thanks for the feedback Laurentiu :)

Collapse
 
jeroka profile image
Esteban Rocha • Edited

Twig is very good, I begin using it on D8 and some flat file CMS's like Pico, it's based on Jinja

Collapse
 
theoretician profile image
theoretician

Thanks for the reply, Esteban. I'm on it!

Collapse
 
thinsoldier profile image
thinsoldier

I have used plates from the php league and blade from laravel. I strongly suggest using any template system over no template system. For a decade I was working at a place where 3rd party code was frowned upon. Slowly but surely over my years my code evolved to contain some basic concepts common to plates and blade. When I left that job and finally used a real template system I wanted to kick myself.

Collapse
 
denniszam profile image
Dennis Funderburg

Definitely recommend using a template engine for PHP to separate presentation and html from PHP. Smarty has been around for a very long time and I highly recommend it.

Collapse
 
theoretician profile image
theoretician

Thanks, I'm working on a crowdfunding platform (similar like Kickstarter).