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

#src/views/pages/hello.ecr

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

<% content_for "css" do %>
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<% end %>

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

<% content_for "javascript" do %>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" 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.

#src/views/layouts/main.ecr

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

results matching ""

    No results matching ""