开发者

Rails Yield With Parameter in Mustache

开发者 https://www.devze.com 2023-02-28 00:17 出处:网络
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.

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[:yiel开发者_开发问答d_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.


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 called show.rb where you can put any custom logic for the template.

The template would use a {{page_title}} call

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

And the view file would define a page_title method to fill in the template

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

精彩评论

暂无评论...
验证码 换一张
取 消