StuntHacks
StuntHacks

Reputation: 447

Is it possible to access a Module from within a haml file?

I'd like to create sort my helpers into sub-modules, in order to make the code cleaner. For example, I'd like to implement something like this:

= UI.spawn_component(UI.alert, UI.error, "message")

I already tried just creating a module in my helper module like this:

module StyleguideHelper
  module UI
    def spawn_component(user, type)
      return user
    end
  end
end

And I also tried to create the module in a different file and require it from my helper file. Both of those didn't work.

Upvotes: 0

Views: 358

Answers (1)

infused
infused

Reputation: 24357

First, make sure that the file is named correctly so that autoloading works correctly. If the module is called StyleguideHelper then the file must be named styleguide_helper.rb. I would place this file in app/helpers unless you've setup lib for autoloading. Define your modules in that file like this:

module StyleguideHelper
  module UI
    def self.spawn_component(user, type)
      return user
    end
  end
end

You should then be able to use the helper in your view like this:

= StyleguideHelper::UI.spawn_component(user, type)

Upvotes: 1

Related Questions