KD2 Framework  Smartyer

So, what is KD2\Smartyer?

  • A lightweight template engine, small library, contained in a single file (27 KB)
  • No dependencies
  • Fast, low memory usage
  • Compact compiled templates
  • For PHP 5.4+
  • Compatible with most of Smarty and Template_Lite out of the box (API and template syntax)
  • BSD licensed
  • Auto-escaping of variables
  • Optional template inheritance
  • No use of eval(), only using includes: better use of opcache
  • Allows more PHP syntax in blocks, eg. use of objects, static class calls, constants, etc.
  • Optional support for gettext and translation with KD2\Translate, see Translate with Smartyer

What does Smartyer doesn't do, compared to Smarty?

  • No cache support, as using file cache is quite slow. Smartyer is built for dynamic apps, you should use APCu, Redis or Memcache to cache your data before feeding it to the template, or cache your template output manually.
  • Smartyer doesn't care about security: there is no way to disable <?php tag rendering, and you can probably put any dangerous PHP code in block tags. So you should not give the ability to anyone to edit your Smartyer templates. If you want to do that, use another template engine, like Mustachier
  • Smartyer doesn't try to check your PHP code: if there is an error in your code, your template will fail to compile and throw an exception

Why not use Twig, Blade, Smarty 3, etc.?

Twig and Blade are quite slow, hard to master, and have a very large code base that you can't understand quickly.

Plus they are not compatible with Smarty syntax, so you would have to rewrite thousands of templates. And their syntax is very cumbersome to use compared to Smarty.

Smarty 3 is a good library, with modern code, but is quite slow and memory heavy, but allows a lot more than what Smartyer does.

How fast is Smartyer?

In a simple "Hello {$world|escape}!" benchmark performed on a Core i5-4278U with 8GB of RAM running Debian Jessie and PHP 5.6.22 in CLI, comparing Smartyer with Dwoo, Smarty 2, Smarty 3, Brainy, Twig, Blade and Template_Lite:

  • Smartyer was up to ten times faster and 17 times more memory efficient than Smarty 3 at compile time
  • Smartyer was 3 times faster and 4 times more memory efficient than Smarty 2
  • Twig was 10 times slower, and used 11 times more memory. It also included more than 60 files (!), filling PHP opcode cache very quickly.
  • Blade was 6 times slower and used 7 times more memory that Smartyer.
  • Compared to all the other engines tested, Smartyer was the fastest and used the less memory. It also was the one that included the less files.

More details on that benchmark can be found here: http://bohwaz.net/Smartyer-a-faster-alternative-to-Smarty-template-engine

Who is using Smartyer?

Smartyer is actively used on https://paheko.cloud/ and other projects, and is currently serving millions of pages every day.