I am using haml with my rails application and i have a question how the easiest way to insert this haml code into a html file:
<div clas="holder"> <div class=top"></div> <div class="content"> Content into the div goes here </div> <div class="bottom"></div> </div>
And I want to use it in my haml document like this:
%html %head %body Maybee some content here. %content_box #I want to get the code i wrote inserted here Content that goes in the content_box like news or stuff %body
Is there an easier way to do this?
I get this error:
**unexpected $end, expecting kEND**
with this code:
# Methods added to this helper will be available to all templates in the application. module ApplicationHelper def content_box(&block) open :div, :class => "holder" do # haml helper open :div, :class => "top" open :div, :class => "content" do block.call open :div, :class => "bottom" end end end
A Helper method is used to perform a particular repetitive task common across multiple classes. This keeps us from repeating the same piece of code in different classes again and again. And then in the view code, you call the helper method and pass it to the user as an argument.
In Haml, we write a tag by using the percent sign and then the name of the tag. This works for %strong , %div , %body , %html ; any tag you want. Then, after the name of the tag is = , which tells Haml to evaluate Ruby code to the right and then print out the return value as the contents of the tag.
You can use haml_tag too
def content_box haml_tag :div, :class => "holder" do haml_tag :div, :class => "top" haml_tag :div, :class => "content" do yield haml_tag :div, :class => "bottom" end end
and in haml
%html %head %body Maybee some content here. = content_box do Content that goes in the content_box like news or stuff
The typical solution to this is to use a partial.
Or a helper method in your _helper.rb file:
def content_box(&block) open :div, :class => "holder" do # haml helper open :div, :class => "top" open :div, :class => "content" do block.call end open :div, :class => "bottom" end end
And in haml:
%html %head %body Maybee some content here. = content_box do Content that goes in the content_box like news or stuff
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With