【发布时间】:2012-06-09 11:49:59
【问题描述】:
我想做的是在 Haml 视图中使用别名方法(在 ruby 文件中定义)。
我定义了一个别名方法,如下所示:
require 'sinatra'
require 'sinatra/base'
require 'data_mapper'
require 'haml'
helpers do
include Haml::Helpers
alias_method :h, :html_escape
end
class App < Sinatra::Base
use Rack::MethodOverride
# ...
end
然后我在 Haml 视图中使用了方法 h(),如下所示:
- @notes.each do |note|
%article{:class => note.complete? && "complete"}
%p
=h note.content
但是我打开页面时出现错误:
NoMethodError - # 的未定义方法“h”:
...
当我直接在haml文件上使用Haml::Helpers.html_escape()时,没有问题:
%p
= Haml::Helpers.html_escape note.content
如何在 haml 文件中使用别名方法而不出错?
感谢您对此问题的任何建议或更正。
【问题讨论】:
-
很多这种符号对我来说是不熟悉的,但是别名不应该放在定义 html_escape 的模块中吗?
-
是的,我发现
=html_escape note.content在haml 中运行良好,而不是冗长的=Haml::Helpers.html_escape note.content。