The open helper is defined in Haml; it's not available in Rails on its own.
Ruby-wise, you could get rid of the "do;end" on the divs, and pass in a
dereferenced proc to the middle one rather than giving it a block in
which you yield. I'd also suggest modifying the CSS a little. Rather
than using "contact_box", then "content_box_header", then
"content_box_header_corner" etc., you could just nest the CSS. You could
have "content_box", and then beneath that "header", and then beneath
that "corner". Then you could reference it in Sass like so:
.content_box
.header
.corner
Also, in order to use curly braces, you also have to use parentheses. So
you could refactor it into the following:
def content_box(params = {}, &proc)
open 'div', :class=> 'content_box', :id => params[:id] do
open('div', :class=> 'header') { open 'div', :class=> 'corner' }
open('div', :class=> 'content', &proc)
open('div', :class=> 'content_box_footer') do
open 'div', :class=> 'content_box_footer_corner'
end
end
end
Hope that's helpful!
- Nathan
[EMAIL PROTECTED] wrote:
> Hey, I was just looking how to plug Haml code into helpers, but this
> is even better (not what I was looking for, but better)
>
> for some reason I couldn't do curly brackets in the helper, but this
> was good:
>
> def my_helper
> open 'div', :class => 'something' do
> yield
> end
> end
>
> Also, this 'open' helper is news to me and it's not even mentioned in
> rails' api. So I ended up with with horrible thing:
>
> def content_box(params = {})
> open 'div', :class=> 'content_box', :id => params[:id] do
> open 'div', :class=> 'content_box_header' do
> open 'div', :class=> 'content_box_header_corner' do;end
> end
> open 'div', :class=> 'content_box_content' do
> yield
> end
> open 'div', :class=> 'content_box_footer' do
> open 'div', :class=> 'content_box_footer_corner' do;end
> end
> end
> end
>
> Rather bad looking when attempting to nest tags. It works, but I'm
> wondering if it could be done nicer.
>
> On May 11, 4:42 pm, "Nathan Weizenbaum" <[EMAIL PROTECTED]> wrote:
>
>> Oh, I should have caught that before! The trick is that there are two ways
>> of getting text to the output buffer. The first is to use "=" and include it
>> as a string; the second is to directly write to the buffer. Haml code
>> directly writes to the buffer, so when you call "yield", that's what's
>> happening. There are two ways around this. You can either capture the Haml
>> as a string and use that:
>>
>> def round_corners
>> "<div class='rc'>#{capture { yield }}</div>"
>> end
>>
>> Or you can use the "open" helper to create your tags, which will write to
>> the buffer:
>>
>> def round_corners
>> open 'div', :class => 'rc' { yield }
>> end
>>
>> This way is more elegant and thus recommended. Note that it won't output
>> anything, so you'll want to have
>>
>> - round_corners do
>> %span some text
>>
>> in your view.
>>
>> - Nathan
>>
>> On 5/11/07, Evgeny <[EMAIL PROTECTED]> wrote:
>>
>>
>>
>>
>>> Oh well...
>>>
>>> it does not work.
>>>
>>> this helper:
>>> def round_corners
>>> "<div class='rc'>#{yield}</div>"
>>> end
>>>
>>> this haml:
>>> = round_corners do
>>> %span some text
>>>
>>> renders this html:
>>> <span>some text</span>
>>> <div class='rc'>false</div>
>>>
>>> I tried variations on yield.to_s, and using a block parameter ... couldn't
>>> find something that works.
>>>
>>> Help again please ......
>>>
>>> Thank you!
>>>
>>> On 5/11/07, Evgeny <[EMAIL PROTECTED]> wrote:
>>>
>>>> Yey! So all I was missing is the "do"! :)
>>>> Thanks!
>>>>
>>>> My example for a helper was written in the mail body, not copied there
>>>> ... so I guess I wrote something that should not work, sorry. The actual
>>>> helper has different div tags.
>>>>
>>>> Thank you again!!
>>>>
>>>> Now I'll be off DRYing my code ....
>>>>
>>>> all those round corners everywhere taking 6 lines of haml will not take
>>>> 1 !
>>>>
>>>> Yey! :)
>>>>
>>>> Regards,
>>>>
>>>> - evgeny
>>>>
>>>> On 5/11/07, Nathan Weizenbaum <[EMAIL PROTECTED]> wrote:
>>>>
>>>>> The reason it's breaking is that you aren't actually passing it a
>>>>> block. What you're looking for is
>>>>> = round_corner do
>>>>> %span Some text
>>>>> Also, your helper produces invalid HTML. A closing tag can't have
>>>>> attributes.
>>>>>
>>>>> - Nathan
>>>>>
>>>>> Evgeny wrote:
>>>>>
>>>>> Greetings Haml users,
>>>>>
>>>>> I tried to write a simple helper method that will use a block to put
>>>>> haml content inside.
>>>>>
>>>>> The divs are a little bit more compilcated, but what it does is this :
>>>>> def round_corner
>>>>> "<div class='top_rc'>#{yield}</div class='bottom_rc'>"
>>>>> end
>>>>>
>>>>> And the Haml code to go with it:
>>>>>
>>>>> = round_corner
>>>>> %span Some text
>>>>>
>>>>> Any idea why it would throw an error and not work?
>>>>>
>>>>> (using haml stable)
>>>>>
>>>>> Regards,
>>>>>
>>>>> - evgeny
>>>>>
>
>
> >
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Haml" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/haml?hl=en
-~----------~----~----~----~------~----~------~--~---