简体   繁体   English

在Mustache中使用参数生成Rails

[英]Rails Yield With Parameter in Mustache

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 . 我正在使用这个宝石的 Rails 3中的Mustache,当我尝试在我通常使用yield :parameter的实例中使用Mustache时,我遇到了障碍。

<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? 有没有办法用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. 但似乎使用mustache_rails3 gem来解决当前的设置会很麻烦。

I'm also open to any answers that point out a good way to avoid this yield approach altogether. 我也对任何答案持开放态度,指出一种完全避免这种yield方法的好方法。 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. 可以在{{page_title}}标签中放入足够的逻辑来处理我设置标题的所有不同情况,但这似乎远非理想。

All of the logic for your Mustache templates should be put into the view file. 您的Mustache模板的所有逻辑都应放入视图文件中。 For example, your show.html.mustache template should have an associated Ruby view file called show.rb where you can put any custom logic for the template. 例如, show.html.mustache模板应该有一个名为show.rb的关联Ruby视图文件,您可以在其中放置模板的任何自定义逻辑。

The template would use a {{page_title}} call 该模板将使用{{page_title}}调用

<html>
  <head>
    <title>{{page_title}}</title>
  </head>
</html>

And the view file would define a page_title method to fill in the template 视图文件将定义一个page_title方法来填充模板

# inside show.rb
def page_title
  SettingsList.site_title + " " + @post.title
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM