开发者

Rails Layout Rendering with controller condition

开发者 https://www.devze.com 2022-12-25 18:12 出处:网络
I don\'t know what\'s the best way to doing this. On my application.html.erb I define a header div. My default controller ( root ) is a homepage controller.

I don't know what's the best way to doing this.

On my application.html.erb I define a header div.

My default controller ( root ) is a homepage controller. And I wish that if I'm at index of the homepage the header is rendering with some content, but all other controllers render another content inside that header.

How can I make a condition in that header div to render different conte开发者_如何学运维nt based on the controller that's being rendered?


You can use the content_for helper to define a block of markup in individual view templates that's yielded to from the application layout.

application.html.erb:

<div id="header">
  <%= yield :header %>
</div>

Then include code like this anywhere within a controller's view template:

<% content_for :header do %>
  This is a controller-specific header.
<% end %>

Note that you can define as many of these blocks as you like, so you could have a conditional sidebar etc. as well.

  • Layouts and content_for Railscast
0

精彩评论

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