带有 Mustache 参数的 Rails 产量
我在 Rails 3 中使用 Mustache 和 this gem 并且我在我通常使用 yield :parameter
的实例中尝试使用 Mustache 时遇到了障碍。
<html>
<head>
<title><%= yield :page_title %></title>
</head>
</html>
显示帖子视图:
<% content_for :page_title do %>
<%= SettingsList.site_title + " " + @post.title %>
<% end %>
有没有办法用 Mustache 重现这种行为?看来,在编译模板时可能有一种方法可以解决这个问题:
mustache = MustacheClass.new
mustache[:yield_page_title] = content_for(:page_title)
但是,使用 Mustache_rails3 gem 来解决我当前的设置似乎很尴尬。
我也愿意接受任何指出完全避免这种 yield
方法的好方法的答案。可以将足够的逻辑放入 {{page_title}}
标记中来处理设置标题的所有不同情况,但这似乎远非理想。
I'm using Mustache in Rails 3 with this gem and I'm hitting a roadblock when trying to use Mustache in an instance where I would normally use yield :parameter
.
<html>
<head>
<title><%= yield :page_title %></title>
</head>
</html>
Show post view:
<% content_for :page_title do %>
<%= SettingsList.site_title + " " + @post.title %>
<% end %>
Is there a way to reproduce this behavior with Mustache? It appears that there may be a way to work this out when the template is compiled:
mustache = MustacheClass.new
mustache[:yield_page_title] = content_for(:page_title)
But it seems that that would be awkward to work out with my current setup using the mustache_rails3 gem.
I'm also open to any answers that point out a good way to avoid this yield
approach altogether. It would be possible to throw enough logic into a {{page_title}}
tag to handle all my different cases of setting the title, but this seems far from ideal.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Mustache 模板的所有逻辑都应放入视图文件中。例如,您的
show.html.mustache
模板应该有一个名为show.rb
的关联 Ruby 视图文件,您可以在其中放置模板的任何自定义逻辑。模板将使用
{{page_title}}
调用,视图文件将定义
page_title
方法来填充模板All of the logic for your Mustache templates should be put into the view file. For example, your
show.html.mustache
template should have an associated Ruby view file calledshow.rb
where you can put any custom logic for the template.The template would use a
{{page_title}}
callAnd the view file would define a
page_title
method to fill in the template