Mustache Logicless Template Engine
I worked on a project a few years back where I worked on a UI which allowed the user to generate a bunch of predefined named formulas. I created a templating engine that allowed the user to define new templates where they could specify what calculation to generate and how to name the calculation. The user could also specify the inputs to the calculation and the application would dynamically generate the UI for specifying the calculation expressions.
I had templates for generating the UI
"Moving total (sum) of {1} in the {2} dimension and {3} hierarchy. Include {4} preceding and {5} following members within {6}."},
I had template for generating the name
"{measure_name} || '_RANK_' || is_selected({hierarchy_level}, {hierarchy
_level_name}, {dimension_name} || '_' || {rank_within_name})"
I needed to do something similar for another project that I was working on but I needed the template to work in Java and Javascript. Mustache is a logic less template engine that solves this problem. Mustache supports if statements, loops and callbacks and is very versatile.
A typical Mustache template:
Hello {{name}}
You have just won {{value}} dollars!
{{#in_ca}}
Well, {{taxed_value}} dollars, after taxes.
{{/in_ca}}
Given the following hash:
{
"name": "Chris",
"value": 10000,
"taxed_value": 10000 - (10000 * 0.4),
"in_ca": true
}
Will produce the following:
Hello Chris
You have just won 10000 dollars!
Well, 6000.0 dollars, after taxes.
Comments