6

I've been doing a lot of work with Knockout templates lately, and I've been using Sublime to do it. one thing that I've noticed though is that when using a template, which needs to be defined in a block like this:

<script type="text/html"></script>

it treats the contents as Javascript, which means I'm missing out on a lot of HTML tools which I have installed. I'd like to make it treat that content as HTML instead of Javascript; is there any setting which I could use to do this?

2 Answers 2

18
+100

I managed to find the answer thanks to iamntz here; the trick is simple. For Sublime Text 3:

  1. Open up Packages within your install directory, then find HTML.sublime-package and open it in 7zip (or your favorite archive tool)
  2. Find HTML.tmLanguage and open it for editing
  3. Find this line:

    <string>(?:^\s+)?(&lt;)((?i:script))\b(?![^&gt;]*/&gt;)</string>
    

    and replace it with this one:

    <string>(?:^\s+)?(&lt;)((?i:script))\b(?!([^&gt;]*text/html[^&gt;]*|[^&gt;]*/>))</string>
    

Nice and easy; the text/html in that second snippet can be replaced with any template type, and it will now be read as HTML by Sublime. This fix will also work with any HTML packages you have installed.

Sign up to request clarification or add additional context in comments.

10 Comments

Where is the HTML.sublime-package..? I don't find it anywhere?
Oh managed to get it working for AngularJS. On windows the sublime-package is actually inside the program files. gist.github.com/CMCDragonkai/6056909
A few more directions for the technically challenged. When you open this it creates a zip file you then go in and edit that file to the line above in my cause i changed it to text/template to work with backbone and underscore. - Then save it, then you want to on mac rename the zip file back to the HTML.sublime-package - You will need to click on "get info" to remove the .zip extension and then open sublime it should work
On Ubuntu the HTML.sublime-package was found at /opt/sublime_text/
There's nice plugin to open package resources github.com/skuroda/PackageResourceViewer. Of course, you may consider it as overkill for this problem.
|
1

This doesn't appear to be necessary any longer for Sublime Text 3 build 3103. Just make sure your script tag's type attribute begins with "text/" and doesn't end in "javascript" and it should handle HTML correctly now.

EDIT:

This has become a problem again with Sublime Text 3 build 3176. The fix is to modify the HTML package again but with this change in HTML.sublime-syntax:

         - script-javascript
         - tag-generic-attribute-meta
         - tag-generic-attribute-value
-    - match: (?i)(?=text/html(?!{{unquoted_attribute_value}})|'text/html'|"text/html")
+    - match: (?i)(?=text/html(?!{{unquoted_attribute_value}})|'text/html'|"text/html"|"text/x-template")
       set:
         - script-html
         - tag-generic-attribute-meta

Replace "x-template" with whatever type you are using for your script tag templates.

1 Comment

I've the same issue in Sublime Tex3 build 3176, after 3 updates I've lost my html markup

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.