Using JS string interpolation inside @raw in template content throws "Unexpected end of template expression"
See original GitHub issueHey,
I was trying to @raw
to include a script in my page, which was part of a template (a basic root template with <html>
tags, etc). Inside the script block I used JS string interpolation e.g. console.log(`hello ${name}`)
. Unfortunately this threw an exception, detailed below.
Minimal reproducable example:
testroot.jte
:
@import gg.jte.Content
@param Content content
<head>
<title>Test</title>
</head>
<body>
<div class="content">
${content}
</div>
</body>
testraw.jte
:
@template.testroot(
content = @`
<p>Hello World!</p>
@raw
<script>
const hello = "Hello!"
console.log(`${hello}`)
</script>
@endraw
`
)
(In the real example, I had included the script as a separate parameter passed to the template, which also failed)
Would you happen to know what’s going on here? Thanks!
Exception encountered:
gg.jte.TemplateException: Failed to compile testraw.jte, error at line 7: Unexpected end of template expression
at gg.jte.compiler.java.JavaCodeGenerator.onError(JavaCodeGenerator.java:221)
at gg.jte.compiler.TemplateParser.extractTextPart(TemplateParser.java:446)
at gg.jte.compiler.TemplateParser.doParse(TemplateParser.java:359)
at gg.jte.compiler.TemplateParser.parse(TemplateParser.java:94)
at gg.jte.compiler.java.JavaCodeGenerator$ContentProcessor.writeJavaCode(JavaCodeGenerator.java:573)
at gg.jte.compiler.java.JavaCodeGenerator$ContentProcessor.process(JavaCodeGenerator.java:547)
at gg.jte.compiler.java.JavaCodeGenerator.writeJavaCodeWithContentSupport(JavaCodeGenerator.java:429)
at gg.jte.compiler.java.JavaCodeGenerator.appendParam(JavaCodeGenerator.java:482)
at gg.jte.compiler.java.JavaCodeGenerator.appendParams(JavaCodeGenerator.java:470)
at gg.jte.compiler.java.JavaCodeGenerator.onTemplateCall(JavaCodeGenerator.java:388)
at gg.jte.compiler.TemplateParser.lambda$doParse$3(TemplateParser.java:257)
at gg.jte.compiler.TemplateParser.extract(TemplateParser.java:947)
at gg.jte.compiler.TemplateParser.doParse(TemplateParser.java:257)
at gg.jte.compiler.TemplateParser.parse(TemplateParser.java:94)
at gg.jte.compiler.TemplateParser.parse(TemplateParser.java:89)
at gg.jte.compiler.TemplateCompiler.generateTemplateCall(TemplateCompiler.java:299)
at gg.jte.compiler.TemplateCompiler.generate(TemplateCompiler.java:218)
at gg.jte.compiler.TemplateCompiler.precompile(TemplateCompiler.java:142)
at gg.jte.compiler.TemplateCompiler.load(TemplateCompiler.java:45)
...
Issue Analytics
- State:
- Created a year ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Template literals (Template strings) - JavaScript | MDN
Template literals are literals delimited with backtick (`) characters, allowing for multi-line strings, string interpolation with embedded ...
Read more >Why do I get an unexpected template string expression error?
It can be easy to use the wrong quotes when wanting to use template literals, by writing "${variable}" , and end up with...
Read more >String Interpolation in JavaScript - Dmitri Pavlutin
The string interpolation in JavaScript is performed by template literals (strings wrapped in backticks ` ) and ${expression} as a ...
Read more >Understanding Template Literals in JavaScript - DigitalOcean
In this article, you will go over the differences between single/double-quoted strings and template literals, running through the various ways ...
Read more >Razor syntax reference for ASP.NET Core - Microsoft Learn
Learn about Razor markup syntax for embedding server-based code into webpages.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hey @casid, thanks for the prompt fix! I have tested it out locally and the fix works a charm.
I might have explained poorly - the other example I was still using
@raw
, just passing it as another param in the template instead of content. E.g.Using
@raw
here with your fix works a charm. Having a separate file would definitely work - but the script is still quite small and having it next to the HTML is kinda nice (and I’m lazy :p)Thanks for all your work on JTE - it has been an absolute joy to use!
I just released version 2.1.2 to maven central, including this fix 😃