[ 3 posts ]

John DaCosta

  • Username: john.dacosta
  • Joined: Wed Mar 21, 2012 7:42 am
  • Posts: 2
  • Location: Miami, FL
  • Twitter: mr_jdacosta
  • Offline
  • Profile

Handlebars.js - Cannot Get Template to Work with Variable

Post Posted: Wed Mar 21, 2012 7:53 am
+0-
I cannot seem to get my template to render my Jobs Collection, however I can get it to render a JSON String that I pass into the template function directly.

Can anyone help me identify what i am doing wrong or if there is a bug?

The template is here:
Code:
<script id="job-template" type="text/x-handlebars-template">
   <div id="job-template-header">Job List</div>
         <ul>
         {{#items}}
         <li>{{name}}</li>
         {{/items}}
         </ul>
   </script>


The javascript is at the jump below:

http://pastie.org/3642147


The code which renders is:
Code:
var html = template({items:[{name:'Job 100'},{name:'Job 200'}]});


The code that does not work is:
Code:
 var html = template(jsonstring );

Dav Glass

  • Username: davglass
  • Joined: Thu Aug 28, 2008 9:28 am
  • Posts: 2088
  • Location: Marion, IL, US
  • Twitter: davglass
  • GitHub: davglass
  • Gists: davglass
  • IRC: davglass
  • Offline
  • Profile
Tags:

Re: Handlebars.js - Cannot Get Template to Work with Variabl

Post Posted: Wed Mar 21, 2012 8:50 am
+0-
AFAIK, a template doesn't accept a string of JSON, it's expecting an object.

John DaCosta

  • Username: john.dacosta
  • Joined: Wed Mar 21, 2012 7:42 am
  • Posts: 2
  • Location: Miami, FL
  • Twitter: mr_jdacosta
  • Offline
  • Profile

Re: Handlebars.js - Cannot Get Template to Work with Variabl

Post Posted: Wed Mar 21, 2012 10:36 am
+0-
davglass wrote:
AFAIK, a template doesn't accept a string of JSON, it's expecting an object.



Thanks, I got it to work.
When I pass the object "jobs" the template function failed.
When I stringified the object, the template function failed.
In the end the successful method was to 1. serialize object to json, then 2. to convert the json into an object:


Code:
html = template({'jobs':Y.JSON.parse(Y.JSON.stringify(jobs,null,4))});
  [ 3 posts ]
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum