幾個月前,我在CodeIgniter中編寫了一個視頻庫Web應用程序,並試圖使用Express + Jade將後端移動到Node上以生成內容。 PHP版本將JSON發送到前端,通常包括大量的HTML。如何通過JSON輸出Jade內容? (Node.js)
我想呈現與Jade的HTML位(因爲這是它的存在),但顯然我想輸出它以JSON格式,而不是直接瀏覽器。如何做到這一點可能非常明顯,但目前我無法弄清楚......非常感謝任何幫助。
原始PHP
ob_start();
for ($i = $start; $i < $end; $i++)
{
$v = $videos[$i];
echo '<a id="video-' . $v->url . (isset($v->otherid) ? '-' . $v->otherid : '') . '" href="#!' . Settings::$pretty_link . '/' . $v->url . (isset($v->otherid) ? '/' . $v->otherid : '') . '" class="Box' . ($i == $start ? " Batch" : "") . '" title="Click to view the ' . $v->name . '"><img src="' . site_url('images/thumb/' . $v->image) . '" alt="The ' . $v->name . '" /><span>' . $v->name . '</span></a>';
}
$data[$html] = ob_get_clean();
// Add some other things to $data
echo json_encode($data);
閃亮新玉
- var v
- for (var i = view.start; i < view.end; i++) {
- v = view.videos[i]
a(id="#{v.url + (v.otherid === null ? "" : "-" + v.otherid)}",
href="#!#{settings.prettyLink + "/" + v.url + (v.otherid === null ? "" : "/" + v.otherid)}/",
class="Box #{i == view.start ? "Batch" : ""}",
title="Click to view the #{v.name}"
)
img(src="#{settings.siteUrl}images/thumb/#{v.image}", alt="The #{v.name}")
span #{v.name}
- }
我該怎麼做了$data[$html] = ob_get_clean();
線? (可能還有json_encode
一個,雖然我目前有JSON.stringify
寄予厚望。:)
編輯作爲@loganfsmyth要求,用於渲染玉文件中的代碼(基本上只是一樣快的例子)。
res.render("search", { view: view, settings: vgSettings.app });
你可以顯示你用來渲染玉文件的代碼嗎? – loganfsmyth 2012-02-24 02:50:52
@loganfsmyth添加了它,雖然PuerkitoBio的答案在下面是我正在嘗試做的。 (對不起,緩慢的回覆 - 就寢時間。) – meloncholy 2012-02-24 14:59:07