Skip to content

Knotx/knotx-template-engine

Repository files navigation

Build Status CodeFactor codecov Gradle Status

Template Engine

Knot.x Template Engine module is a Knot responsible for processing Fragment's body (treating it as a Template) and the data from Fragment's payload using chosen template engine strategy.

How does it work

Template Engine reads Fragment's body and treats it as a Template. It also reads Fragment's payload and uses the data within it to resolve placeholders from the Template. Finally it overwrites Fragment's body and returns it in the FragmentResult together with Transition.

Please note that example below uses Handlebars to process the markup. Read more about it below. You may read about it in the Handlebars module docs

Fragment's body

  <div class="col-md-4">
    <h2>{{_result.title}}</h2>
    <p>{{_result.synopsis.short}}</p>
    <div>Success! Status code: {{_response.statusCode}}</div>
  </div>

Fragment's payload

{
  "_result": {
    "title":"Knot.x in Action",
    "synopsis": {
      "short": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc vel enim ac augue egestas rutrum non eget libero."
    }
  },
  "_response": {
    "statusCode":"200"
  }
}

Fragment's body after processing

<div class="col-md-4">
  <h2>Knot.x in Action</h2>
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc vel enim ac augue egestas rutrum non eget libero.</p>
  <div>Success! Status code: 200</div>
</div>

How to use

Please note that example below uses Handlebars to process the markup. Read more about it below. You may read about it in the Handlebars module docs. For using a different template engine, refer to pebble module docs or core module docs.

Define a module that creates io.knotx.te.core.TemplateEngineKnot instance.

modules {
  myTemplateEngine = "io.knotx.te.core.TemplateEngineKnot"
}

Configure it to listen on some address and other things:

config.myTemplateEngine {
  address = my.template.engine.eventbus.address
  factory = handlebars
}

See the configuration docs for detailed configuration options.

In the Fragment's Handler actions section define a Template Engine Knot Action using knot factory.

actions {
  te-hbs {
    factory = knot
    config {
      address = my.template.engine.eventbus.address
    }
  }
}

Now you may use it in Fragment's Tasks.

Example configuration is available in the conf section of this module.

OOTB Template Engine Strategies

Currently this repository delivers handlebars and pebble TE strategies implementation. You may read more in the handlebars and pebble module docs.

How to create a custom Template Engine Strategy

Read more about it in the API module docs.

About

Template Engine processes Fragment's data and template into final markup

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published