I have a rails 3 app that grabs a ‘post’ via ajax and inserts the returned json into a mustache template, I am using Poirot for both client and serverside templating but I’m having a problem rendering the template, after the ajax call, with my defined view class methods.
The methods work fine when rendering the mustache template from the server in rails but the methods don’t work in the javascript template and I’m not sure how to render the data inserted into the js template outside of the js template afterwards.
If my View Class look like:
module Posts
class PostView < Poirot::View
def user_url
user_path(post.user_id)
end
def user_name
post.user.name
end
end
end
and my template looks like:
<h1>{{ title }}</h1> //posts title
<p>Author: <a href="{{ user_url }}"/>{{ user_name }}</a>
This works fine when I just:
<%= render 'post' %>
… all the definitions are included and it outputs as I expect.
But when I render this template from javascript:
poirot.post(data)
…all the view class methods are not available.
So how do I extract the data out of the js template and insert it as regular html/mustaches???
I thought something like this would work:
var html = poirot.post(data)
$('#showPost').append(html);
But I still get the template with no view class methods. I’m quite new to js so any help would be much appreciated.
Thanks
The poirot view class can only be interpreted by ruby so when rendering a mustache template from the server you have access to the view class methods, but when rendering the template on the client via javascript you no longer have access to these methods.
Handlebars.js is an extension to the Mustache templating language. Handlebars.js allows you to write javascript helper methods to use in templates.