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: bob@example.com
}
}
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开发者_如何学C:#{author_email}">#{author_name}</a></div>
</div>
');
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: 'bob@example.com'
}
};
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.
精彩评论