how to use open_tag?

3 views
Skip to first unread message

Tal

unread,
Nov 20, 2009, 1:24:10 PM11/20/09
to Haml
I'm wanting to make helpers and I've come across open_tag in a few
places. I can't figure out where it's used though. Do i just have to
be sure to call it from within something being rendered by haml? or
can i get it to actually output text?

Hampton

unread,
Nov 23, 2009, 8:29:09 AM11/23/09
to ha...@googlegroups.com
Open tag, in a helper, will automatically append itself to the document.

So... you might have the line in your haml "- print_nav" (the - means "no output")

however, the method goes like this

def print_nav
  open_tag :div, :class => "nav" do
    open_tag "a", "Nav!"
  end
  return true
end

This will come out with

<div class='nav'>
  <a>Nav!</a>
</div>

The meaning of the "return true" is just to demostrate that the return value doesn't matter... since we are using -. As soon as open_tag or push_text or other haml-helper methods are called, the results are added to the response document.

Make sense?

-hampton.


--

You received this message because you are subscribed to the Google Groups "Haml" group.
To post to this group, send email to ha...@googlegroups.com.
To unsubscribe from this group, send email to haml+uns...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/haml?hl=.



Mislav Marohnić

unread,
Nov 23, 2009, 8:37:38 AM11/23/09
to ha...@googlegroups.com
On Mon, Nov 23, 2009 at 14:29, Hampton <hca...@gmail.com> wrote:

Make sense?

No. How is it different than `haml_tag` 

Hampton

unread,
Nov 23, 2009, 9:00:28 AM11/23/09
to ha...@googlegroups.com
Oh god. Did I just totally have a brain-fail?

-hampton.

--

Mislav Marohnić

unread,
Nov 23, 2009, 9:41:08 AM11/23/09
to ha...@googlegroups.com
On Fri, Nov 20, 2009 at 19:24, Tal <swimmi...@gmail.com> wrote:
I'm wanting to make helpers and I've come across open_tag in a few
places. I can't figure out where it's used though.

If you really meant `open_tag` and not `haml_tag`, I don't see how you could have seen it in places other than internal Haml code, because it's part of a private Haml::Buffer API. In fact, I can only find its definition but not where it's called from:

    def open_tag(name, self_closing, try_one_line, preserve_tag, escape_html, class_id,
                 nuke_outer_whitespace, nuke_inner_whitespace, obj_ref, content, *attributes_hashes)
      ...
    end

Nice attribute list :)

Anyway, regardless of whether this method is actually used internally or not, you should not use it. `haml_tag` is a pretty good option for view helpers and it's used like Hampton described in his reply.

Nathan Weizenbaum

unread,
Nov 23, 2009, 1:48:58 PM11/23/09
to ha...@googlegroups.com
open_tag is used internally; it's called from the compiled Ruby source, like most Haml::Buffer methods.

--

Reply all
Reply to author
Forward
0 new messages