2015-11-23 37 views
1

我試圖在我的web-app上使用mustache.js。所以,我收取的mustache.js和我的劇本我做的:小鬍子.js在html模板中顯示

<script type="text/javascript"> 
    $(document).ready(function() { 
     var person = { 
      firstName: "Christophe", 
      lastName: "Coenraets", 
      blogURL: "http://coenraets.org" 
     }; 
     var template = "<h1>{{ firstName }} {{ lastName }}</h1>Blog: {{ blogURL }}"; 
     var html = Mustache.to_html(template, person); 
     console.log(html); 
     $('#userListWrapper').html(html); 
    }); 
</script> 

的問題是,在我的console.log(html)我得到:<h1> </h1>Blog:,所以鬍子不被解釋。你能幫我嗎 ?我不明白問題在哪裏。 Thx提前。

回答

0

您的代碼正在運行,您只需確保包含這些庫。

<head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 

<script src="https://cdnjs.cloudflare.com/ajax/libs/mustache.js/2.2.0/mustache.js"></script> 
</head> 
<p id="userListWrapper"></p> 
<script type="text/javascript"> 
    $(document).ready(function() { 
     var person = { 
      firstName: "Christophe", 
      lastName: "Coenraets", 
      blogURL: "http://coenraets.org" 
     }; 
     var template = "<h1>{{ firstName }} {{ lastName }}</h1>Blog: {{ blogURL }}"; 
     var html = Mustache.to_html(template, person); 
     console.log(html); 
     $('#userListWrapper').html(html); 
    }); 
</script> 
+0

我使用樹枝也許這是問題,不是嗎? –

+0

嗯,如果沒有鏈接到這個庫,這是一個問題,你正在使用原始問題中的鬍子對象。你也可以在這裏查看 - > http://stackoverflow.com/questions/10862521/how-to-output-mustache-templates-in-a-twig-template –