Twig, the next generation template engine for PHP 
I've been using Smarty 2 for about 7 years, and, while Smarty 3 is still in beta, Twig just popped up in the scene. Notable Smarty opposer Fabien Potencier was apparently converted by this template engine, so Twig must either be magic, or excellent, or both.
One of the major drawbacks of Smarty is that it is bloated (think of all those "internal functions" next to the two walruses of classes), renders fairly unreadable source code and is not that well extensible. Some of the syntax choices in smarty aren't that practical either... How many times have you typed {foreach $items as $value} in a Smarty template? I have about a gazillion times. And who the H thought of that abominable {section ...} thing?).
Of course, I also need to point out that it is actually quite fast, it makes for an excellent alternative to writing templates in pure PHP, and there has never been any template engine nearly as simple and at the same time powerful as Smarty. Not to mention the fact that it was actually the first one to come with a compiler paradigm. Well, I have used it for such a long time, I think that counts as enough defence.
Ever since PHP is evolving more and more into an object-oriented language, and even more so, the community is gaining more and more good developers, there was bound to be someone to say: "Smarties are for children, real men just gnaw on wood". So Twig is born. And a prodigy it is.
The set-up is very straight forward. A solid compiler, with great extensibility. You can hook into the compiler's syntax tree with your custom nodes, change the syntax by defining delimiters, easily read the generated code, and redesign every part of the engine because of it's design-by-contract implementation.
Here are some pointers if you're used to smarty:
Default delimiters, the $ sign and data types
Default, Twig uses {{ var }} delimiting for variable output, and {% ... %} for control structures. The dollar sign is a goner, so get used to that. There is no difference between an array and an object, so you can write {{ var.property }} for both arrays and objects. A slight difference is that in case var is an object, Twig also checks if the var has a getter for the property in either form property() or getProperty().
For-loops and sections
foreach is called for, and sections need to be rewritten to for-loops as well.
code:
Special context-sensitive variables are available in the loop-variable.
modifiers and functions
Twig supports Smarty-style modifiers, though parameters are passed within parentheses: {{ var|modify("with argument") }}. Functions are supported as well, and can be implemented by writing extensions to the compiler.
Cool stuff that smarty doesn't do
I hope a 1.0 version will see the daylight soon. I think it is a very welcome attribution to the community, so let's thank Armin Ronacher for kick-starting Twig, and Fabien for his conversion to the one true faith: PHP is no longer just a template engine.
PS: If the first question that comes to mind is Why use a template engine if you're already using PHP, never mind. There are plenty valid points to use a separate template engine. There a plenty valid points against it too. I don't really care. Smarty has done the job outstandingly, and as long as the deemed successor compiles into PHP code, I'm happy.
One of the major drawbacks of Smarty is that it is bloated (think of all those "internal functions" next to the two walruses of classes), renders fairly unreadable source code and is not that well extensible. Some of the syntax choices in smarty aren't that practical either... How many times have you typed {foreach $items as $value} in a Smarty template? I have about a gazillion times. And who the H thought of that abominable {section ...} thing?).
Of course, I also need to point out that it is actually quite fast, it makes for an excellent alternative to writing templates in pure PHP, and there has never been any template engine nearly as simple and at the same time powerful as Smarty. Not to mention the fact that it was actually the first one to come with a compiler paradigm. Well, I have used it for such a long time, I think that counts as enough defence.
Ever since PHP is evolving more and more into an object-oriented language, and even more so, the community is gaining more and more good developers, there was bound to be someone to say: "Smarties are for children, real men just gnaw on wood". So Twig is born. And a prodigy it is.
The set-up is very straight forward. A solid compiler, with great extensibility. You can hook into the compiler's syntax tree with your custom nodes, change the syntax by defining delimiters, easily read the generated code, and redesign every part of the engine because of it's design-by-contract implementation.
Here are some pointers if you're used to smarty:
Default delimiters, the $ sign and data types
Default, Twig uses {{ var }} delimiting for variable output, and {% ... %} for control structures. The dollar sign is a goner, so get used to that. There is no difference between an array and an object, so you can write {{ var.property }} for both arrays and objects. A slight difference is that in case var is an object, Twig also checks if the var has a getter for the property in either form property() or getProperty().
For-loops and sections
foreach is called for, and sections need to be rewritten to for-loops as well.
code:
1
2
3
| {% for key, value in items %}
{{ key }} contains {{ value }}
{% endfor %} |
modifiers and functions
Twig supports Smarty-style modifiers, though parameters are passed within parentheses: {{ var|modify("with argument") }}. Functions are supported as well, and can be implemented by writing extensions to the compiler.
Cool stuff that smarty doesn't do
- Template inheritance. Define a layout in one and populate the blocks in another template.
- Macro's, which are tiny pieces of template code, reusable as a function in the rest of the template. You can store macro's in external templates, and import them for reuse.
- Access a parent context reference, which enable you to identify for example which loop variable you want to access in a nested loop, the inner or the outer one.
- A generic expression parser, which enables you to set and define complex expressions wherever an expression is allowed
I hope a 1.0 version will see the daylight soon. I think it is a very welcome attribution to the community, so let's thank Armin Ronacher for kick-starting Twig, and Fabien for his conversion to the one true faith: PHP is no longer just a template engine.
PS: If the first question that comes to mind is Why use a template engine if you're already using PHP, never mind. There are plenty valid points to use a separate template engine. There a plenty valid points against it too. I don't really care. Smarty has done the job outstandingly, and as long as the deemed successor compiles into PHP code, I'm happy.
|
|
VirtualBox is more free than you might think |
Comments
What is its added value compared to what PHP brings by itself?
Still, i'm going to ask that question to you
. Templating engines only limit what you can do in your view, and that way you will always end up with template-related code in your controllers. If you use PHP itself, you can do anything that PHP can do.
Also see: http://nosmarty.net/
Also see: http://nosmarty.net/
So, err, why is this better than Smarty or basic inline PHP again? I wasn't able to discern that from the post.
There is no such thing as better, just different approach in separating data and html in a more frontend developer friendly way 
First off, I'm not telling anyone to use a template engine, I'm just saying that if you do, Twig is really something to look into.
For me there are two simple reasons. One is brevity, the other is simplicity. Regarding brevity, short tags in PHP are comparable, but imo not as simple or even short. That is a matter of taste, I agree. Secondly, I tend to have less trouble explaining Smarty to non-PHP front-end developers than Smarty PHP.freakingme
What is its added value compared to what PHP brings by itself?
I have never had that experience with Smarty, and since Twig seems more powerful and more easily extensible, I'm pretty sure I won't have that experience with Twig either.kokx
Still, i'm going to ask that question to you. Templating engines only limit what you can do in your view, and that way you will always end up with template-related code in your controllers. If you use PHP itself, you can do anything that PHP can do.
really, this is one of the lamest and most disrespectful flames I have read ever. Fortunately, they can't be taken seriously if they really suggest PHPTAL and XSLT as alternatives to Smarty. It surprises me they didn't suggest JSPAlso see: http://nosmarty.net/
[Comment edited on Wednesday 10 February 2010 09:55]
Getting late rightexplaining Smarty to ... than Smarty.
Thanks for the blog it's always nice to read about something other then the usual suspects ;-)
[Comment edited on Tuesday 09 February 2010 23:54]
@drm I am (and have been) using XSLT as my primairy template engine for quite a while and wouldn't have any arguments on why to change to something else, can you enlighten your opinion about the engine?
Indeedafvalzak: Getting late right
I'm not saying you need to change to something else. It is just not a fair comparison. XSLT is way more complex than Smarty, and the declarative approach is even harder to explain than plain PHP.SoaDmaggot: @drm I am (and have been) using XSLT as my primairy template engine for quite a while and wouldn't have any arguments on why to change to something else, can you enlighten your opinion about the engine?
To be frank, my opinion about xslt is that it is one of the greatest techniques out there. I have loved XSLT from the first moment I tried it, and actually used it as a template engine for some websites back in the php4 days. But an easy switch from Smarty to XSLT is just not realistic.
Ok, I misunderstood your previous comment about XSLT and Smarty not being comparable. I thought your comment was ment as a negative point towards XSLTdrm: I'm not saying you need to change to something else. It is just not a fair comparison. XSLT is way more complex than Smarty, and the declarative approach is even harder to explain than plain PHP.
To be frank, my opinion about xslt is that it is one of the greatest techniques out there. I have loved XSLT from the first moment I tried it, and actually used it as a template engine for some websites back in the php4 days. But an easy switch from Smarty to XSLT is just not realistic.