This is an example snippet from a library im working on
example.rb · GitHub I need to be able to ‘buffer’ or collect
the results without yielding an object to any number of nested blocks.
I know this can be done with method_missing etc but my brain hurts haha
and for some stupid reason im stuck with this issue and have the
incorrect output below
Output:
On 29.06.2009 22:39, Tj Holowaychuk wrote:
It seems you are trying to solve not a buffering problem but rather want
some kind of context present for every tag you generate. You can do so
by having the context in an object.
class Context
def initialize
@out = “”
end
def tag(name, &b)
if b
@out << “<” << name << “>”
instance_eval(&b)
@out << “</” << name << “>”
else
@out << “<” << name << “/>”
end
end
def text(s)
@out << s
end
end
def markup(&b)
c = Context.new
c.instance_eval(&b)
c
end
mup = markup do
tag “foo” do
tag “bar” do
text “blah”
end
tag "empty"
end
end
Kind regards
robert
Thanks for the reply!
Buffer is a bad choice of wording on my part (lack of a better name for
the module).
The library is http://github.com/visionmedia/tagz/tree/master which is
used by http://github.com/visionmedia/tagz/tree/master
Tagz is the one with the Tagz::Buffer module with this issue. The code
is pretty much the same as what I posted above. Everything works fine
until a method uses #tag to create nested tags, for example in Formz the
#fieldset method returns fieldset markup and a legend in the markup as
well so two calls to #tag in my current version start to duplicate
markup
Hope that sort of makes sense :s