【发布时间】:2012-03-14 12:17:00
【问题描述】:
几个月前,我在 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);
闪亮的新翡翠
- 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 的要求,用于呈现Jade 文件的代码(基本上与Express 示例相同)。
res.render("search", { view: view, settings: vgSettings.app });
【问题讨论】:
-
你能展示一下你用来渲染jade文件的代码吗?
-
@loganfsmyth 添加了它,尽管 PuerkitoBio 在下面的回答是我想要做的。 (抱歉反应慢 - 是就寝时间。)