Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

css.erb custom helpers #3116

Closed
daniel2d2art opened this Issue · 8 comments

4 participants

@daniel2d2art

can't we add some custom helper to use in css.erb files

we try to put some methods to Helpers/application_helper but they are not accesible from css.erb... in template output <%= myHelperMethod() %>

we add the "helper :all" in application_controller ... but no lucky!!

The helper and their methods are not accesible from css.erb

Thx!

@parndt

ActionView::Helper classes are generally only available to users of ActionView like app/views/

My thought is that you would be better off putting your methods in a lib/ module and including that module inside the css.erb file. I'm guessing you could also include ActionView::Helpers inside the css.erb file...

@guilleiguaran

Try including your helper module in Sprockets::Context:

Rails.application.assets.context_class.instance_eval do
  include YourHelperModule
end
@daniel2d2art

Hi Guille ...
As initializer or in application.rb??

@guilleiguaran

@daniel2d2art try as initializer

@guilleiguaran

@daniel2d2art this still being an issue for you?

@daniel2d2art

No, I work arround templating directly to css, that is what I wanted to avoid. But in any case is another way to solve.

@daniel2d2art

closing issue ...

@nowhereman

Here a solution :
Create a config/initializers/sprockets_helpers.rb file
With this content :

# Support all ActionView goodness in asset files with *.erb extension
# More info at : https://github.com/rails/rails/issues/3116 and https://github.com/sstephenson/sprockets/issues/218
Rails.application.assets.context_class.class_eval do
  include ERB::Util
  include ActionView::Helpers
  include Rails.application.routes.url_helpers
end

Then you can use ActionView helpers, Routes helpers and some ERB methods like h() in your [app|lib|vendor]/assets/[javascripts|stylesheets]/*.erb files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.