- Every json is a valid template in itself.
- The templates can have expressions, which can execute javascript.
- These templates are compiled in context of a scope.
- Directives can pass instructions to compiler.
- Inbuilt directives for iterating, conditional contents, including other templates etc.
- Custom directives can replace/modify template body, execute expressions, read other templates.
var jeyson = require('jeyson').create(),
scope = {message: 'Hello!'},
templateJson = {"message": "{{message}}"},
compiled = jeyson.compile(scope, templateJson);
An expression is defined as '{{expr}}'
Following template.json :
{
"age": "{{21 + 33}}"
}
is compiled to
{
"age": 54
}
Any valid javascript snippet is a valid expression, e.g following template.json :
{
"list" : "{{'one,two,three,four,five'.split(',')}}",
}
is compiled to
{
"list" : ["one", "two", "three", "four", "five"],
}
Any field on scope object, is available as local variable in expressions.
e.g. Given scope defined as :
var scope = {message: "Hello !"},
then following template.json :
{
"message": "{{message}}"
}
is compiled to
{
"message": "Hello !"
}
e.g. Given scope defined as :
var scope = {
message: function(){
return "Hello !";
}
};
then following template.json :
{
"message": "{{message()}}"
}
is compiled to
{
"message": "Hello !"
}
- Directive is a field with name starting with "@"
- Directive body is the parent subtree of directive field
- Directive argument is the value of directive field.
E.g.
{
"list" : {
"@repeat" : "name in ['one','two','three','four','five']",
"id" : "{{$index + 1}}",
"name" : "{{name}}",
}
}
- "list" value is directive body (has a directive child)
- @repeat is directive name
- count in [1,2,3,4,5] is directive argument
It will compile to following json :
{
"list": [ {"id": "1","name" : "one"},
{"id": "2","name" : "two"},
{"id": "3","name" : "three"},
{"id": "4","name" : "four"},
{"id": "5","name" : "five"}];
}
Following are the conditional directives
- switch
- if
- ignore-if (a system directive)
scope = {role : {name: "admin"}}
template = {
"user" : {
"role" : {
"@switch" : "role.name",
"@admin" : {"name" : "admin"},
"@visitor" : {"name" : "visitor"},
"@default" : {"name" : "guest"}
}
}
Will compile to
template = {
"user" : {
"role" : {"name" : "admin"}
}
{
"author" : "ABC",
"book" : {
"@if" : "1 == 2",
"@then" : {"title" : "Harry Potter"}
"@else" : "unknown",
}
}
will result in
{
"author" : "ABC",
"book" : "unknown"
}
{
"author" : "ABC",
"book" : {
"@if" : "1 == 1",
"@then" : {"title" : "Harry Potter"}
}
will result in
{
"author" : "ABC",
"book" : {"title" : "Harry Potter"}
}
{
"author" : "ABC",
"book" : {
"@if" : "1 == 2",
"@then" : {"title" : "Harry Potter"}
}
will result in
{
"author" : "ABC",
"book" : null
}
e.g
{
"author" : "ABC",
"book" : {
"@ignore-if": "1 == 1",
"title" : "Harry Potter"
}
}
will result in
{
"author" : "ABC"
}
Inbuilt directives for repeating, including other json etc.
- $index
- array
- result
- Reading scope
- Executing expressions
- Modifying body
- Replacing body
- Reading other templates by relative path