Rendering A Rails Partial As A jQuery Template

A question was asked on twitter:

Screen shot 2011 06 22 at 10 52 47 PM

Here’s one of the answers that I sent to rob, in the form of a gist:

    <script id="some-template" type="x-jquery-tmpl">
      <%= render :partial => "some/partial", :locals => {:model => @template_model} %>

    <script language="javascript" type="text/javascript">
      $("#some-template").tmpl({value: "this is some data"});

I’m using this solution in my current rails app and it’s a very elegant way to use an existing partial as a jQuery template. The only issue you have to solve, then, is how to get the “${value}” syntax populated into the rendered template fields. That, too, is simple.

Given this partial:

this is a partial. <%= model.value %>

I need to turn “<%= model.value %>” into “${value}” so that the jQuery template will populate the data. To do that, we provide an @template_model instance of our model, populated with the “${value}” values, from our controller.

class SomeController < ApplicationController
  def show
    @template_model = => "${value}")

Easy-peasy. Our partial renders with the output of “this is a partial. ${value}” and when the jQuery template does its magic, it produces “this is a partial. this is some data” as the final result. From here, you can use jQueryUI’s dialog to open the rendered template as a dialog.

About Derick Bailey

Derick Bailey is an entrepreneur, problem solver (and creator? :P ), software developer, screecaster, writer, blogger, speaker and technology leader in central Texas (north of Austin). He runs - the amazingly awesome podcast audio hosting service that everyone should be using, and where he throws down the JavaScript gauntlets to get you up to speed. He has been a professional software developer since the late 90's, and has been writing code since the late 80's. Find me on twitter: @derickbailey, @mutedsolutions, @backbonejsclass Find me on the web: SignalLeaf, WatchMeCode, Kendo UI blog, MarionetteJS, My Github profile, On Google+.
This entry was posted in Javascript, JQuery, Rails. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • Anonymous

    What’s the advantage to this over sending the data as json array and keeping the template inline? I could see putting the template in one location and use the engine to render it in different places, is that what you’re trying achieve?

    • “it depends” :)

      the context was general, so i gave a general answer that could be manipulated into a specific context, easily. to specify any advantage / disadvantage, i’d have to know more about the situation. 

  • Anonymous

    I like this approach. With a little metaprogramming you can implement a model that responds to any arbitrary method with “${method_name}” and use it to convert templates.

    • that would be nice. i’ve been resorting to building my own .to_template_json methods for the objects that need it. the method returns a hash of the objects attributes, with ${attr} as the value. tedious, but works… a bit of meta-programming would certainly help a lot :)