content_for & yield_content

content_for is a great for situations where you might want to change something from the layout on every view. For example a page title or a css script.

On The View


<% content_for "title" do %>
  <title>My Kemal Application</title>
<% end %>

<% content_for "css" do %>
  <link href="" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<% end %>

<div class="content-area">
  this will be the view content

<% content_for "javascript" do %>
  <script src="" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<% end %>

Use the content_for and give it a name. You can have multiple content_for macros on a view. For example one for the title, another for css, and another for javascript.

On The Layout

add the yield_content with the name of the content you want to display.


  <%= yield_content "title" %>
  <%= yield_content "css" %>
    <ul class="menu">
      <li><a href="#">Home</a></li>
      <li><a href="#">Contact</a></li>
  <%= content %>
  <%= yield_content "javascript" %>

results matching ""

    No results matching ""