Home Download Buy Blog Forum Support

.js.erb and html.erb

.js.erb and html.erb

Postby damoncali on Sat Jan 29, 2011 4:52 pm

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?
damoncali
 
Posts: 2
Joined: Sat Jan 29, 2011 4:50 pm

Re: .js.erb and html.erb

Postby adzenith on Sat Jan 29, 2011 5:21 pm

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.
adzenith
 
Posts: 1217
Joined: Mon Oct 19, 2009 9:12 pm

Re: .js.erb and html.erb

Postby guillermooo on Sat Jan 29, 2011 7:21 pm

damoncali wrote: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?


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:
Code: Select all
<string>html.erb</string>

5) Restart Sublime

That should work if I've got you right.

HTH
Guillermo
guillermooo
 
Posts: 729
Joined: Thu Jul 23, 2009 9:06 am

Re: .js.erb and html.erb

Postby adzenith on Sat Jan 29, 2011 7:55 pm

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: Select all
{
   "extensions":
   [
      "erg"
   ]
}

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.
adzenith
 
Posts: 1217
Joined: Mon Oct 19, 2009 9:12 pm

Re: .js.erb and html.erb

Postby guillermooo on Sat Jan 29, 2011 7:58 pm

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.
guillermooo
 
Posts: 729
Joined: Thu Jul 23, 2009 9:06 am

Re: .js.erb and html.erb

Postby adzenith on Sat Jan 29, 2011 8:11 pm

The method you described *should* work, but unfortunately it seems like Sublime Text is doing something like:
Code: Select all
extension = filename[filename.rfind(".")+1:len(filename)]
if extension == "html.erg":
...

instead of something like:
Code: Select all
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. :) So now I'm just having fun guessing about the internals of Sublime Text.
adzenith
 
Posts: 1217
Joined: Mon Oct 19, 2009 9:12 pm

Re: .js.erb and html.erb

Postby adzenith on Sat Jan 29, 2011 8:18 pm

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.
adzenith
 
Posts: 1217
Joined: Mon Oct 19, 2009 9:12 pm

Re: .js.erb and html.erb

Postby adzenith on Tue Feb 01, 2011 5:31 pm

You can totally do this with a plugin, as gpfsmurf points out:
viewtopic.php?f=5&t=856&p=7066&hilit=syntax#p7032
adzenith
 
Posts: 1217
Joined: Mon Oct 19, 2009 9:12 pm


Return to Technical Support

Who is online

Users browsing this forum: Exabot [Bot], Yahoo [Bot] and 22 guests