使用Rails 3和Haml 3,如何配置Haml?

时间:2021-11-26 07:40:22

I'm using Rails 3.0.0.beta3 and Haml 3.0.0.rc.2, and I can't find where I need to place the configuration lines for Haml (nor what they are in the new version, for that matter). Using Rails 2.3.5 and Haml 2, I would do

我正在使用Rails 3.0.0.beta3和Haml 3.0.0.rc.2,我找不到我需要为Haml放置配置行的位置(也不是它们在新版本中的用途) 。使用Rails 2.3.5和Haml 2,我会这样做

Haml::Template.options[:format] = :html5

in environment.rb. Or, in Sinatra,

在environment.rb中。或者,在Sinatra,

set :haml, {:format => :html5}

in my main file. But in Rails 3 everything's been changed around, and no matter where I put that configuration line, I get an undefined method or undefined object error.

在我的主文件中。但是在Rails 3中,一切都被改变了,无论我把配置线放在哪里,我都会得到一个未定义的方法或未定义的对象错误。

2 个解决方案

#1


17  

Create the file:

创建文件:

#{Rails.root}/config/initializers/haml.rb

With haml option:

使用haml选项:

Haml::Template.options[:attr_wrapper] = '"'

#2


8  

In accordance with Rails 3's lazy-loading philosophy, Haml only initializes itself once ActionView::Base is loaded, which may not have happened when the configuration file is being parsed. In order to run code once Haml's been loaded, you need to run it in a ActiveSupport#on_load block. For example:

根据Rails 3的延迟加载原理,Haml只在加载ActionView :: Base时才初始化,这在解析配置文件时可能不会发生。为了在加载Haml后运行代码,您需要在ActiveSupport#on_load块中运行它。例如:

ActiveSupport.on_load(:action_vew) do
  Haml::Template.options[:format] = :html5
end

I'm considering ways of making the configuration accessible before the full Haml system has been loaded, either by defining Haml::Template.options earlier or adding a special config.haml hash.

我正在考虑在加载完整的Haml系统之前使配置可访问的方法,可以通过先前定义Haml :: Template.options或添加特殊的config.haml哈希。

#1


17  

Create the file:

创建文件:

#{Rails.root}/config/initializers/haml.rb

With haml option:

使用haml选项:

Haml::Template.options[:attr_wrapper] = '"'

#2


8  

In accordance with Rails 3's lazy-loading philosophy, Haml only initializes itself once ActionView::Base is loaded, which may not have happened when the configuration file is being parsed. In order to run code once Haml's been loaded, you need to run it in a ActiveSupport#on_load block. For example:

根据Rails 3的延迟加载原理,Haml只在加载ActionView :: Base时才初始化,这在解析配置文件时可能不会发生。为了在加载Haml后运行代码,您需要在ActiveSupport#on_load块中运行它。例如:

ActiveSupport.on_load(:action_vew) do
  Haml::Template.options[:format] = :html5
end

I'm considering ways of making the configuration accessible before the full Haml system has been loaded, either by defining Haml::Template.options earlier or adding a special config.haml hash.

我正在考虑在加载完整的Haml系统之前使配置可访问的方法,可以通过先前定义Haml :: Template.options或添加特殊的config.haml哈希。