Forum: Ruby on Rails can helpers manipulate block?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B0c31b2dad1edd570adff34f672e6314?d=identicon&s=25 Michael Bannister (Guest)
on 2007-07-08 01:49
(Received via mailing list)
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?

thanks in advance, mike
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-07-08 02:47
(Received via mailing list)
Hi --

On Sat, 7 Jul 2007, Michael Bannister wrote:

> 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:

   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)
   RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
     & consulting:  Ruby Power and Light, LLC (http://www.rubypal.com)
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2007-07-08 03:18
(Received via mailing list)
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| %>
   <br />
   <%= creator.create_html %>
   <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 ||.

--
  Phlip
  http://www.oreilly.com/catalog/9780596510657/
  "Test Driven Ajax (on Rails)"
  assert_xpath, assert_javascript, & assert_ajax
This topic is locked and can not be replied to.