开发者

How do I use Haml in a view in my new plugin?

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


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/
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜