Helper rendering block within partial

11 views
Skip to first unread message

Paulo Fabiano Langer

unread,
Dec 28, 2017, 4:55:49 PM12/28/17
to Padrino Framework
Hi, guys!

I've been struggling with this a while and I decided to ask for help.

In rails, I created a helper method for rendering a custom partial, like this:

def component(component_name, locals = {}, &block)
    name = component_name.split("_").first
    render("components/#{name}/#{component_name}", locals, &block)
end

So, in the view, I can do something like this:

_box.html.erb

<div class='box'>
   <%= yield %>
</div>

_message.html.erb

<div class='message'>
   <%= yield %>
</div>

<% component("box") do %>
  <% component("message") do %>
    <p>Hello World!</p>
  <% end %>
<% end %>

And it outputs

<div class='box'>
   <div class='message'>
     <p>Hello World!</p>
   </div>
</div>

I really need to create it in Padrino but I can't do it.
Could anyone help me?

Thanks,
Paulo

Adam Daniels

unread,
Dec 31, 2017, 12:52:14 PM12/31/17
to Padrino Framework
I simplified the helper so I could make sure it worked, but you can add back in your component_name stuff as you see fit. Confirmed working.

def component(name, locals={}, &block)
  partial("components/#{name}", { locals: locals }, &block)
end

 Hope this helps.

Adam

Paulo Fabiano Langer

unread,
Jan 2, 2018, 5:43:33 AM1/2/18
to Padrino Framework
Thanks a lot, Adam!

It helps very much :)

Happy new year.

Att.,
Paulo
Reply all
Reply to author
Forward
0 new messages