如何通过JSON输出Jade内容?(Node.js的)

mel*_*oly 1 json node.js express pug

我几个月前在CodeIgniter中写了一个视频库网络应用程序,我试图将后端移动到Node,使用Express + Jade来生成内容.PHP版本将JSON发送到前端,通常包括大量的HTML.

我想用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);
Run Code Online (Sandbox Code Playgroud)

闪亮的新玉

- 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}
- }
Run Code Online (Sandbox Code Playgroud)

我该怎么做$data[$html] = ob_get_clean();?(也可能是那个json_encode,虽然我目前寄予厚望JSON.stringify.:)

编辑为@loganfsmyth请求,用于呈现Jade文件的代码(基本上与Express示例相同).

res.render("search", { view: view, settings: vgSettings.app });
Run Code Online (Sandbox Code Playgroud)

mna*_*mna 5

没有尝试过,虽然从Express文档中,这应该工作:

res.render('yourJadeView', { someOptionalLocal: 'someValue' }, function(err, string) {
    res.json({html: string});
});
Run Code Online (Sandbox Code Playgroud)