JSON-T Directives
Directives in JSON-T are like commands. They tell the renderer how to render a section of JSON. You can identify them because they use an extra dot, and they often come in pairs (ex.{.section foo}).
Section
Sections do most of the work in JSON Template. They allow you to "zoom in" on a part of the JSON context, removing duplicated dot notation prefixes throughout a section of code. Here are two examples, one using a section and one without:
Code
There are two things you need to know about Section and Repeated Section:
- The content inside a section will only display if the section exists
- Sections define scope, meaning they set the root for any data tags within
Repeated Section
Code
NOTE: Inside a repeated section you can number each item with an instance of {@index} which outputs the current index of the item. Index numbers start with 0.
Or Clause
You can use an or statement within a section or repeated section to display something if the condition of the section or repeated section is not met ... for example, if the section is missing or false:
Code
Alternates With Clause
Within a repeated section, the alternates with allows you to insert delimiters between each item that is repeated. Handy for commas, slashes or even horizontal rules, this block is expanded in between every pair of repeating sections.
Code
Var Directive
The var directive allows you to temporarily store a value for reference later. This can be useful along with sections to keep code concise and legible.
Code
A var allows you to create a variable and then use it later within any section or repeated section within the current JSON-T file. Here's an example:
Code
Comment Directive
Occasionally, you may want a comment that would not end up rendering in your HTML. This JSON-T comment is going to accomplish that for you. There are two types of comments available, single line and multiline.
Code