All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.soffa.foundation.commons.TemplateHelper Maven / Gradle / Ivy

package dev.soffa.foundation.commons;

import com.google.common.collect.ImmutableMap;
import com.mitchellbosecke.pebble.PebbleEngine;
import com.mitchellbosecke.pebble.template.PebbleTemplate;
import dev.soffa.foundation.error.TechnicalException;
import dev.soffa.foundation.model.TemplateMessage;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;

public final class TemplateHelper {

    private static final PebbleEngine PEBBLE = new PebbleEngine.Builder().build();

    private TemplateHelper() {
    }

    public static String render(TemplateMessage message) {
        return render(message.getTemplate(), message.getValues());
    }

    public static String render(InputStream template, Map context) {
        String content = IOUtil.toString(template).orElseThrow(() -> new TechnicalException("Error while opening template"));
        return render(content, context);
    }

    public static String render(PebbleEngine engine, InputStream template, Map context) {
        String content = IOUtil.toString(template).orElseThrow(() -> new TechnicalException("Error while opening template"));
        return render(engine, content, context);
    }

    public static String render(File template, Map context) {
        try (InputStream is = Files.newInputStream(Paths.get(template.toURI()))) {
            return render(is, context);
        } catch (IOException e) {
            throw new TechnicalException("Error while rendering template", e);
        }
    }

    public static String render(String template, Map context) {
        if (context == null) {
            return render(PEBBLE, template, ImmutableMap.of());
        }
        return render(PEBBLE, template, context);
    }

    public static String render(PebbleEngine engine, String template, Map context) {
        PebbleTemplate compiledTemplate = engine.getLiteralTemplate(template);
        Writer writer = new StringWriter();
        try {
            compiledTemplate.evaluate(writer, context);
            return writer.toString();
        } catch (IOException e) {
            throw new TechnicalException("Error while rendering template", e);
        }
    }

}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy