如何使用Tempo.js进行JSON模板化?

时间:2013-02-13 19:51:23

标签: javascript json templates tempo

我正在尝试使用Tempo.js进行模板化,文档很稀疏。 请尝试在SO中进行,而不是jsFiddle。

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <head>

            <script type="text/javascript" src="https://raw.github.com/twigkit/tempo/master/tempo.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>


        <script type="text/javascript" charset="utf-8">

            var data = [
                    {'name':{'first':'Leonard','last':'Marx'},'nickname':'Chico','born':'March 21, 1887','actor': true,'solo_endeavours':[{'title':'Papa Romani'}]},
                    {'name':{'first':'Adolph','last':'Marx'},'nickname':'Harpo','born':'November 23, 1888','actor':true,'solo_endeavours':[{'title':'Too Many Kisses','rating':'favourite'},{'title':'Stage Door Canteen'}]},
                    {'name':{'first':'Julius Henry','last':'Marx'},'nickname':'Groucho','born': 'October 2, 1890','actor':true,'solo_endeavours':[{'title':'Copacabana'},{'title':'Mr. Music','rating':'favourite'},{'title':'Double Dynamite'}]},
                    {'name':{'first':'Milton','last':'Marx'},'nickname':'Gummo','born':'October 23, 1892'},
                    {'name':{'first':'Herbert','last':'Marx'},'nickname':'Zeppo','born':'February 25, 1901','actor':true,'solo_endeavours':[{'title':'A Kiss in the Dark'}]}
                ];

            Tempo.prepare('marx-brothers').render(data);

        </script>       

    </head>
    <body>

    <ol id="marx-brothers">
        <li data-template>{{nickname}} {{name.last}}</li>
    </ol>

    </body>
</html>

1 个答案:

答案 0 :(得分:2)

您尝试在DOM之前运行Tempo.prepare调用,并使用其引用的ID加载。

将脚本标记放在</body>结束标记之前,或者在运行代码之前等待DOM准备就绪,使用window.onload或jQuery {{1}模式。