Home > HamlTojQueryTemplates

HamlTojQueryTemplates

HamlTojQueryTemplates is a project mainly written in Ruby, it's free.

creates jquery templates from haml, including rails helpers

This library will allow you to reuse your haml views as jQuery templates. This way, you can write your view once and render it directly from a controller or from the client with an ajax call. The main features are control flow parsing and reordering, and template objects that will use method_missing to replace ruby method calls with their analogous jQuery template variable names.

You can use rails helpers within the partials and even add your own helpers to the scope.

Add this folder to your lib directory. Then create a helper:

module JsTemplateHelper require "haml_file"

def haml_js_template(current_scope, partial_name, options = {}) haml_tag :script, :type => "text/x-jquery-tmpl", :id => options[:as] || "#{partial_name}_template" do haml_concat HamlFile.new("#{RAILS_ROOT}/app/views/#{partial_name}.html.haml", :scope => current_scope, :with => options[:with]).render_js_template end end

end

In your view add a line such as:

  • haml_js_template(self, "admin/content_module_guides/_show", :as => "content_module_guide_template", :with => :content_module_guide)

The attributes are the scope (in this case self, the actionview), the partial you want to convert, and options such as what you want the template to be called and what the variables will be that need to have their method calls converted.

Finally, in your js file, you can render the template as follows:

$("#new_content_module_guide_form").live("submit", function(event) { event.preventDefault();

$.ajax({
      url: $(event.target).attr("action"),
      type: "post",
      data: $(this).serialize(),
       dataType:"json",
       success: function (data) {
             $("#existing_content_module_guides").append($("#content_module_guide_template").tmpl(data));
       }
    });

});

Previous:redis-net