Better Partials Plugin for Rails

Posted 3 months ago by jacques | 6 comments

Wesley made a good post yesterday about using helper methods to call partials.

I’ve always liked the approach of wrapping all my render :partial calls in helpers. However on big projects I found this to be a big mess. Too much code. I hate code.

The real problem here is that render :partial is just ugly. And if you have well partitioned Rails view, it’s absolutely everywhere. render :partial here, render :partial there. And :locals everywhere. And why does this have to be a separate hash? Clearly something needs to be done about all this.

# introducing...
better_partials
script/plugin install http://betterpartials.googlecode.com/svn/trunk/better_partials

Syntactic Candy

You can now render a partial with:

<%= partial "people/form" %>

You can pass in any arguments you want, nice and easy:

<%= partial "people/form", :form => form, :show_header => true %>

It treats :collection, :object, :spacer_template, and :use_full_path special. So you can still use these options like you would normally with render :partial.

<%= partial "people/person", :collection => @people %>

The terse approach still works also:

<%= partial @people %>

Partials with Blocks. Ever use these?

Adding yield statements to partials is a great way to get maximum reuse out of your views. However using render, you used to have to remember to call render :layout instead. I always accidently typed the wrong thing and wondered why it was ignoring my block.

Now it’s nice and simple:

<% partial "boxes/fancy" do %>
  put your fancy box content here..
<% end %>

Your boxes/_fancy.html.erb might then look like this:

<div class="fancy-box">
  <h2>Fancy Header</h2>
  <%= yield %>
</div>

This is especially awesome when using Haml since its so natural to use blocks everywhere in your view. I made sure the plugin works perfectly in Haml since that’s the view framework I use on all my projects. Other view frameworks should be fine too since I’m not doing anything too fancy (just wrapping around render :partial).

Sample Project

You can download a sample project that has a basic scaffold generated. I refactored it to use partials in lots of different ways. Might help you wrap your mind around it (especially playing around with the partial blocks).

Download Download Sample Project

Also, if you have any good ideas on how to write some good unit tests for a plugin like this (rendering partials and such), let me know. I tried but I couldn’t configure all the Rails paths when executing the tests.

If you see a bug, submit it to my Google Code Project Page and I’ll get it fixed right away. The codebase is next to nothing, but I use this on a bunch of my projects so the more bug fixes the better.


Add a Comment

or cancel

Reader Comments

  1. zerohalo 3 months ago
    Pixel

    Great idea! Thanks.

  2. Scott Becker 2 months ago
    Pixel

    That's great. Looks like something that should be in core to me.

  3. Jason 2 months ago
    Pixel

    Nice... Especially partials with blocks!

  4. Maximilian Schulz 2 months ago
    Pixel

    That looks awesome. I think it will simplify a lot of my work with partials. I would vote for including it into core, too!

  5. Ist hier viel erledigte Arbeit, offensichtlich. Guter Aufstellungsort !~ http://www.alasundmeadows.com/casa-vacanza-livorno 2 days ago
    Pixel

    Ist hier viel erledigte Arbeit, offensichtlich. Guter Aufstellungsort !~ http://www.alasundmeadows.com/casa-vacanza-livorno

  6. ..was just passing by.. good work http://www.camerette-per-ragazzi.nceatracker.com 2 days ago
    Pixel

    ..was just passing by.. good work http://www.camerette-per-ragazzi.nceatracker.com