【发布时间】:2021-10-04 02:45:49
【问题描述】:
可能永远不会使用 node.js 或 Nunjucks 进行任何真正的开发,但现在出于某种原因需要:
- 使用
Nunjucks将一些简单的模板预编译为javascript - 运行
node.js下的预编译模板
我已经完成了:
- 安装了
node.js和npm(例如有node和npm命令) mkdir njtest && cd njtest- 用
npm install nunjucks安装了nunjucks(得到了node_modules/nunjucks目录) mkdir templates在模板中,我创建了两个文件
index.html和layout.html,内容如下:jinja2/nunjuckslayout.html
<!doctype html>
<head>
<title>simple example</title>
</head>
<body>
<h1>Simple example</h1>
{% block body %}{% endblock %}
</body>
index.html
{% extends "layout.html" %}
{% block body %}
hello world
{% endblock %}
- 我使用 预编译了模板
./node_modules/nunjucks/bin/precompile templates >templates.js
在templates.js 我有预编译的代码。
接下来我应该to do 来获得一个正在运行的网络服务器什么将使用预编译的template.js?
请不要搜索任何关于这个问题的高级内容。对于了解 node 和 javascript 的人来说,这可能是一个愚蠢的简单问题。
我知道,将需要,创建一个文件让说 app.js 并需要使用 node 运行它 - 但应该包含什么?
require 'nunjucks';
可能是这样的:var res = nunjucks.render('templates.js'); 还有什么? (最简单的(一次性)解决方案)。注意:希望在服务器端使用 Nunjucks,而不是在浏览器中。
【问题讨论】: