Templates

Last updated: oct.13, 2016 - 12:04:35

Overview

This chapter covers how to create and handle Templates.

Templates are datas that may contain :

  • hard coded data (ex: "value1=my value")
  • keys using mustache template (keys will be valuated in a specific context, namely the applications)

Templates can be created at two levels :

  • modules
  • technos, which are inherited at the module level

Working copy and release

Modules and technos (described below) contain templates and are associated with a version. Hesperides use another concept for modules and technos : the type. A type can be either "working copy" or "release". It can be compared with "snapshot" and "release" from Maven.

Working copy

This type allows you to continue editing the module/techno.

You can verify that you are currently editing a working copy with this element :

When you are satisfied with datas inside templates, you can release the version of your module/techno.

Release

Type "release" identifies a module/techno that can not be changed.

You can create the release by :

  1. clicking on "Create release" button
  2. fulfill version (1) you want to release and clicking on "Create" button (2)

Modules

Module is the first and mandatory level to handle with templates.

In this section, you will learn how to manage modules, and how to manage templates inside modules.

Create a module

Before managing templates, you have to create a module. To do that, get in "By module" menu and clic on "Create a working copy" :

You can now just fulfill the module name (1), the version (2) and click on "create" button (3) :

Congratulations !

You created your first working copy module ! You can now start to learn how to handle with templates

Create a module from

Instead of creating a module from scratch, you can create a module from an existing one. To do that, get in "By module" menu and clic on "Create a working copy from..." :

You can now just fulfill the module name (1), the version (2) and use the autocomplete field to find which module you want to copy (3) - then hit the "create" button (4):

Search module

After creating modules, you can edit them using the autocomplete input "Search" :

Useful tip !

Use the power of autocomplete

Techno

Techno is a kind of module that contains templates, but that can be inherited in a module.

Create a techno

To create a techno, get in "By techno" menu and clic on "Create a working copy" :

You can now just fulfill the techno name (1), the version (2) and click on "create" button (3) :

Congratulations !

You created your first working copy techno ! You can now start to learn how to handle with templates

Create a Techno from

Instead of creating a techno from scratch, you can create a techno from an existing one. To do that, get in "By techno" menu and clic on "Create a working copy from..." :

You can now just fulfill the techno name (1), the version (2) and use autocomplete field to find which techno you want to copy (3) - then hit the "create" button (4):

Search techno

After creating technos, you can edit them using the autocomplete input "Search" :

Useful tip !

Use the power of autocomplete

Template

As explained quickly earlier, templates are some kind of files created inside a module or a techno, that contain plain text or templated values using mustache.

Important Notice

Each time a templated value can be used, you have acces to a set of predefined valuation. Pay attention to them to prevent duplicate.

Add template

In either modules or technos, you can add template by clicking the "Add a template" button:

Template attributes

When you click on "Add template" button, you have to fill in several informations :

  • Title
    unique key used by Hesperides to identified the template inside a module or techno
  • Name of file
    string formed with templated values or hard coded datas (ex: foo{{user}}.properties)
  • Destination folder
    string formed with templated values or hard coded datas (ex: /HOME/{{user}})
  • User and Group
    handle with if you want to assign specific rights for the generated file

Template content

All you have to do is to define the content of your template and it can be anything you want : key-value, json, plain text, xml etc ....

Inside the content, you can put some hard coded datas, or use mustache framework.

Example 1 :
key1=hard_coded_value
key2={{templated_value}}
Example 2 :
<logger name="com.mkyong.web" level="{{level}}"
        additivity="{{additivity}">
        <appender-ref ref="STDOUT" />
</logger>  

Note : Properties' name can contain whitespaces at the start and the end.
Example :
key = {{ key1 }} is the equal to key = {{key1}}

They can also be dotted like this
username = {{ mysql.user.name }}

Annotations

We add some specifics annotations

  • @required
    during valuation, the value of the property must not be empty
  • @comment "comment"
    during valuation, "comment" will be display in tooltip when hovering the key with mouse
  • @default "value"
    without valuation, "value" will be considered
  • @password
    in production mode, an non authorized user will not see the value of property
  • @pattern
    the property have to fulfill the pattern
Examples :
key1=hard_coded_value
required_key={{value1|@required}}
key_with_comment={{value2|@comment "some comment to explain the key"}}
key_with_default_value={{value3|@default "false"}}
key_password={{value4|@password}}
key_with_pattern={{value5|@pattern "[a-z]*"}}

Iterables Properties

Now that you're comfortable with "simple properties", you can handle with iterables properties. With this new concept, you can iterate on blocks to avoid repeating templated values.

Let's approve that with an example : we want to generate an html list of shoes with the brand and bar code

To define iterable block, use {{#FOO}} to start block and {{/FOO}} to end it :

<html>
<body>
    <ul>
        {{#ITERATE_ON_SHOES}}
        <li>{{brand}} has bar code : {{barcode}} </li>
        {{/ITERATE_ON_SHOES}}
    </ul>
</body>
</html>

Go to valuation section to see how to fulfill values for iterable properties.

Mustache info

Iterable block concept is directly defined by mustache framework

Full example