/twig.compiler.js

Twig.js compiler implementation. Convert twig templates to native JavaScript code.

Primary LanguageJavaScriptMIT LicenseMIT

Build Status

twig.compiler.js

Convert twig templates to native JavaScript code

IMPORTANT!

Simple example

template.twig

{% if foo %}{{ bar }}{% else %}foobar{% endif %}

template.js not minified

var t = function(ctx) {
  return (function(ctx) {
    var o = "";
    if ((ctx["foo"] !== undefined ? ctx["foo"] : "")) {
      o += (function(ctx) {
        var o = "";
        o += (ctx["bar"] !== undefined ? ctx["bar"] : "");
        return o;
      }(ctx) || "");
    } else {
      o += (function(ctx) {
        var o = "";
        o += "foobar";
        return o;
      }(ctx) || "");
    }
    return o;
  }(ctx) || "");
};

template.js uglified

var t = function(c) {
  return function(a) {
    var b = "";
    return b = void 0 !== a.foo && a.foo ? b + (function(a) {
      return a = "" + (void 0 !== a.bar ? a.bar : "")
    }(a) || "") : b + (function(a) {
      return "foobar"
    }(a) || "")
  }(c) || ""
};

Contributors

Licence & copyright

twig.compiler.js is copyright © 2015 Egor Sharapov and licenced under the MIT licence. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.