Here at SiteCrafting, we set out to compare different PHP template engines in order to find one that best suits our needs and can be adopted as a company-wide standard. Here is an account of our process and findings.

First, why use a template engine?

As we continue to develop complex web applications, it is becoming more important to have a wall of separation between application logic and display logic so that back-end developers and front-end developers can collaborate on the same areas of the website without having to step around each other’s code. A template engine solves this situation by providing a concise syntax front-end developers can use to display data prepared by back-end developers. The front-end developers experience the benefit of clean and pretty views they can read and modify, and in-turn the back-end developers can free themselves of the responsibility of maintaining the views. It’s win-win.

Which engines did we compare?

In order for an engine to qualify for our comparison process, it had to satisfy a few simple criteria. It had to be written for PHP, actively supported, and be generally well regarded by the internet community. After drafting an initial list and putting out a call to our team for further suggestions, we arrived at the following list of candidates:

1. Blade

This is the template engine used by Laravel 4, a PHP framework that began it’s life in 2011 and has quickly risen to become one of the top-used PHP frameworks in the world today. This template engine was rumored to be pretty fast due to the fact that it’s core runs on a short collection of regex replacements. Plus, as a part of Laravel it’s bound to be loaded with features.

2. Mustache

If you haven’t heard of mustache you’ve been hiding under a rock. It’s first defining attribute is that it’s available for nearly any language you can think of. Next, it is advertised as logic-less, which basically means that it only contains the absolute baseline of logic a template engine can have (token replacement, for each, if null). They leave it logic-less intentionally, perhaps for ease of portability to all those languages, but perhaps also to take the concept of clean views to an extreme for those who consider that their top priority.

3. Smarty

I had my first experience with a template engine back in 2002 when I was working with shopping cart software that used Smarty to render out all the screens and product listings, and for years afterward the name Smarty was practically synonymous with the term ‘template engine’. Fast forward to today and Smarty is still alive and kicking. While their website may still look like it did in 2002, the template engine itself is in active development with the OOP version 3.

4. Twig

Twig rose to popularity when Symfony PHP framework developer Fabien Potencier found an earlier iteration of the engine, took on ownership of the project, updated it to meet his vision, and integrated it as Symfony’s official view engine. Twig’s benefit lies not only in it’s pitch-perfect take on inheritance, extendability, and included functionality – but also in the fact that it is an independent component, meaning it can be used in any framework or free-standing PHP application.

5. Volt

This is the template engine used by Phalcon, the PHP framework that is lightning fast because it’s distributed as a C-compiled PHP extension. I have high hopes for the Phalcon PHP framework as a whole, so this was a great opportunity to see how it’s template engine stacked-up beside others.

What Did We Look at When Comparing?

We looked at many things including syntax, available logic, expandability, documentation, development activity, community support, and performance in a speed test. Our speed test compiled one main template containing a loop that included 1,000 extended templates with sequentially numbered content. Each extended template contained array tokens, object tokens, an if/else condition, and extended block content. Each test reported memory used and a final templates rendered per second calculation. In cases where the template engine was embedded in a framework (Blade, Volt), the framework’s memory usage and load-time were include in the result. Here is is a summary of how each engine stacked up:

Blade

Syntax & Functionality

There is no question that Blade is packed with functionality. However, I found myself a bit ‘on the fence’ with the syntax as I’m not completely sold on the look of the @’s for function calls. I could probably get used to the syntax, but I found the extendable section closure tags (show, hide, overwrite) confusing and simply couldn’t get an extended template to compile through an include – which seemed odd because all of the other engines handled it without a hitch.

Documentation & Support

Blade is pretty well documented, but I found it a tad ‘overview-ish’ as it leaves some of the finer points up to other websites (like Code Bright) and tutorials to explain in more detail.

Test Performance

This engine was highly intriguing during the test. Speed was screaming fast (100,000+ templates/sec) when only clocking template compilation, but as soon as I went to clock the whole framework it fell to last place (2,206 templates/sec). Without being sure how effectively one could extract Blade from Laravel, I had to include the framework load time in the test for the sake of safe decision making.

Conclusion

With questions about syntax, trouble compiling extended templates through include function, and having it bolted to Laravel 4, I didn’t see this as the right fit for us.

Mustache

Syntax & Functionality

Mustache sports as little logic as possible, which simply won’t fly very well with our development team. With only token printing, simple looping, and if null/empty comparisons, it would force back-end developers to create extra flags for whether or not to print certain things and take the ability to filter/escape values away from our front-end developers.

Documentation & Support

There isn’t much to the documentation, but that makes sense since the functionality is so limited. I’m guessing that once you learn a couple of basics there isn’t any need to review the documentation again.

Test Performance

Mustache is very fast (6,000 templates/sec) so no troubles there.

Conclusion

There is a lot to love about Mustache’s simplicity – and I know many people do, but in the end the lack of logic keeps it from being a viable option for us.

Smarty

Syntax & Functionality

The syntax looks great; it’s very clean and simple. Lots of good functionality, very extendable.

Documentation & Support

The documentation for Smarty is clear and well organized. I have no trouble finding answers to things when I go there. The website itself looks pretty dated and they have no github presence, but they seem to get along just fine.

Test Performance

Smarty is quite fast with non-cached templates (9,634 templates/sec) and abnormally fast when serving straight from cache (57,115 templates/sec). If speed is your priority and you have a huge number of static templates, Smarty could be for you.

Conclusion

It doesn’t feel as modern as other options, but it’s good functionality undeniable speed carried it into the final two.

Twig

Syntax & Functionality

Taking inspiration from the Jinja template engine for Python, Twig is clean and pretty to look at. It comes with a comprehensive set of functions, filters, tests, and macros that you can extend. It sports useful control over escaping, a sandbox mode, exception handling, and template loading.

Documentation & Support

The documentation is well organized, informative, and demonstrative. The community around Twig is large and vocal. Development is active on Github. The framework it calls home (Symfony 2) has been selected to power Drupal 8; the OOP release of the 2nd largest CMS in the world behind WordPress.

Test Performance

While not as fast as Smarty or the C-compiled Volt, Twig holds it’s own in terms of non-cached speed (4,318 templates/sec) and cached (5,982 templates/sec). This will be perfectly speedy for our needs.

Conclusion

Twig looks like a fine engine to adopt. As an independent package it can go anywhere you want to bring it. It’s documentation is on-point, the engine speed meets our demands, and it sports the functionality necessary to properly equip our front-end development team.

Volt

Syntax & Functionality

Like Twig, Volt also takes inspiration from the Jinja template engine, so the syntax is clean and pretty to look at. It comes packed with functionality, and allows you to code your own  filters, functions, macros, and engine extensions.

Documentation & Support

The documentation is clean, informative, and demonstrative. Development of the whole Phalcon framework is alive and buzzing on Github. The Phalcon community forum is also very active.

Test Performance

With this engine running in compiled-C, Volt’s non-cached speed soars into supersonic territory (23, 906 templates/sec) and doubles this speed when leveraging cache (47,851 templates/sec). Most impressively, it pulls those numbers with the entire Phalcon Framework stack load included. As you may recall, the other engine bolted to a framework (Blade) was roughly 20x slower than this after including it’s whole framework.

Conclusion

Volt is an impressive option. It is lightning fast, highly functional and well documented. It is also a non-portable engine (bolted to the Phalcon framework) which precludes it from being adopted at this time – but it’s striking similarities to Twig make it something worth a closer look in the future.

And the winner is…Twig

As a framework-independent technology, Twig is an engine we can adopt and hit the ground running with immediately. Twig is feature-rich, customizable, perfectly documented, and industry supported. It’s developer community is ablaze with enthusiasm. I am excited for us to begin using it.

And a bonus… it’s syntax is practically identical to that of Volt’s so if we end up adopting the C-compiled Phalcon framework in the future, it would be a smooth transition.

最后修改:2019 年 04 月 11 日
如果觉得我的文章对你有用,请随意赞赏