[英]dust.js render dynamic content
我的模板中包含以下代碼,該代碼在加載頁面時提供所需的輸出。
有沒有辦法讓塵埃動態渲染頁面,因為我正在使用套接字更新數據
{#storylines}
<li>{text|bl|s} <span class="badge yellow">{@negidx}{.}{/negidx}</span></li>
{/storylines}
套接字IO代碼:
socket.on('updatechat', function (username, data) {
$('#newstoryline').before(blurlines(data));
});
我嘗試將<span class="badge yellow">{@negidx}{.}{/negidx}</span>
到數據末尾,但輸出為{@negidx}{.}{/negidx}
-有什么方法可以使dust.js呈現最新數據? 還是我必須使用某種jQuery而不是{@negidx}{.}{/negidx}
?
如果要在客戶端渲染模板,這應該很容易。 您的代碼如下所示:
socket.on('updatechat', function (username, data) {
if (data) {
dust.render('storyline', data, function(err, output) {
if (output) {
$('#newstoryline').before(output);
}
});
}
});
這里發生了什么:
返回的數據應為JSON。 檢查是否已返回某些內容。
if (data) {
使用dust
使用返回的數據渲染模板。
dust.render('storyline', data, function(err, output) {
檢查output
:
if (output) {
將output
插入頁面。 通常,這是使用innerHTML完成的,但是您可以使用任何適合您的方法。 請記住,此時output
為字符串。
$('#newstoryline').before(output);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.