Search in sources :

Example 1 with JsonWriter

use of io.kestra.core.runners.pebble.JsonWriter in project kestra by kestra-io.

the class VariableRenderer method recursiveRender.

public String recursiveRender(String inline, Map<String, Object> variables) throws IllegalVariableEvaluationException {
    if (inline == null) {
        return null;
    }
    boolean isSame = false;
    String currentTemplate = inline;
    String current = "";
    PebbleTemplate compiledTemplate;
    while (!isSame) {
        try {
            compiledTemplate = pebbleEngine.getLiteralTemplate(currentTemplate);
            Writer writer = new JsonWriter(new StringWriter());
            compiledTemplate.evaluate(writer, variables);
            current = writer.toString();
        } catch (IOException | PebbleException e) {
            if (this.variableConfiguration.disableHandlebars) {
                if (e instanceof PebbleException) {
                    throw properPebbleException((PebbleException) e);
                }
                throw new IllegalVariableEvaluationException(e);
            }
            try {
                Template template = handlebars.compileInline(currentTemplate);
                current = template.apply(variables);
            } catch (HandlebarsException | IOException hbE) {
                throw new IllegalVariableEvaluationException("Pebble evaluation failed with '" + e.getMessage() + "' " + "and Handlebars fallback failed also  with '" + hbE.getMessage() + "'", e);
            }
        }
        isSame = currentTemplate.equals(current);
        currentTemplate = current;
    }
    return current;
}
Also used : IllegalVariableEvaluationException(io.kestra.core.exceptions.IllegalVariableEvaluationException) PebbleException(com.mitchellbosecke.pebble.error.PebbleException) StringWriter(java.io.StringWriter) PebbleTemplate(com.mitchellbosecke.pebble.template.PebbleTemplate) IOException(java.io.IOException) JsonWriter(io.kestra.core.runners.pebble.JsonWriter) StringWriter(java.io.StringWriter) JsonWriter(io.kestra.core.runners.pebble.JsonWriter) Writer(java.io.Writer) PebbleTemplate(com.mitchellbosecke.pebble.template.PebbleTemplate) Template(com.github.jknack.handlebars.Template)

Aggregations

Template (com.github.jknack.handlebars.Template)1 PebbleException (com.mitchellbosecke.pebble.error.PebbleException)1 PebbleTemplate (com.mitchellbosecke.pebble.template.PebbleTemplate)1 IllegalVariableEvaluationException (io.kestra.core.exceptions.IllegalVariableEvaluationException)1 JsonWriter (io.kestra.core.runners.pebble.JsonWriter)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1