Closed ammmze closed 6 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.
The reason why they don't exist is bc mustache/handlebars are logic-less and semantic template engines.
@ammmze could you elaborate on how you did this, please?
@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:
src/main/antlr4/com/github/jknack/handlebars/internal/HbsLexer.g4
)Applied the following to my copy of the HbsLexer.g4
(note: i'm diff'ing against v4.0.6):
--- ./handlebars/src/main/antlr4/com/github/jknack/handlebars/internal/HbsLexer.g4 2022-05-03 08:47:48.000000000 -0700
+++ ./custom/src/main/antlr4/com/github/jknack/handlebars/internal/HbsLexer.g4 2017-09-12 10:50:20.000000000 -0700
@@ -273,10 +273,15 @@
SINGLE_STRING
:
'\'' ( '\\\'' | ~[\n] )*? '\''
;
+BACKTICK
+ :
+ '`' ( '`' | ~[\n] )*? '`'
+ ;
+
EQ
:
'='
;
src/main/antlr4/com/github/jknack/handlebars/internal/HbsParser.g4
)Applied the following to my copy of the HbsParser.g4
:
--- ./handlebars/src/main/antlr4/com/github/jknack/handlebars/internal/HbsParser.g4 2022-05-03 08:47:48.000000000 -0700
+++ ./custom/src/main/antlr4/com/github/jknack/handlebars/internal/HbsParser.g4 2017-09-12 10:50:20.000000000 -0700
@@ -168,18 +168,19 @@
END_BLOCK nameEnd=QID END
;
pexpr
:
- LP sexpr RP QID? hash* #dynamicPath
- | path = (QID|PATH) QID? hash* #staticPath
- | path = (DOUBLE_STRING | SINGLE_STRING) QID? hash* #literalPath
+ LP sexpr RP QID? hash* #dynamicPath
+ | path = (QID|PATH) QID? hash* #staticPath
+ | path = (DOUBLE_STRING | SINGLE_STRING | BACKTICK) QID? hash* #literalPath
;
param
:
DOUBLE_STRING #stringParam
+ | BACKTICK #stringParam
| SINGLE_STRING #charParam
| INT #intParam
| BOOLEAN #boolParam
| QID #refParam
| LP sexpr RP #subParamExpr
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>${antlr.version}</version>
<configuration>
<libDirectory>src/main/antlr4</libDirectory>
<outputDirectory>target/antlr4</outputDirectory>
<visitor>true</visitor>
</configuration>
<executions>
<execution>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>add-antlr-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/antlr4</source>
<source>target/antlr4</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
@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?
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.
@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.
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.
Which yields this stack trace...