Tweeg: a Twig to JS compiler

  • Published: 2017-03-05
  • Modified: 2017-03-05 12:47
  • By: Mishoo
  • Tags: javascript, compiler, php, twig
  • Comments: 1 (add)
Mar
5
2017

Tweeg: a Twig to JS compiler

Tweeg is a compiler for the popular PHP template engine Twig. It converts templates to JavaScript. Not being a PHP fan, to say the least, I don't particularly like Twig. But it does the job. At eMAG we're using PHP + Symfony on the server, and Twig is pretty much non-negotiable at this point.

Sometimes we need to render the same template on the server (on initial page load) and also on the client (when we update stuff via AJAX requests). So far, such cases were rare, so we wrote separate templates for JS using a slightly modified version of John Resig's micro-templating. It sucks, but it did the job. However, we will soon face the situation that we'll have to duplicate dozen of templates, just because Twig was not supported in JavaScript. So I took upon myself the lovely task of writing a Twig parser and compiler.

Halfway through, a colleague pointed out a similar package (twig.js). Tweeg was already better in a number of ways, so I didn't give up:

eMAG kindly agreed to open-source this package under the MIT license, so here it is. Usage details in the README.

PS: I would welcome a Gulp plugin for it. ;-)

One comment. ONE! Gimme some more!

Add your comment

# Oleh Kuchuk
2017-03-06 23:09
Nothing about Gulp but I will make Fly plugin. Btw, recommend to check it out as a slight lighter alternative to Gulp https://github.com/flyjs/fly