arrow-down

Thymeleaf library

Contents

xp 7.+ blue

Thymeleaf library allows you to render templates using the Thymeleaf template engine

To start using this library, add the following into your build.gradle file:

dependencies {
  include 'com.enonic.lib:lib-thymeleaf:2.0.0'
}

Usage

To use this library in your JavaScript code, it first needs to be required:

var thymeleafLib = require('/lib/thymeleaf');

Then, you will need to resolve your template:

var view = resolve('view/test.html');

Template:

<div>
    <div data-th-each="fruit : ${fruits}">
        Name:
        <div data-th-text="${fruit.name}">Name</div>
        Color:
        <div data-th-text="${fruit.color}">Color</div>
    </div>
</div>

All the variables used in the template/view must be gathered into a JSON object:

var model = {
    fruits: [
        {
            name: 'Apple',
            color: 'Red'
        },
        {
            name: 'Pear',
            color: 'Green'
        }
    ]
};

Thymeleaf template mode might be set:

var options = { mode: 'HTML' }; // default mode

To process template:

var result = thymeleaf.render(view, model, options);

or simply:

var result = thymeleaf.render(view, model);

Output:

<div>
    <div>
        Name:
        <div>Apple</div>
        Color:
        <div>Red</div>
    </div>
    <div>
        Name:
        <div>Pear</div>
        Color:
        <div>Green</div>
    </div>
</div>

API

The following function is defined in this library.

render

Renders a thymeleaf template .

Parameters

  • view (object) Location of the template. Use resolve(..) to resolve a template.

  • model (object) Model used to render the template.

  • options (object) Optional Template mode. HTML is used by default. Valid options are: HTML, XML, TEXT, JAVASCRIPT, CSS and RAW.


Contents

Contents

AI-powered search

Juke AI