I saw someone’s code in View/car.html.haml like followows:
%body
=yield
Can anybody explain me the use of “=yield” here?
-where can I find the actual body part code?
-What does it means,
-why use it or in what situation we should use it?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Typically this will appear in a layout file (by default – views/layouts/application.html.haml). It simply tells Rails to render the content of the current action at that location.
So if you have views/layouts/application.html.haml with:
And you have views/posts/index.html.haml with:
Then when you go to an action that renders the posts index page (probably /posts), you will see the html that has that heading rendered at the location of the
yieldin your layout file:This is particularly useful to include things like common page navigation markup, or headers that should show up on all pages without having to re-define the exact html in every view.
If you have a different layout for a separate section of your site you might render your view in the controller like this:
Which will use the specified view, rendered inside the specified layout wherever the layout’s
yieldoccurs. In the else of the statementuser/unauthorized.html.hamlwould be rendered within thelayouts/external.html.hamllayout, and in the signed_in? case you would get theposts/index.html.hamlview rendered within thelayouts/application.html.hamllayout.