With readers

Macros with readers use data from a file and give it to a template to create a formatted output.

Macro

Description

{@marker-props:template_context:
template_name:properties_file}

Creates an Elephant marker within context with name. The properties_file is stored in props.

{@marker-xml:template_context:
template_name:xml_file}

Creates an Elephant marker within context with name. The xml_file is stored in doc.

{@marker-json:template_context:
template_name:json_file}

Creates an Elephant marker within context with name. The json_file is stored in json.

How does it work?

The easiest way to read a file is store it in context's repository and access it with */file. The * will expand into rendering context repository. For example:

{@marker-xml:xml:doc:*/doc.xml}

Reads doc.xml in current repository and passes its data to [Template_root]/xml/docTemplate.html to be formatted.

Reading properties

Properties are stored in props list in the same order they are stored on disc. You can iterate props with #list.

<#list props?keys as key>
   <#assign value=props[key]/>
   // do something with key and value
   ...
< /#list>

Reading XML

XML documents are stored in doc model. You can iterate the elements using dot syntax and iterate collections as you would usually do for any other data.

Reading JSON

JSON files are stored in json model. To access a root, use json.read("$.root"). If the root is a list, use json.list("$.root"). Once retrieved the root, you can iterate the elements using dot syntax and iterate collections as you would usually do for any other data.

The JSON books example

The data:

{
  "book": [
    {
      "title": "...",
      "author": "...",
      "excerpt": "...",
      "availability": "...",
      "link": "...",
      "filelink": "...",
      "imagelink": "...",
      "pages": 0
    },
    {
      ...
    },
    ...
  ]
}

The template (Semantic-UI):

<div class="ui divided items">
  <#list json.list("$.book") as book>
    <div class="item">
      <a class="ui tiny image" href="${book.link}">
        <img src="${book.imagelink}"/>
      </a>
      <div class="content">
        <a class="header" href="${book.link}">${book.title}</a>
        <div class="meta">
          ${book.author}
        </div>
        <div class="description">
          ${book.excerpt}
        </div>
        <div class="extra">
          ${book.availability}
          <#if book.filelink?has_content>
            <a target="_new" href="${book.filelink}">${book.pages}</a>
          < /#if>
        </div>
      </div>
    </div>
  < /#list>
</div>
Printer version
English04/24/16 13:06Lluís Turró Cutiller
English12/29/17 09:48Lluís Turró Cutiller
English01/08/18 10:25Lluís Turró Cutiller