我想使用单个 haml/mustache 模板,如下所示:
.foo
%h2 {{title}}
我可以通过两种方式呈现:
1)作为部分并传递哈希值:
render :partial => 'foo', :mustache => {:title => 'hello world'}
=>
<div class='foo'><h2>hello world</h2></div>
2) 作为不带哈希的部分在脚本标记内使用:
:javascript
render :partial => 'foo'
=>
<script type='text/javascript'>
<div class='foo'><h2>{{title}}</h2></div>
</script>
我尝试过各种文件名组合...例如“_foo.mustache.haml”,但 Rails 无法识别该扩展名。
此外,我正在使用 ryan bates 的 Mustache_template_handler.rb: http://railscasts.com/episodes/295-sharing-mustache-templates
最佳答案
所以,这可行——但它不是真正的模板链接(这可能吗?!)
module MustacheTemplateHandler
def self.call(template)
haml = "Haml::Engine.new(#{template.source.inspect}).render"
if template.locals.include? :mustache
"Mustache.render(#{haml}, mustache).html_safe"
else
haml.html_safe
end
end
end
ActionView::Template.register_template_handler(:mustache, MustacheTemplateHandler)
关于ruby-on-rails-3 - _foo.mustache.haml 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8807407/