Template plugin

The template plugin is a new addition to XILL IDE 3.5.x. It enables you to generate (textual) files using a template file and data. This plugin enbales you to decorate data using special template files. The plugin is build on the Freemarker library. If want to explore the possibilities of this library, take a look at http://freemarker.org/

Example

This is a simple example that demonstrates how to generate a simple html page based on data from the api of tvmaze.com (http://www.tvmaze.com/api)

This api can retrieve information formatted as json. For example: this link will return all information known about the Homeland TV series: http://api.tvmaze.com/singlesearch/shows?q=Homeland&embed=episodes

Take a look at the data returned from this link:

{
  "id": 7,
  "url": "http://www.tvmaze.com/shows/7/homeland",
  "name": "Homeland",
  "type": "Scripted",
  "language": "English",
  "genres": [
    "Drama",
    "Thriller",
    "Espionage"
  ],
  "status": "Running",
  "runtime": 60,
  "premiered": "2011-10-02",
  "schedule": {
    "time": "21:00",
    "days": [
      "Sunday"
    ]
  },
...

Next we can create a template that uses the keys with the ${...} notation. For example:

<!DOCTYPE html>
<html>
<body>
  <h1>${name}</h1>
  ${summary}
</body>
</html>

When we save this template with the name template.ftlh, we can create a robot to combine this template with the previous data:

use Template, File, XURL;

// Retrieve information about series
var result = XURL.get("http://api.tvmaze.com/singlesearch/shows?q=Homeland&embed=episodes");

// Create the templating engine
var engine = Template.getEngine();

// Open a file for writing
var output = File.openWrite("output.html");

// Combine the data and the template into the output file
Template.process("template.ftlh", output, result.body, engine);

Running this script will get us the following result in output.html:

<!DOCTYPE html>
<html>
<body>
  <h1>Homeland</h1>
  <p>The winner of 6 Emmy Awards including Outstanding Drama Series, <strong>Homeland</strong> is an edge-of-your-seat sensation. Marine Sergeant Nicholas Brody is both a decorated hero and a serious threat. CIA officer Carrie Mathison is tops in her field despite being bipolar. The delicate dance these two complex characters perform, built on lies, suspicion, and desire, is at the heart of this gripping, emotional thriller in which nothing short of the fate of our nation is at stake.</p>
</body>
</html>


As you can see, the placeholders ${name} and ${summary} are replaced with the values from the data.

We also can use some iteration logic in the template. As the data contains all kind of information about every episode of the series, we can iterate over the episodes to compose a table:

<!DOCTYPE html>
<html>
<body>
  <h1>${name}</h1>
  ${summary}
  <h2>episodes</h2>
  <table>
    <thead>
      <tr><td>Season</td><td>Episode</td><td>Title</td><td>Airdate</td><td>Summary</td></tr>
    </thead>
    <tbody>
      <#list _embedded.episodes as episode>
        <tr><td>${episode.season}</td><td>${episode.number}</td><td>${episode.name?html}</td><td>${episode.airdate}</td><td>${episode.summary!}</td></tr>
      </#list>
    </tbody>
  </table>
</body>
</html>

When you run the same robot again, the output.html file will contain additional information about the episodes formatted in a table.

There are two special suffix notations used to this example:

  1. ${episode.name?html} The ?html suffix instructs the engine to html escape a string. This means a < is converted into &gt;
  2. ${episode.summary!} The ! suffix instructs the engine to show the value if it is available. Otherwise nothing is show. If this suffix is ommmited, an exception will be thrown explaining that a value is not available and the proces will terminate.

The freemarker library does provide a lot more possibilities. See http://freemarker.org/ for more information.