# can helpers manipulate block?

**URL:** <https://discuss.rubyonrails.org/t/can-helpers-manipulate-block/15408>\
**Category:** rubyonrails-talk\
**Created:** [July 7, 2007, 11:49pm UTC](https://discuss.rubyonrails.org/t/can-helpers-manipulate-block/15408 "2007-07-07T23:49:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Bannister](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@Michael\_Bannister](https://discuss.rubyonrails.org/u/Michael_Bannister)\
**Post date:** [July 7, 2007, 11:49pm UTC](https://discuss.rubyonrails.org/t/can-helpers-manipulate-block/15408/1 "2007-07-07T23:49:09Z")

</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 %\> &nbsp;&nbsp;\<br /\> \<% end %\>

is there a way i can programatically work with the html content (i.e. \<br /\>)? 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:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://discuss.rubyonrails.org/u/David_A_Black1)\
**Post date:** [July 8, 2007, 12:46am UTC](https://discuss.rubyonrails.org/t/can-helpers-manipulate-block/15408/2 "2007-07-08T00:46:44Z")

</div>

Hi --

> 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 %\> \<br /\> \<% end %\>
> 
> is there a way i can programatically work with the html content (i.e. \<br /\>)? 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:

&nbsp;&nbsp;&nbsp;def my\_helper(&block) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string = capture(&block) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;concat(do\_something\_to(string), block.binding) &nbsp;&nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![Phlip](https://avatars.discourse-cdn.com/v4/letter/p/ed8c4c/32.png) [@Phlip](https://discuss.rubyonrails.org/u/Phlip)\
**Post date:** [July 8, 2007, 1:18am UTC](https://discuss.rubyonrails.org/t/can-helpers-manipulate-block/15408/3 "2007-07-08T01:18:00Z")

</div>

Michael Bannister wrote:

> 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 %\> \<br /\> \<% end %\>
> 
> is there a way i can programatically work with the html content (i.e. \<br /\>)? is there a variable available in my helper that represents 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| %\> &nbsp;&nbsp;&nbsp;\<br /\> &nbsp;&nbsp;&nbsp;\<%= creator.create\_html %\> &nbsp;&nbsp;&nbsp;\<br /\> \<% 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 ||.
