jknack / handlebars.java

Logic-less and semantic Mustache templates with Java
http://jknack.github.io/handlebars.java
Other
1.46k stars 381 forks source link

Extending the if/unless helpers #595

Closed ammmze closed 6 years ago

ammmze commented 7 years ago

I'm just barely starting to dig into this library, but i'm attempting to simulate the helpers that mandrill uses. Specifically, wanting to support their if expressions.

But haven't been able to get too far yet because I blow up with syntax errors with the backticks. I haven't written a helper yet since I need to get past the syntax piece first.

Here's my simple test.

assertEquals("Foo: is bar", handlebars.compileInline("Foo: {{#if `foo == "bar"`}}is bar{{/if}}").apply(new HashMap<String, String>(){{
            put("foo", "bar");
        }}));

Which yields this stack trace...

com.github.jknack.handlebars.HandlebarsException: inline@33185c00:1:11: found: '`'
Foo: {{#if `foo == "bar"`}}is bar{{/if}}
           ^

    at com.github.jknack.handlebars.internal.HbsErrorReporter.syntaxError(HbsErrorReporter.java:93)
    at org.antlr.v4.runtime.ProxyErrorListener.syntaxError(ProxyErrorListener.java:65)
    at com.github.jknack.handlebars.internal.HbsParserFactory$2.notifyListeners(HbsParserFactory.java:148)
    at org.antlr.v4.runtime.Lexer.nextToken(Lexer.java:168)
    at org.antlr.v4.runtime.BufferedTokenStream.fetch(BufferedTokenStream.java:185)
    at org.antlr.v4.runtime.BufferedTokenStream.sync(BufferedTokenStream.java:168)
    at org.antlr.v4.runtime.BufferedTokenStream.consume(BufferedTokenStream.java:152)
    at org.antlr.v4.runtime.Parser.consume(Parser.java:593)
    at org.antlr.v4.runtime.Parser.match(Parser.java:226)
    at com.github.jknack.handlebars.internal.HbsParser.sexpr(HbsParser.java:884)
    at com.github.jknack.handlebars.internal.HbsParser.block(HbsParser.java:657)
    at com.github.jknack.handlebars.internal.HbsParser.statement(HbsParser.java:336)
    at com.github.jknack.handlebars.internal.HbsParser.body(HbsParser.java:222)
    at com.github.jknack.handlebars.internal.HbsParser.template(HbsParser.java:165)
    at com.github.jknack.handlebars.internal.HbsParserFactory$1.parse(HbsParserFactory.java:84)
    at com.github.jknack.handlebars.cache.NullTemplateCache.get(NullTemplateCache.java:54)
    at com.github.jknack.handlebars.Handlebars.compile(Handlebars.java:414)
    at com.github.jknack.handlebars.Handlebars.compileInline(Handlebars.java:384)
    at com.github.jknack.handlebars.Handlebars.compileInline(Handlebars.java:368)
    at com.example.handlebars.HandlebarsImplTest.testIfExpression(HandlebarsImplTest.java:39)
ammmze commented 7 years ago

So far the easiest thing I found was in my project add the modified HbsLexer.g4 and HbsParser.g4 under the same path as in this project and add the necessary maven plugins to compile the files with antlr so that they take precedence over the HbsLexer.class and HbsParser.class provided by this project.

jknack commented 6 years ago

The reason why they don't exist is bc mustache/handlebars are logic-less and semantic template engines.

nakolkin commented 2 years ago

@ammmze could you elaborate on how you did this, please?

jknack commented 2 years ago

@nakolkin Look here https://github.com/jknack/handlebars.java#conditional-helpers and here: https://github.com/jknack/handlebars.java/blob/master/handlebars/src/main/java/com/github/jknack/handlebars/helper/ConditionalHelpers.java

ammmze commented 2 years ago

@nakolkin I can try...it's been about 5 years and I don't think we've touched it since it was set up. I'm assuming you are asking about the backtick syntax...But the gist is in my project I did the following:

ammmze commented 2 years ago

@jknack IIRC, I think my main intention with this issue request was really about supporting backticks as an alternative to quotes. I will admit, the issue wasn't super clear about that though. Would you be open to supporting backticks in this project?

nakolkin commented 2 years ago

Wow, @ammmze thank you a lot, that's super helpful. My main objective is I think quite similar to yours: to support Mandrill's if expressions to not have multiple versions of the same templates. I guess I'll need to do something more, to support <, >, >=, <= etc. Not sure how hard it is to implement, though. Just started to learn this lib.

ammmze commented 2 years ago

@nakolkin 👍🏻 , here's what I had done as far as helpers go (and creating a spring bean with the helpers registered). They probably are not perfect and are probably incomplete. We just use it for a small number of templates and for internal purposes and it has been sufficient for that.