Using templates to render HTML in Javascript is great; but unfortunately Sublime Text 2 treats templates embedded in script tags as Javascript (which means syntax highlighting and completions don't work correctly). Luckily, this can easily be fixed by tweaking Sublime's HTML language file.
Here's what you need to do:
-
Go to Sublime's
Packages
directory (or the use thePreferences -> Browse Packages…
menu item). -
Open the
HTML/HTML.tmLanguage
file. -
Change line 286 from:
<string>(?:^\s+)?(<)((?i:script))\b(?![^>]*/>)</string>
to:
<string>(?:^\s+)?(<)((?i:script))\b(?![^>]*/>)(?!.*type=["']text/template['"])</string>
Now any script tags with a type
of text/template
will be treated as HTML:
<script type="text/template">{{title}}</script>
GitHub Comments
samtgarson
tp
bonsak
jrtitus
sciascia
Rowno- Copy
- Copy the
- Edit the
- Profit. ✨
sciascia
Rowno
ghost
add comment via GitHubIn Sublime Text 3, this line is at 308. Worth doing a find just make sure.
I have filed this as a bug with ST, so that all users may enjoy this fix in a future version: http://sublimetext.userecho.com/topic/453232-/
Thanks @Rowno. Great tip. This was driving me crazy.
@Rowno - Thanks for posting this. You rock.
Hi There,
Does anyone know how to fix this issue in ST3 on Mac OS10.9?
There are no
HTML.tmLanguage
files if I goPreferences -> Browse Packages…
.If I
Show package contents
on the Sublime Text app then goContents –> MacOS –> Packages
I can see a file calledHTML.sublime-package
but it looks to be some sort of compressed archive that can't be edited.If I search the system (including system files) for
HTML.tmLanguage
all that's returned is a cache file namedHTML.tmLanguage.cache
and a python file namedsublimezenplugin.py
.Any pointers in the right direction would be much appreciated.
Cheers
Ben
@sciascia The default packages are no longer extracted into the user Packages directory in Sublime Text 3.
Here's what you need to do:
Packages/HTML.sublime-package
file from the installation directory and decompress it as a.zip
file (rename it toHTML.sublime-package.zip
).HTML.tmLanguage
file inside toHTML/HTML.tmLanguage
in your user Packages directory (ie:Preferences -> Browse Packages…
).HTML.tmLanguage
file to make the required change at line 308.Maybe I should write a followup blog post. 😉
@Rowno Cheers, thanks heaps for the reply and your help.
I'd just installed SublimeLinter which seemed to install it's own version of
HTML.tmLanguage
and fixed the issue.BTW I was having this issue with straight js script tags (with no type declared) but have now noticed that js syntax highlighting doesn't work. I guess it's an either/or situation - yes?
@sciascia No, both are working fine for me.
couldn't get none of these working for me in macos sierra.. ended up setting up the syntax as xml :-)