# Can helpers manipulate block?

**URL:** <https://www.ruby-forum.com/t/can-helpers-manipulate-block/108842>\
**Category:** Rails\
**Created:** [July 8, 2007, 1:49am UTC](https://www.ruby-forum.com/t/can-helpers-manipulate-block/108842 "2007-07-08T01:49:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Bannister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@Michael\_Bannister](https://www.ruby-forum.com/u/Michael_Bannister)\
**Post date:** [July 8, 2007, 1:49am UTC](https://www.ruby-forum.com/t/can-helpers-manipulate-block/108842/1 "2007-07-08T01:49:44Z")

</div>

hi,

i’m unsure the best way to ask this but i’m wondering if a rails  
helper can manipulate html. for instance if i had a helper that looked  
like this:

\<% my\_helper do %\>  
  
  
\<% end %\>

is there a way i can programatically work with the html content (i.e.  
  
)? is there a variable available in my helper that represents  
the content between the helper and end tags?

thanks in advance, mike

---

<div class="post-metadata">

**Author:** ![Michael\_Bannister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@Michael\_Bannister](https://www.ruby-forum.com/u/Michael_Bannister)\
**Post date:** [July 8, 2007, 2:47am UTC](https://www.ruby-forum.com/t/can-helpers-manipulate-block/108842/2 "2007-07-08T02:47:47Z")

</div>

Hi –

On Sat, 7 Jul 2007, Michael Bannister wrote:

> is there a way i can programatically work with the html content (i.e.  
>   
> )? is there a variable available in my helper that represents  
> the content between the helper and end tags?

Yes; you could do it like this:

def my\_helper(&block)  
string = capture(&block)  
concat(do\_something\_to(string), block.binding)  
end

capture grabs the content from the template; concat inserts the  
manipulated string into the output stream. You have to pass  
block.binding along so that concat is operating in the right scope.

David

–

- Books:  
RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242](http://www.awprofessional.com/title/0321509242))  
RUBY FOR RAILS ([Ruby for Rails](http://www.manning.com/black))
- Ruby/Rails training  
& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![Michael\_Bannister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@Michael\_Bannister](https://www.ruby-forum.com/u/Michael_Bannister)\
**Post date:** [July 8, 2007, 3:18am UTC](https://www.ruby-forum.com/t/can-helpers-manipulate-block/108842/3 "2007-07-08T03:18:49Z")

</div>

Michael Bannister wrote:

> the content between the helper and end tags?  
> Unfortunately yes. Inside my\_helper, use capture and yield. The return  
> value  
> of capture(yield), IIRC, is the HTML.

capture is sensitive to call because eRB mangles your HTML before  
evaluating  
it, and capture naturally sees the mangled version. Google for capture  
to  
see how to use it. Then use \<%= my\_helper, and return your version of  
the  
captured HTML.

And please reconsider. There’s always a way to write the correct HTML  
the  
first time; you shouldn’t want to search and replace inside it. For  
example:

\<% my\_helper do |creator| %\>  
  
  
\<%= creator.create\_html %\>  
  
  
\<% end %\>

Now my\_helper initializes a creator, and this creates more HTML and  
inserts  
it into the block. This is generally what blocks are for - to mix and  
match  
objects from outside the block (like eRB) with objects inserted into the  
block by the goal-posts operator ||.

–  
Phlip  
[Test Driven Ajax (on Rails) [Book]](http://www.oreilly.com/catalog/9780596510657/)  
“Test Driven Ajax (on Rails)”  
assert\_xpath, assert\_javascript, & assert\_ajax
