css.erb custom helpers #3116

Closed
daniel2d2art opened this Issue Sep 23, 2011 · 9 comments

Comments

Projects
None yet
5 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

This comment has been minimized.

Show comment Hide comment
@parndt

parndt Sep 27, 2011

Contributor

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...

Contributor

parndt commented Sep 27, 2011

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

This comment has been minimized.

Show comment Hide comment
@guilleiguaran

guilleiguaran Sep 27, 2011

Owner

Try including your helper module in Sprockets::Context:

Rails.application.assets.context_class.instance_eval do
  include YourHelperModule
end
Owner

guilleiguaran commented Sep 27, 2011

Try including your helper module in Sprockets::Context:

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

This comment has been minimized.

Show comment Hide comment
@daniel2d2art

daniel2d2art Sep 29, 2011

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

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

@guilleiguaran

This comment has been minimized.

Show comment Hide comment
@guilleiguaran

guilleiguaran Sep 29, 2011

Owner

@daniel2d2art try as initializer

Owner

guilleiguaran commented Sep 29, 2011

@daniel2d2art try as initializer

@guilleiguaran

This comment has been minimized.

Show comment Hide comment
@guilleiguaran

guilleiguaran Dec 8, 2011

Owner

@daniel2d2art this still being an issue for you?

Owner

guilleiguaran commented Dec 8, 2011

@daniel2d2art this still being an issue for you?

@daniel2d2art

This comment has been minimized.

Show comment Hide comment
@daniel2d2art

daniel2d2art Dec 11, 2011

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

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

This comment has been minimized.

Show comment Hide comment
@daniel2d2art

daniel2d2art Dec 11, 2011

closing issue ...

closing issue ...

@nowhereman

This comment has been minimized.

Show comment Hide comment
@nowhereman

nowhereman Dec 14, 2011

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.

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.

@buck3000

This comment has been minimized.

Show comment Hide comment
@buck3000

buck3000 Dec 9, 2017

More up-to-date look at this:

Rails.application.config.assets.configure do |env|
env.context_class.class_eval do
include ApplicationHelper
end
end

buck3000 commented Dec 9, 2017

More up-to-date look at this:

Rails.application.config.assets.configure do |env|
env.context_class.class_eval do
include ApplicationHelper
end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment