Skip to content

Instantly share code, notes, and snippets.

@panych
Last active December 26, 2015 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save panych/7118574 to your computer and use it in GitHub Desktop.
Save panych/7118574 to your computer and use it in GitHub Desktop.
Block alias in Ruby on Rails
# application_helper.rb
def b(block_name, options={}, &block)
options.merge!(:body => capture(&block)) if block_given?
render :partial => block_name, :locals => options
end
<!-- Usage: -->
<!-- home/index.html.erb -->
<%= b 'nav', :items => @nav_items %>
<%= b 'article' do %>
<h1>Title</h1>
<p>lorem</p>
<% end %>
<!-- old style -->
<%= render :partial => 'nav', :locals => { :items => @nav_items } %>
<%= render :layout => 'article_old' do %>
<h1>Title</h1>
<p>lorem</p>
<% end %>
<nav>
<% items.each do |item| %>
<a href="<%= item[:url] %>"><%= item[:text] %></a>
<% end %>
</nav>
<article>
<%= body %>
</article>
<article>
<%= yield %>
</article>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment