Knotx / knotx

Knot.x is a highly-efficient and scalable integration framework designed to build backend APIs
https://knotx.io
Apache License 2.0
126 stars 26 forks source link

Introduce Pebble template engine as faster & more feature rich alternative #477

Closed pun-ky closed 5 years ago

pun-ky commented 5 years ago

In Handlebars java, easy things are not easy. There are missing many helpers like for removing whitespaces, logical operators etc. The limited functionality of handlebars is ok in case of concerns separation (model & view layers) but... in a real/dev world when we use Knot.x to just pass the JSON feed data from 3rd party service and render it we need advanced logic in template engine (for now, implement plenty of handlebars helpers) or be forced to manipulate 3rd party service response before passing it to template engine which could cause more poor performance in case of need to buffer response, map feed objects to pojos etc.

Without a need to transform 3rd party responses and into desired format, such little logic could be covered by more advanced template engine (with var assignments, macros, inheritance and much more built-in helpers) with even better performance like Pebble Template Engine.

image

https://github.com/mbosecke/template-benchmark

Rocker is alternative tpl engine with even better performance, but it does not offer such many features like Pebble.

To cover my case, I would like to have an option to write (with no extra pebble extensions needed to be impl):

[
{
all_swatch_ids: "PX1|PC2|PG1"
all_swatch_links: "http://my-website.com/products/1|http://my-website.com/products/2|http://my-website.com/products/3"
all_swatch_images: "http://my-images.com/swatch1.jpg|http://my-images.com/swatch2.jpg|http://my-images.com/swatch3.jpg"
all_swatch_names: "Product 1|Product 2|Product 3"
description: "Sample product description"
title: "My product"
},
/* ... and more items */
]

then

{% for(item in items) %}
<div class="product">
{% for (swatch in swatches) %}
    {% set links = split(item.all_swatch_links) %}
    {% set ids= split(item.all_swatch_ids) %}
    {% set images= split(item.all_swatch_images) %}
    <ul class="product-swatches">
       <li class="product-swatch {% if loop.first %}is-active{% endif %}"><a href=""></a></li>
    </ul>
</div>
{% endfor %}

reference: https://pebbletemplates.io/wiki/tag/for/ https://pebbletemplates.io/wiki/tag/set/ https://pebbletemplates.io/wiki/filter/slice/

in handlebars it is not trivial to introduce new variable which values comes from custom helper to be able to use other helpers to consume a value

to sum up, more ways to cover edge cases in Knot.x templates make reduce costs of integrating 3rd party services and this is main motivation to introduce this issue.

current set of handlebars helpers is not good enough in my opinion. pebble just offers more built-in features and they are well tested. I'd like to avoid using untested helpers (be forced to implement my own for simple scenarios /yes it is ugly truth for now).

malaskowski commented 5 years ago

@pun-ky Thanks for that issue. This is actually more related to the Knot.x Template Engine module. Handlebars module from this repo is deprecated and will be removed in the next major release. This benchmark looks really promising. Will you be able to implement Pebble Template Engine strategy - here is guide how to and create a Pull Request?

pun-ky commented 5 years ago

of course, not now, but within next months, probably ;)

pun-ky commented 5 years ago

ok, so closing this issue here, and introducing there for better visibility.