Handlebars icon indicating copy to clipboard operation
Handlebars copied to clipboard

Using Handlebar expression in <script> blocks breaks syntax highlighting in Sublime

Open jospas opened this issue 4 years ago • 4 comments

Great tool guys just some niggly bits when using to produce HTML output:

<script>
  {{#if myVariable}}
    myFunc('Test');
  {{/if}}
</script>

image

jospas avatar Dec 03 '20 02:12 jospas

Hey @jospas,

That's expected, for a bare <script> tag it's assumed that it'll be all Javascript inside.

If you want the contents to be parsed as Handlebars, you'll need to add a type to it like text/x-handlebars-template, text/x-handlebars or text/template.

For more context have a look at the HTML example file: https://github.com/daaain/Handlebars/blob/master/test/inline_script.handlebars.html#L35

I know that's not ideal for your use case, but due to the limitations of the language syntax definition format it would be hard to seamlessly detect and switch between 3 languages in this case.

If you're keen for this to happen though I can review a PR.

daaain avatar Dec 03 '20 10:12 daaain

That's expected, for a bare <script> tag it's assumed that it'll be all Javascript inside.

If you want the contents to be parsed as Handlebars, you'll need to add a type to it like text/x-handlebars-template, text/x-handlebars or text/template.

I don't think this is the use case presented here. The whole document is a Handlebars template, I would expect, that you can use the Handlebars logic anywhere in it. I think the PHP highlighter is a good example here. You can insert a PHP block anywhere in the document, regardless if it's in the HTML context, CSS context or JS context: image

But with Handlebars, there are some issues with overriding the embedded syntax (here, CSS and JS): image

Moreover, there is an issue with the script end tag </script>, regardless of having {{...}} inside: image

Although, I'm using the newest Sublime Text, currently version 4, build 4143. The ending tag issue seems to be quite new, I didn't have it on ST3. So maybe ST4 is here to blame.

pepkin88 avatar Dec 03 '22 21:12 pepkin88

From a user's perspective these use cases are of course reasonably expected to be supported, but to make it work this syntax library would need to reimplement and maintain both CSS and JS syntaxes instead of just handing off to the existing ones (which then of course sadly don't support Handlebars).

I personally haven't used Handlebars for several years so definitely not going to have the time and motivation to take on a big feature like that, but if you feel like taking on it I can review a PR.

As for the regression in ST4, do you know exactly which version broke it? Wonder if it's a regression in the syntax engine of ST4 itself so would be worthwhile raising as an issue there? This Handlebars syntax library hasn't changed since 2019.

daaain avatar Dec 03 '22 22:12 daaain

From a user's perspective these use cases are of course reasonably expected to be supported, but to make it work this syntax library would need to reimplement and maintain both CSS and JS syntaxes instead of just handing off to the existing ones (which then of course sadly don't support Handlebars).

I don't know much about making syntax highlighters for Sublime Text, but I'm now looking how they did it for PHP, and they just refer to the JavaScript.sublime-syntax file. Maybe it is a newer thing that wasn't available a few years ago, I don't know. Also, they now use YAML natively, without converting to .tmLanguage :)

I personally haven't used Handlebars for several years so definitely not going to have the time and motivation to take on a big feature like that, but if you feel like taking on it I can review a PR.

Yeah, understandable. I'll give it a shot.

As for the regression in ST4, do you know exactly which version broke it?

Unfortunately no. I recently stumbled upon a file that I wrote a good while ago using ST3, and now I see that bug, which I don't remember seeing before. And I know that there were some changes with their syntax highlighting engine for the version 4, so I presume that it might not be completely backward compatible.

pepkin88 avatar Dec 03 '22 23:12 pepkin88

I'd like to report, that this issue seems to be resolved, I assume by #111. Thank you @MatthiasPortzel and @daaain <3

pepkin88 avatar Mar 10 '24 10:03 pepkin88

Amazing news, thanks for letting us know!

daaain avatar Mar 11 '24 20:03 daaain