From: Steve V Date: 2005-04-20T02:24:19+09:00 Subject: Re: Store erb block and alter scope of erb block eval? > Is there something special erb does when you capture a block from an rhtml > page? I tried passing my own variable values to the block within my > class(separate from the controller), and the values are ignored. > I think the > heart of the problem is that you're calling ERB.new on a regular string. I > need to capture this information from an rhtml page. The only way I have > found to do that is by using a "do" block. > > r.content do%> > Test <%=@sometext%> > <%end%> > > This gives me a block, and not a string. Is there a way to turn > the contents > of the returned proc as a string? to_s just gives me the memory > location(?) > of the proc object. > > The other question is why aren't my variables used for the block call when > I'm in the class? Does it have something to do with the existing scope of > the proc object? Can I change the scope? The only place I can get that > @sometext value to be used from is within the controller. I have tried > defining @sometext within my separate class, and passing that > into the call, > but nothing happens. > > def content(&block) > @sometext = "sometext" > @data = block.call(@sometext) > end > > def render() > return @data > end > > The above will just render "Test ", and not use the specified value for > @sometext. After some more experimenting, I have made a little more progress, but I'm still not able to use a compiled ERB template. <%r.data = %q{%> So close! <%=blah%> <%}%> The above rhtml code will get me a string into my class, but it isn't "\t\tSo close! <%=blah%>\n\t" as expected. Instead it is what appears to be some type of parsed string: "; _erbout.concat "\n" _erbout.concat "\t\tSo close! "; _erbout.concat((@blah).to_s); _erbout.concat "\n" _erbout.concat "\t"; " I can use eval() on the above, with the binding of the working class which sort of solves the problem. Can anyone tell me what piece I'm missing here to keep the code suitable for loading into an ERB template? Thanks, Steve