Skip to content

Latest commit

 

History

History
executable file
·
101 lines (64 loc) · 3.62 KB

template_guide.md

File metadata and controls

executable file
·
101 lines (64 loc) · 3.62 KB

Template Author's Guide

This document details how to use Rythm to write template source.

[introduction]Introduction

Rythm Template Engine is a text generator that merges dynamic content into static template.

java-version

In order to use Rythm to generate a text file, the essential work is to create template file which write down the static part and use Rythm syntax to define the dynamic parts. It is also important to understand that a template file has one or more arguments which is the origin of the dynamic content. The Java program pass parameters to template file via arguments to generate different result.

So a template file is a text file, some parts of which have placeholders for dynamically generated content. The template’s dynamic elements are written using the Java language.

Dynamic elements are resolved during template execution. The rendered result is then sent as part of the HTTP response.

[at]The magic @

The Rythm template syntax is designed to be easy to learn and what you need to understand is one fact:

The magic @ character is used to lead all rythm element.

If you want to output a literal @ character, you need to double it. E.g.
This is an email address in template: someone@@gmail.com

[static-content]Static content

All static content (those text not defined in Rythm elements including script blocks) are output literally including whitespace and new lines.

When a template part is executed in compact mode, the additional whitespace and new lines are removed. See Compact output

[comment]Comment

one line comment start with @//

@// this is one line comment

multiple lines comment are put inside @* and *@ block.

@*
    this is a multiple line comment.
    The text inside this block will be ignore by Rythm template processor
*@

[expression]Expression

Output expression is the core function of Rythm template, it is used to emit dynamic content passed into the template, including emission of object fields and methods, class fields and methods:

@args User user, Foo foo
  
// emit instance field
@foo.bar

// emit instance method
@user.getName()

// emit class field
@Long.MAX_VALUE
@Integer.MAX_VALUE

// emit class method
@Boolean.parseBoolean("true")

The bracket ( ) can be used to compose complicated expressions or to separate an expression from other part of the template:

@(1 + 5) @// print out the result of 1 + 5

@// use ( ) to separate expression from other part of the template
@(foo.bar)_and_some_other_string 

Source of expression

As shown in the above example, the instances/classes/fields/methods inside an expression could come from anyone of the followings:

See also

[invoke]Invoke template

TBD