Sublime Forum

.js.erb and html.erb

#1

I often use files that end in .js.erb in my rails projects. Currently, all .erb files are displayed as HTML(Rails). Is there a way I can set .html.erb files to HTML(Rails) and .js.erb to JavaScript?

0 Likes

#2

It looks like this is not possible right now—as far as I can tell, Sublime Text 2 considers everything after the last . in the filename to be the extension, so even if you set up a rule that “js.erb” and “html.erb” are extensions for JavaScript and HTML (Rails) respectively, they won’t fire.

0 Likes

#3

I believe you want to modify the .tmLanguage file for HTML (Rails) and JavaScript.

A quick and dirty way to do this:

  1. Open the packages folder (Preferences | Browse Packages…)
  2. Go to the Rails package (folder)
  3. Open the HTML (Rails).tmLanguage file
  4. Find the “fileTypes” key and add the following to its array:
<string>html.erb</string>
  1. Restart Sublime

That should work if I’ve got you right.

HTH
Guillermo

0 Likes

#4

In general, you can just go to View>Syntax>Open all with current extension as… and then pick whatever you want.
When you do this, Sublime Text creates a new file in your [sublime packages]/User folder with the name of the syntax and the extension “.sublime-settings.” For example, when you pick “HTML (Rails)” from the “Open all with…” list, ST makes a file called “HTML (Rails).sublime-settings”.
The contents of this file are pretty simple:

[code]{
“extensions”:

	"erg"
]

}
[/code]
However, you’ll note that Sublime Text only grabs the filename after the last period. If you change this file so instead of “erg” it says “html.erg”, it won’t work, for the same reason that it says “erg” in the first place: as far as I can tell, Sublime Text only considers the part of the filename after the last period to be the extension, and obviously “html.erg” cannot come after the last period because it contains a period.

0 Likes

#5

Ah, I didn’t know about that… Now I understand. However, the method explained in my post above would work for this case? It makes more sense to make this a use specific thing as you say, though.

0 Likes

#6

The method you described should work, but unfortunately it seems like Sublime Text is doing something like:

extension = filename[filename.rfind(".")+1:len(filename)]
if extension == "html.erg":
 ...

instead of something like:

if filename.endswith("html.erg"):
 ...

which means that for the filename “asdf.html.erg”, extension ends up being “erg”.

I guess what I’m trying to say is that I tried to get the “html.erg” and “js.erg” thing to work and I couldn’t. :smile: So now I’m just having fun guessing about the internals of Sublime Text.

0 Likes

#7

Incidentally, it appears that having a file in [packages]/User named “HTML (Rails).sublime-settings” crashes Sublime Text on startup. “HTML.sublime-settings” works, “TCL.sublime-settings” doesn’t… I don’t get the pattern. Strange.

0 Likes

#8

You can totally do this with a plugin, as gpfsmurf points out:
viewtopic.php?f=5&t=856&p=7066&hilit=syntax#p7032

0 Likes