A simple example using python and jinja2 template engine, I’m evaluating it for doing some metaprogramming (generating php/html and javascript from a json).
I evaluated also fmpp/freemarker but jinja2 is just so simple and its syntax doesn’t conflict with php/html/javascript/css.
test.py
:
from jinja2 import Template
import json
rootJson = json.load(open('test.json', 'r'))
tpl = Template(open('test.jinja', 'r').read().decode('utf-8'))
print tpl.render(root=rootJson).encode('utf-8')
test.json
:
{
"test": "valueèè&&"
}
test.jinja
:
Hello {{ root.test|e }}
I thought would be nice to have a template engine with the same syntax for both Php and Javascript, I’m thinking about a website which could work on Php only then scale with Javascript, below a list of something that already exist:
So, mustache seems to be most interesting, I’ll give a try sooner or later, in the while I been tempted (still) to write my own, inspired by something simple like JavaScript template engine in just 20 lines and JavaScript Micro-Templating. The target would be a template engine with the same exact syntax for both Php and Javascript, a generator, script or command, which given a template would create a function for both the languages (eventually any language introducing a output backend concept).
Doing some fast test immediately shown an horrific implementation design, Javascript’s (json) objects are unsorted (arrays are sorted).