String Buffering Issue

T

Tj Holowaychuk

This is an example snippet from a library im working on
http://gist.github.com/137785 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:
<ul ><a href="/users"></a>
<li ><a href="/users"></a>
</li>
<a href="/reports"></a>
<li ><a href="/users"></a>
<li ><a href="/users"></a>
</li>
<a href="/reports"></a>
</li>
<a href="/content"></a>
<li ><a href="/users"></a>
<li ><a href="/users"></a>
</li>
<a href="/reports"></a>
<li ><a href="/users"></a>
<li ><a href="/users"></a>
</li>
<a href="/reports"></a>
</li>
<a href="/content"></a>
</li>
</ul>
 
R

Robert Klemme

This is an example snippet from a library im working on
http://gist.github.com/137785 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:
<ul ><a href="/users"></a>
<li ><a href="/users"></a>
</li>
<a href="/reports"></a>
<li ><a href="/users"></a>
<li ><a href="/users"></a>
</li>
<a href="/reports"></a>
</li>
<a href="/content"></a>
<li ><a href="/users"></a>
<li ><a href="/users"></a>
</li>
<a href="/reports"></a>
<li ><a href="/users"></a>
<li ><a href="/users"></a>
</li>
<a href="/reports"></a>
</li>
<a href="/content"></a>
</li>
</ul>

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
 
T

Tj Holowaychuk

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,174
Messages
2,570,941
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top