Hi, I''m wanting to use a Template for an active record object that has been converted to json. The resulting json is along these lines: var group_with_admin = {"attributes" : {"name" : "Group 1"}, "users" : {"attributes" : { "first" : "John"}}}; var groupTemplate = new Template(''#{attributes.name} is administered by #{users.attributes.first}''); groupTemplate.evaluate(group_with_admin); // -> is administered by and if i change the template to (''#{attributes} is administered by #{users}''); groupTemplate.evaluate(group_with_admin); // -> [object Object] is administered by [object Object] Obviously what I''m hoping for is to be able to render Group1 is administered by John. Is there a way of doing this using the Template class? Regards Adam --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Adam, addywaddy a écrit :> var groupTemplate = new Template(''#{attributes.name} is administered > by #{users.attributes.first}'');Currently Template only uses direct properties of the evaluated object: it doesn''t go down. That''s rather simple to fix, though. I''ll consider a patch soon about this, in the meantime here''s a working fix: evaluate: function(object) { return this.template.gsub(this.pattern, function(match) { var before = match[1]; if (before == ''\\'') return match[2]; // START OF FIX var obj = object; match[3].split(''.'').each(function(comp) { if (obj) obj = obj[comp]; }); return before + String.interpret(obj); // END OF FIX }); } -- Christophe Porteneuve aka TDD tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
A reply from the mighty Christophe! Thanks for this mate. That''ll make it child''s play for updating the page with an array of active record objects. All the best Adam On Apr 20, 11:04 am, Christophe Porteneuve <t...-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org> wrote:> Hi Adam, > > addywaddy a écrit : > > > var groupTemplate = new Template(''#{attributes.name} is administered > > by #{users.attributes.first}''); > > Currently Template only uses direct properties of the evaluated object: > it doesn''t go down. That''s rather simple to fix, though. I''ll consider > a patch soon about this, in the meantime here''s a working fix: > > evaluate: function(object) { > return this.template.gsub(this.pattern, function(match) { > var before = match[1]; > if (before == ''\\'') return match[2]; > // START OF FIX > var obj = object; > match[3].split(''.'').each(function(comp) { > if (obj) > obj = obj[comp]; > }); > return before + String.interpret(obj); > // END OF FIX > }); > } > > -- > Christophe Porteneuve aka TDD > t...-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Christophe, I mistyped my JSON: var group_with_admin = {"attributes" : {"name" : "Group 1"}, "users" : [{"attributes" : { "first" : "John"}}]}; users is an array. So I tried this: var groupTemplate = new Template(''#{attributes.name} is administered by #{users[0].attributes.first}''); Thanks to your fix, #{attributes.name} renders "Group 1", but I can''t get to "John". My active record object is the result of this finder: Group.find(:first, :include => :users) where groups have many users. I presume this isn''t such a straight-forward fix. Any pointers? This is the first time I''ve actually looked at the source code for prototype - shame on me! Regards Adam On Apr 20, 11:12 am, addywaddy <adam.gro...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> A reply from the mighty Christophe! > > Thanks for this mate. That''ll make it child''s play for updating the > page with an array of active record objects. > > All the best > > Adam > > On Apr 20, 11:04 am, Christophe Porteneuve <t...-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org> wrote: > > > Hi Adam, > > > addywaddy a écrit : > > > > var groupTemplate = new Template(''#{attributes.name} is administered > > > by #{users.attributes.first}''); > > > Currently Template only uses direct properties of the evaluated object: > > it doesn''t go down. That''s rather simple to fix, though. I''ll consider > > a patch soon about this, in the meantime here''s a working fix: > > > evaluate: function(object) { > > return this.template.gsub(this.pattern, function(match) { > > var before = match[1]; > > if (before == ''\\'') return match[2]; > > // START OF FIX > > var obj = object; > > match[3].split(''.'').each(function(comp) { > > if (obj) > > obj = obj[comp]; > > }); > > return before + String.interpret(obj); > > // END OF FIX > > }); > > } > > > -- > > Christophe Porteneuve aka TDD > > t...-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Hey Andy, addywaddy a écrit :> Thanks to your fix, #{attributes.name} renders "Group 1", but I can''t > get to "John". My active record object is the result of this finder:Yeah. I guess the fix can be made even more trivial by relying on the String#evalJSON method in sanitized mode instead of manually running the string. Basically, this just requires prefixing each dynamic fragment with "object." or something, then evalJSON''ing. I don''t have a minute to spare just now but you should try along these lines, it probably goes something like this: // START OF FIX return before + (''object.'' + match[3]).evalJSON(true); // END OF FIX -- Christophe Porteneuve aka TDD tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---