views:

25

answers:

1

I'm creating a new plugin for a jruby on rails application that will eventually be turned into a gem. Inside my plugin I have controllers, helpers and views. For my views I'd like to use Haml. The problem is that I can't seem to get it to recognize that they are haml templates. Is it even possible to do this? Is there a way for a plugin to have Haml as a dependency for its view? And by that I mean, I intend for the plugin that I'm creating to have a view created by the plugin, that can be used by the application developer.

for example:

vendor/   
  plugins/    
    my_plugin/  
      lib/  
        app/  
          views/  
            my_plugin_demo/  
              index.haml.html
           controllers/  
             my_plugin_demo_controller.rb
           helpers/

In my plugin's init.rb, I tried:

require 'my_plugin'  
require 'haml'  #doesn't seem to make a difference :(

but that didn't seem to make any difference. Has anybody had any experience with this? I can't seem to find any documentation on how to make this work. Are plugin views restricted to .erb templates?

Update: @Jens Fahnenbruck

I'm a still a bit confused... are you recommending that I place the following into my_plugin's init.rb file?

# Load Haml and Sass.  
# Haml may be undefined if we're running gems:install.  
Haml.init_rails(binding) if defined?(Haml)  

require 'my_plugin'  

I tried doing just that and it didn't work. its still giving me the following page error:

Missing template my_plugin_demo/index.erb in view path app/views  

Not sure I understand what you were recommending...

A: 

this is my vendor/plugins/haml/init.rb file created by haml --rails /path/to/app (see http://wiki.rubyonrails.org/howtos/templates/haml)

begin
  require File.join(File.dirname(__FILE__), 'lib', 'haml') # From here
rescue LoadError
  begin
    require 'haml' # From gem
  rescue LoadError => e
    # gems:install may be run to install Haml with the skeleton plugin
    # but not the gem itself installed.
    # Don't die if this is the case.
    raise e unless defined?(Rake) &&
      (Rake.application.top_level_tasks.include?('gems') ||
        Rake.application.top_level_tasks.include?('gems:install'))
  end
end

# Load Haml and Sass.
# Haml may be undefined if we're running gems:install.
Haml.init_rails(binding) if defined?(Haml)

I believe the last line is what you need for the plugin to be working

UPDATE

Your init.rb file should look like this:

require 'haml'
Haml.init_rails(binding)
require 'my_plugin'

Update 2

Try another folder structure:

vendor/   
  plugins/    
    my_plugin/  
      lib/  
      app/  <-- one level up
        views/  
          my_plugin_demo/  
            index.haml.html
         controllers/  
           my_plugin_demo_controller.rb
         helpers/
jigfox
@Jens FahnenbruckThanks for the response, but I'm not sure it works. I've updated my original question with details per your suggestion...
Eric
@Jens Fahnenbruck... I tried your update but I'm still getting the following message:"Missing template my_plugin_demo/index.erb in view path app/views." For the life of me, I can't figure out what I'm doing wrong. :(
Eric
@Jens Fahnenbruck: It seems like the only way I can get a view to show up is when I place an index.html.erb or index.html.haml in the Rails.root/app/views/... Any time I try and place either of those index files in Rails.root/vendor/plugins/my_plugin/lib/app/views/my_plugin_demo/ it spits out: Missing template my_plugin_demo/index.erb in view path app/views
Eric
I think you could have a wrong file structure: the `app` folder should be in `vender/plugins/my_plugin/app` instead of `vender/plugins/my_plugin/lib/app`
jigfox
Thanks Jens! That totally worked! So stoked!
Eric