Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a helper or something for haml with ruby on rails

Tags:

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 
like image 595
Lisinge Avatar asked Mar 26 '10 13:03

Lisinge


People also ask

How do you use the helper method in Ruby on Rails?

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.

How do I write code in HAML?

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.


2 Answers

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 
like image 157
shingara Avatar answered Sep 20 '22 21:09

shingara


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 
like image 40
Glenn Jorde Avatar answered Sep 17 '22 21:09

Glenn Jorde