casid / jte

Secure and speedy templates for Java and Kotlin.
https://jte.gg
Apache License 2.0
748 stars 56 forks source link

Replace intercepted tag with different content #297

Closed dzikoysk closed 8 months ago

dzikoysk commented 9 months ago

I'm currently making research around some existing template engines and jte looks quite promising. One of my requirements is the ability to replace a custom tag with some other content, e.g.:

<Custom>
  <p>Text</p>
</Custom>

with:

<article>
  <section>
    <p>Text</p>
  </section
</article>

I was playing a little bit with html interceptor:

templatingEngine.setHtmlTags("custom");
templatingEngine.setHtmlInterceptor(new HtmlInterceptor() {
    @Override
    public void onHtmlTagOpened(String name, Map<String, Object> attributes, TemplateOutput output) {
        if (name.equals("Custom")) {
            output.writeUserContent("abc");
        }
    }
    @Override
    public void onHtmlTagClosed(String name, TemplateOutput output) {
        if (name.equals("Custom")) {
            output.writeUserContent("cdf");
        }
    }

but this API seems to be a little too simple for that use case. I wonder if there's some other way to achieve this kind of results.

casid commented 9 months ago

Not really.

The idiomatic jte way to do this is to call another template, e.g.

@template.custom(body = @`
    <p>Text</p>
`)