ruby-on-rails-3 - _foo.mustache.haml 模板

标签 ruby-on-rails-3 haml mustache

我想使用单个 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/

相关文章:

ruby-on-rails - Ruby-Thin 服务器在生产中随机崩溃

javascript - .live 不是 rails 3.2.11 中的函数错误

ruby-on-rails - 抽佣 Assets :precompile cannot load such file -- uglifier error

html - 为什么在 Chrome 和 Firefox 中元素宽度不同?

ruby-on-rails - Vbox 和 Rails 上的 Ubuntu—— "Please install tilt-1.3.3"

ruby - 找不到 RubyGem haml

java - 可以在 Java Web 应用程序中使用 Haml 吗?

javascript - 如何在由 Jekyll 和 Liquid Layouts 一起生成的站点中使用 mustache.js

php - 如何在 Mustache 模板中使用 switch case?

java - mustache +吊带+CQ5