我如何在新插件的视图中使用Haml? [英] How do I use Haml in a view in my new plugin?

查看:127
本文介绍了我如何在新插件的视图中使用Haml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为rails应用程序创建了一个新的插件,最终会变成一个gem。在我的插件里面有控制器,助手和视图。对于我的看法,我想用Haml。问题是,我似乎无法认识到他们是haml模板。这甚至有可能吗?有没有办法让插件将Haml作为其视图的依赖项?我的意思是,我打算为我创建的插件创建插件创建的视图,供应用程序开发人员使用。

例如:

 供应商/ 
插件/
my_plugin /
lib /
app /
views /
my_plugin_demo /
index.haml.html
控制器/
my_plugin_demo_controller .rb
助手/

在我的插件的init.rb中,我试过了:

  require'my_plugin'
require'haml'#似乎没有区别:(

但这似乎没有任何区别。有没有人有过这方面的经验?我似乎无法找到任何更新:@Jens Fahnenbruck

更新:@Jens Fahnenbruck

我仍然有点困惑......你是否建议我将以下内容放入my_plugin的init.rb文件中?

 #加载Haml和Sass。 
#如果我们运行gems:install,Haml可能是未定义的。
Haml.init_rails(绑定)如果已定义?(Haml)

需要'my_plugin'

我尝试过这样做,但没有奏效。它仍然给我以下页面错误:

 缺少模板my_plugin_demo / index.erb在视图路径应用程序/视图

不知道我明白你在推荐什么...

解决方案

这是由 haml创建的供应商/ plugins / haml / init.rb 文件 - hails --rails / path / to / app (参见 http://wiki.rubyonrails。 org / howtos / templates / haml

  begin 
require File.join(File.dirname __FILE__),'lib','haml')#从这里
rescue LoadError
begin
require'haml'#从gem
rescue LoadError => e
#gems:install可以运行以安装Haml,其中包含骨架插件
#但不包括安装的gem。
#如果是这种情况,不要死。除非定义,否则
会提升e(Rake)&&
(Rake.application.top_level_tasks.include?('gems')||
Rake.application.top_level_tasks.include?('gems:install'))
end
end

#加载Haml和Sass。
#如果我们运行gems:install,Haml可能是未定义的。
Haml.init_rails(绑定)如果已定义?(Haml)

我相信最后一行是你需要的插件工作



更新



您的init .rb文件应该如下所示:

  require'haml'
Haml.init_rails(绑定)
需要'my_plugin'

更新2



尝试另一个文件夹结构:

 供应商/ 
插件/
my_plugin /
lib /
app /< - 上一级
views /
my_plugin_demo /
index.haml.html
控制器/
my_plugin_demo_controller.rb
助手/


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/

这篇关于我如何在新插件的视图中使用Haml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆