2013-04-26 23 views
3

有誰知道如何在HTML頁面中打印.hbs/.handlebars模板?我到處尋找,但我不知道如何將.hbs文件放在一個目錄中,然後在我的index.html中打印它們。抱歉的問題,但我是一個新的車把手用戶。提前致謝!如何獲取並打印.hbs或.handlebars模板?

回答

7

比方說,你有一個把手模板post.handlebars

<div class="entry"> 
    <h1>{{title}}</h1> 
    <div class="body"> 
    {{body}} 
    </div> 
</div> 

使用車把預編譯器來編譯模板:

$ handlebars post.handlebars -f templates.js 

在HTML文檔的編寫模板和車把運行

<script src="/libs/handlebars.runtime.js"></script> 
<script src="templates.js"></script> 

現在編譯的模板將被訪問作爲Handlebars.templates的財產。接下來將數據傳遞給模板,生成一些html並將其附加到DOM。

<script type="text/javascript"> 
    var template = Handlebars.templates.post; 
    var context = {title: "My New Post", body: "This is my first post!"}; 
    var html = template(context); 
    
 $(document).append(html); 
</script> 

有關預編譯的詳細信息,請參閱http://handlebarsjs.com/precompilation.html。這裏還有一個很棒的把手教程:http://javascriptissexy.com/handlebars-js-tutorial-learn-everything-about-handlebars-js-javascript-templating/

1

將你的hbs文件保存爲片段文件。說myTemplate.jspf

在你的HTML/JSP文件中包含該如下

<script type="text/x-handlebars-template"> 
    <%@ include file="myTemplate.jspf" %> 
</script>