question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using JS string interpolation inside @raw in template content throws "Unexpected end of template expression"

See original GitHub issue

Hey,

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:closed
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dwraggecommented, Jul 3, 2022

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.

@param gg.jte.Content title
@param gg.jte.Content content
@param gg.jte.Content script = @``

<!doctype html>
<html lang="en">
<head>
    <title>${title}</title>
    <meta charset="utf-8">
</head>
<body>
<div class="content">
    ${content}
</div>
${script}
</body>
</html>

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!

0reactions
casidcommented, Jul 8, 2022

I just released version 2.1.2 to maven central, including this fix 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found