edit this page - page history - about editing

HTML syntax validation within Handlebars templates in Sublime Text 2

To enable HTML syntax validation within Handlebars templates in Sublime Text 2, follow similar instructions to http://stackoverflow.com/questions/5578472/amend-html-grammar-based-on-attributes-in-textmate.

<body>  
	<script type="text/x-handlebars" data-template-name="todos">
		<section id="todoapp">
		<!-- ... -->
		</section>
	</script>
</body>

Even with ST2 plugins such as sublime-text-handlebars, the HTML code still would not be presented as HTML (and you had to change the syntax of the file to Handlebars, removing ALL HTML syntax validation). Ideally we would keep the HTML file syntax as HTML.

Edit %APPDATA%/Sublime Text 2/Packages/HTML/HTML.tmLanguage and change:

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

to:

		<dict>
			<key>begin</key>
			<string>(?:^s+)?(&lt;)((?i:script))b(?!([^>]*text/x-handlebars[^&gt;]*|[^&gt;]*/>))</string>
			<key>beginCaptures</key>

The change will be instant; to see if it works, try commenting out a block (Ctrl-/) within a script tag. If Sublime Text thinks it is currently in HTML mode, they will be wrapped with HTML comments; otherwise Javascript comments.

(I don't know if there is a better place to put this that won't be overridden by updates yet.)
Categories: Sublime Text | HTML | IDEs
edit this page - what links to here? - page history - top
Last edited by jevon jevon 40 months ago