开发者

Is it possible to use a complex json object with a Prototype template?

开发者 https://www.devze.com 2023-01-16 03:44 出处:网络
Say I have an object such as: { id: 345, title: \'Some title\', body: \'Here be a lot of text\', author: {

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消