Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6367705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:35:00+00:00 2026-05-25T00:35:00+00:00

I’d like to to something nested like that in my views: <%= helper_a do

  • 0

I’d like to to something nested like that in my views:

<%= helper_a do |ha| %>
  Content for a
  <%= ha.helper_b do |hb| %>
    Content for b
    <%= hb.helper_c do |hc| %>
      Content for c
      ... and so on ...
    <% end %>
  <% end %>
<% end %>

To get for example this:

<tag_a>
  Content for a
  <tag_b class="child_of_tag_a">
    Content for b
    <tag_c class="nested_child_of_tag_a child_of_tag_b">
      Content for c
    </tag_c>
  </tag_b>
</tag_a>

This means, each level has access to some information of the level above (that’s why they are nested and not completely autonomous methods)

I know how to create a simple helper:

def helper_a(&block)
  content = capture(&block)
  content_tag :tag_a, content
end

And I know I can pass my arguments to the capture to use them in the view, so something like this to get live up the |ha| of my example

def helper_a(&block)
  content = capture(OBJECT_HERE, &block)
  content_tag :tag_a, content
end

But where do I define this OBJECT_HERE, especially the class for it, and how can this go on nested with multiple levels capturing each block?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:35:00+00:00Added an answer on May 25, 2026 at 12:35 am

    I came up with a couple solutions, but I’m far from being an expert in the Rails templating system.

    The first one is using an instance variable :

    def helper_a(&block)
      with_context(:tag_a) do
        content = capture(&block)
        content_tag :tag_a, content
      end
    end
    
    def helper_b(&block)
      with_context(:tag_b) do
        content = capture(&block)
        content_tag :tag_b, content
      end
    end
    
    def helper_c(&block)
      with_context(:tag_c) do
        content = capture(&block)
        content_tag :tag_c, content
      end
    end
    
    def with_context(name)
      @context ||= []
      @context.push(name)
      content = yield
      @context.pop
      content
    end
    

    which is used this way :

    <%= helper_a do %>
      Content for a
      <%= helper_b do %>
        Content for b
        <%= helper_c do %>
          Content for c
          ... and so on ...
        <% end %>
      <% end %>
    <% end %>
    

    And the other solution, which passes the context at each step :

    def helper_a(context = [], &block)
      context = capture(context.push(:tag_a), &block)
      content_tag(:tag_a, content)
    end
    
    def helper_b(context = [], &block)
      context = capture(context.push(:tag_b), &block)
      content_tag(:tag_b, content)
    end
    
    def helper_c(context = [], &block)
      context = capture(context.push(:tag_c), &block)
      content_tag(:tag_c, content)
    end
    

    which is used this way :

    <%= helper_a do |context| %>
      Content for a
      <%= helper_b(context) do |context| %>
        Content for b
        <%= helper_c(context) do |context| %>
          Content for c
          ... and so on ...
        <% end %>
      <% end %>
    <% end %>
    

    But I’d really advise against using either of these solutions if all you’re doing is CSS styling and/or Javascript manipulation. It really complicates the helpers, is likely to introduce bugs, etc.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have some data like this: 1 2 3 4 5 9 2 6
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
this is what i have right now Drawing an RSS feed into the php,
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.