2010-09-13 19 views
0

說我有一個對象,如:是否可以在Prototype模板中使用複雜的json對象?

{ 
    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'> 
     <h1><a href='/blog/post/#{id}'>#{title}</a></h1> 
     <div>#{body}</div> 
     <div><a href="mailto:#{author_email}">#{author_name}</a></div> 
    </div> 
'); 

回答

0

是的,它是可能的,只要使用點符號,看這裏http://jsfiddle.net/nBfr8/6/

我這裏改寫代碼:

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)); 

你也犯了一些句法錯誤(如遺忘撇號或雙引號),我擺脫了他們。

+0

赫爾我不知道爲什麼我沒有嘗試點符號馬上。謝謝! (重新:語法,我只是輸入了一個例子,這不是實際的代碼) – JaredM 2010-09-13 18:51:22

相關問題