Awesomeness
Awesomeness

Reputation: 2631

HAML - understanding how to layout a page

I am still learning HAML and I have some syntax that I am not clear about.

When I see code like this:

-content_for :primary_content do
  .content_container

What does the content_for block do and mean? Is that a call to a specific layout? Or is .content_container a class somewhere?

The reason I ask is because I have some links that appear on the page and I am not sure why they appear there. I wonder if primary_content gets a block of code to render? If so, how do I find or look up this block of code?

Thanks!

Upvotes: 0

Views: 304

Answers (3)

jdoe
jdoe

Reputation: 15771

First off: content_for isn't a Haml's feature. It's one of helpers available in Ruby on Rails views. You can use it in ERb too.

This method renders the given block but doesn't output it. To see the result of rendering you must call = yield :key where :key is the same symbol that was used for content_for. To check if content was generated use content_for? :key.

Details

Upvotes: 3

Romain
Romain

Reputation: 12819

Your HAML is roughly equivalent to the following ERB:

<%- content_for :primary_content do %>
    <div class='content_container'></div>
<%- end %>

Essentially, "content_for :symbol" defines a named piece of content that will later be included in a layout or something.

.something is a HAML short-hand way of saying "make me a div which CSS class is something".

Upvotes: 5

sailor
sailor

Reputation: 8034

You use content_for to put content in a placeholder called primary_content located somewhere in your layout.

http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#method-i-content_for-3F

Upvotes: 1

Related Questions