|
Reference
Reference for the language.
The language is very small.
Variable SubstitutionA variable is evaluated in a scoped context. A {.section} or {.repeated section} attempts to push the named context onto a stack. If the key is absent, empty, or null, then the section is not expanded into the output. To substitute a value foo, put it within metacharacters in the template: {foo} To substitute it, after running it through the html formatter, use this syntax: {foo|html} Example: The value of the variable foo, formatted as HTML is: {foo|html}When looking up a variable name, we start at the top of the stack of contexts. And then we simply look down the stack until we find it. If it's not found, then an UndefinedVariable exception is raised (or some other language-specific error). You may also set a default string value for undefined variables. A period performs looks up a field within a record (i.e. like a C struct). Example: {foo.bar.baz} will find the value Hello within {"foo": {"bar": {"baz": "Hello"}}}. foo will be searched for up the stack, but bar and baz are simple lookups. Built-in Directives
Clauses
Literals
FormattersYou can set the default formatter in the constructor of the Template class, or in the first few lines of the template, e.g default-formatter: html as the first line, with a blank line following it. Not all implementations must support all formatters. The user can supply their own formatters. These are reserved names -- implementations should not use them to produce other formats. Simple Formatters
Formatters For DictionariesFormatters are often used on "leaf" values in the JSON tree, but they also work on dictionaries and lists. The {pairs} formatter formats a dictionary so that its key and value are available as {@key} and {@value}. See testPairsFormatter in jsontemplate_test.py for an example. Formatters with ArgumentsUsing the more_formatters callback mechanism, it's easy to create formatters which take arguments.
Template OptionsIn the Python version, the arguments the Template constructor can be written in the template string itself. To construct a Template instance from a string with such options, use Template.FromString() or Template.FromFile(). Example: meta: {{}}
default-formatter: html
format-char: :
<must have a blank line here>
This is the template, and now your variables should look like this:
{{.section foo}}
{{bar:str}}
{{.end}}And if the formatter str is left off, then the html formatter is used by default. APIJSON Template is intended to be language-independent specification. This is why the Python tests run against both the Python and JavaScript implementations. At a mininum, the internal API should consist of:
A free function expand is a one-liner from there, and is also useful. The Python API includes more tools for processing template, including FromFile, FromString, and CompileTemplate. See RecommendedApi for more details. WhitespaceThe preferred mode of handling whitespace has the property that well-indented input produces well-indented output. In other words, if anything except a substitution or literal (e.g. {.space}) appears on a line by itself, then all the whitespace from the line is omitted from the output. Implementations of JSON Template may differ in whitespace, in recognition of the fact that this rule will lengthen the implementation (i.e. it's no longer sufficient to look at each token without context). In this case, the tests should be relaxed (using the language-specific verifier.py shim). | ||||||||||||||||||||||||||||||||||||||||||||
Hi, I was reading this document.
Tried testing template {foo.bar.baz}
with JSON {"foo": {"bar": {"baz": "Hello"}}}
It doesn't work. Version(R210)
What exactly does the following mean?
The character '@' is the cursor value. It can be used in a substitution, or as the section name of a repeated section.
Why do you need separate constructs for {.section} and {.repeated section}? You could take the absence of a key to signify "hide this section", its presence to signify "show it once, with these values", and its presence as an array to signifiy "show it multiple times, one per element". That's the way google-ctemplate works, and it seems to work quite well.
@Oleg I think it's mainly for backwards-compatibility reasons, see: http://groups.google.com/group/json-template/msg/d15ffd511dfc4dc4
How to do an if statement?
{.section foo=='bar'}
{.end} {.section foo=='barbara'} {.end}?
How I can add any directives in json-template (package template), to extend it ?
eshurmin: Please post your question to the group: http://groups.google.com/group/json-template