【发布时间】:2015-05-02 20:01:53
【问题描述】:
首先我想说我是 Node.js 的新手。
我的问题是这样的。有没有一种方法可以将单个布局用于 HTML?分别类似于 MCV 和 ASP 中的 shared views 和 Master Layouts。
例如:
//。 hello_world.hjs
<!DOCTYPE html>
<html>
<head>
<title>{{ title }}</title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<h1>{{ title }}</h1>
<div id="content">
<!-- Place changing page content here -->
{{> part }}
</div>
<footer>
{{date}}
</footer>
</body>
</html>
//。 hello_world.js
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('hello_world', {
title: 'Hello World!',
partials: {
part: 'part'
}
});
});
//。 part.html
<h1>This is a part</h1>
我只希望在 hello_world.hjs 中更改 id 为“content”的 div 中的内容,这样当对页脚或导航栏进行更新时,就不必更改对每个文件进行制作。
文件夹结构->
routes
-- hello_world.js
views
--hello_world.hjs
--part.html
--index.hjs
我已尝试寻找解决方案。但是,我不确定我在搜索中是否使用了正确的术语。
【问题讨论】:
-
首先,这实际上与 Node.js 或 Express 没有任何关系,这就是为什么您找不到某些东西的原因。你的模板引擎是什么?玉?胡子?这就是你应该寻找的。 Node.js 是一个应用程序框架。 Express 是一个使用 HTTP 构建 Web 应用程序的框架。您的模板引擎实际上是处理视图的。
-
我目前正在使用 Hogan 模板引擎。我应该使用不同的模板引擎吗?
-
不,您可以使用任何您喜欢的模板引擎!我只是说当谷歌搜索时,你应该寻找 Hogan 而不是 Node.js。
-
谢谢。如果我找到答案,我会在这里回复。
-
我遇到了这个帖子:stackoverflow.com/questions/12783615/… 并尝试了那里的一些建议,但没有运气。我收到类型错误。 TypeError: path must be a string
标签: html node.js layout express hogan.js