views:

10

answers:

1

Say I have an object such as:

{
    id: 345,
    title: 'Some title',
    body: 'Here be a lot of text',
    author: {
         id: 1
         name: Bob
         email: [email protected]
    }
}

How would I reference the properties of the author in my template

e.g.,

var template = new Template('
     <div class='blog_post'>
         <h1><a href='/blog/post/#{id}'>#{title}</a></h1>
         <div>#{body}</div>
         <div><a href="mailto:#{author_email}">#{author_name}</a></div>
     </div>
');
A: 

Yes it is possible, just use the dot notation, look here http://jsfiddle.net/nBfr8/6/

I rewrite code here:

var o = {
    id: 345,
    title: 'Some title',
    body: 'Here be a lot of text',
    author: {
         id: 1,
         name: 'Bob',
         email: '[email protected]'
    }
};
var template = new Template(
     '<div class="blog_post">\n' +
         '\t<h1><a href="/blog/post/#{id}">#{title}</a></h1>\n' +
         '\t<div>#{body}</div>\n' +
         '\t<div><a href="mailto:#{author.email}">#{author.name}</a></div>\n' +
     '</div>'
);

alert(template.evaluate(o));

You also made some syntactic mistakes (like forgetting apostrophes or double quotes), I got rid of them.

pepkin88
Herp I don't know why I didn't try the dot notation right off. Thanks! (re: syntax, I just typed out an example this isn't actual code)
JaredM