2

Using Rails 3.2, you can make files in the asset pipeline use multiple preprocessors by appending multiple file extensions, thusly: index.css.scss.erb

I tried doing this with a view (index.html.slim.erb) and it didn't seem to know what to do (more accurately, it just didn't find the view at all).

Does Rails really not pass views through Tilt? Is there another way I can make a view run through one preprocessor and then another?

(Context: I'm working on something that's intended to modify HTML fed in before being returned, so I'd want it to run after haml/slim/erb.)

Ben Hamill
  • 2,661
  • 2
  • 19
  • 18

1 Answers1

2

Indeed, you can not. Rails does not use tilt for view templates.

One reason it would be complicated for it to do so, is that in normal operation ERB actually 'compiles' to ruby code, not to text, for performance. Ie, the erb template compiles once to live ruby code, which is then executed every time it needs to be displayed in a different context.

I don't know built in way to do what you want. You could certainly roll your own. Nobody says you have to call "render 'template'" to render. Don't forget you can always

render :text => any_method_that_returns_a_string

You could pass things through Tilt yourself. You may see some performance degredation compared to what Rails usually does.

jrochkind
  • 22,799
  • 12
  • 59
  • 74
  • I guess the performance stuff you mention is why it's best to precompile assets, rather than build them at request time? Sounds reasonable, anyway. In my case, I am ding something that is specific to templates used by mailers, so performance isn't a big deal. Does that fact (mailers) have any impact, do you think? – Ben Hamill Mar 22 '12 at 06:03
  • have any impact on what? I don't know. So far as I know there's still no built in way to do it. – jrochkind Mar 22 '12 at 18:52
  • I was thinking maybe mailers handled views differently, but that seems silly not that I say it out loud. I think I have found a way to do what I want using ActionMailer::Base.register_interceptor. – Ben Hamill Mar 23 '12 at 16:11
  • @jrochkind Take a look at http://stackoverflow.com/a/16246891/619510, I did what you said, but it looks awful and brittle. Anyway, thanks a lot, couldn't have solved this without your answer, before I read this I had no clue template handlers and preprocessors were different things. – michelpm May 01 '13 at 15:56
  • If you want the ERB in the CSS to be evaluated every request... that is incompatible with the Rails asset pipeline. The Rails Asset pipeline compiles all your CSS into a static file at 'install time', it can't re-evaluate it for every request. You didn't say that's what you were trying to do in your original question, but if that's what you're trying to do, you can't use asset pipeline (and you may want to reconsider) – jrochkind May 02 '13 at 16:55