Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Precompile slim templates using the rails asset pipeline

It would be super handy if I could precompile slim templates using the rails asset pipeline. I was hoping to stick my templates in app/assets/html and serve them up that way.

Here's what I've got so far:

# config/initializers/slim.rb
Rails.application.assets.register_engine('.slim', Slim::Template)

# config/application.rb
config.assets.paths << "#{Rails.root}/app/assets/html"
config.assets.register_mime_type('text/html', '.html')

Running rake assets:precompile reads the .html.slim files in app/assets/html but it doesn't compile them and the output file still has the .slim extension.

Is there a way to make this work?

like image 267
Jimmy Baker Avatar asked Feb 18 '13 22:02

Jimmy Baker


People also ask

How do you Precompile in rails?

To compile your assets locally, run the assets:precompile task locally on your app. Make sure to use the production environment so that the production version of your assets are generated. A public/assets directory will be created. Inside this directory you'll find a manifest.

What is the rails asset pipeline?

1 What is the Asset Pipeline? The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages and pre-processors such as CoffeeScript, Sass, and ERB.

What does bundle exec rake assets Precompile do?

4.1 Precompiling Assets. Rails comes bundled with a rake task to compile the asset manifests and other files in the pipeline to the disk. Compiled assets are written to the location specified in config.


2 Answers

The answer by @kurt-mueller is correct but needs to be updated for Rails 4 with Sprockets 3+. There was a change in Sprockets that means the assets property is not present during initialisation. Instead you can do:

# config/initializers/slim.rb

Rails.application.config.after_initialize do |app|
  app.config.assets.configure do |env|
    env.register_engine(".slim", Slim::Template)
  end
end
like image 64
Andrew France Avatar answered Sep 18 '22 02:09

Andrew France


Sorry I'm tardy to the party, but Dillon Buchanan answered this question here.

Go to the config/initializers directory and create a file like slim_assets.rb (or something similar) and copy-pasta the following line:

Rails.application.assets.register_engine('.slim', Slim::Template)

I've done something similar with great success with HAML (which I use to write templates for Rails/AngJS apps).

like image 41
Kurt Mueller Avatar answered Sep 20 '22 02:09

Kurt Mueller