简体   繁体   English

如何引用节点模块中包含的 nunjucks 模板?

[英]How to reference a nunjucks template included in a node module?

If I want to package a nunjucks template file with a node module, how do I reference the packaged template file so it is universally available when the package is installed globally?如果我想 package 一个带有节点模块的 nunjucks 模板文件,我如何引用打包的模板文件,以便在全局安装 package 时普遍可用?

I have the following node file, index.js :我有以下节点文件index.js

#!/usr/bin/env node

var nunjucks = require('nunjucks');

var env = nunjucks.configure('./');
var template = env.getTemplate('template.html');
var output = template.render({
  h1_copy: "Foo and Bar"
});

console.log(output);

Here's template.html:这是模板.html:

<html>
  <body>
    <h1>{{ h1_copy }}</h1>
  </body>
</html>

I set it up to have a binary command in the package.json:我将它设置为在 package.json 中有一个二进制命令:

"bin": {
  "make_output": "./index.js"
}

Now, if I install it globally, I can run make_output to make the output:现在,如果我全局安装它,我可以运行make_output来制作 output:

node-nunjucks$ npm install -g .
/usr/local/bin/make_output -> /usr/local/lib/node_modules/node-nunjucks/index.js
+ node-nunjucks@1.0.0
added 1 package in 0.099s

node-nunjucks$ make_output
<html>
  <body>
    <h1>Foo and Bar</h1>
  </body>
</html>

But that only works if template.html is present in the directory where I run the command.但这仅在我运行命令的目录中存在template.html时才有效。 If I try to run the global command from anywhere else, it can't find the template:如果我尝试从其他任何地方运行全局命令,它将找不到模板:

node-nunjucks$ cd ..
tmp$ make_output
/private/tmp/node-nunjucks/node_modules/nunjucks/src/environment.js:296
          throw err;
          ^

Error: template not found: template.html

How do I reference the packaged template file in index.js , so it uses the template in the package (the one at /usr/local/lib/node_modules/node-nunjucks/template.html ) rather than looking for the template in my working directory?如何在index.js中引用打包的模板文件,因此它使用 package 中的模板( /usr/local/lib/node_modules/node-nunjucks/template.html中的模板)而不是在我的工作目录?

The configure call is the one you're going to want to tweak.配置调用是您要调整的调用。 It tells getTemplate where to look for templates.它告诉 getTemplate 在哪里寻找模板。 It can take an array of values referring to multiple directories where it can find templates.它可以采用一组值,这些值引用了可以找到模板的多个目录。

Example:例子:

#!/usr/bin/env node
var nunjucks = require('nunjucks');

var env = nunjucks.configure(['./', './node_modules/node_nunjucks/']);
var template = env.getTemplate('inner.html');
var output = template.render({
  h1_copy: "Foo and Bar"
});
var template2 = env.getTemplate('outer.html');
var output2 = template2.render({
  h1_copy: "Foo2 and Bar2"
});


console.log(output);
console.log(output2);

In this example inner.html resides in the root next to index.js, and outer.html resides in node_modules/node_nunjucks.在此示例中,inner.html 位于 index.js 旁边的根目录中,outer.html 位于 node_modules/node_nunjucks 中。 It's just a relative path to the node_modules folder, but you can get more clever with aliases and whatnot depending on what tools you're using to build with.它只是 node_modules 文件夹的相对路径,但是您可以使用别名和诸如此类的东西变得更聪明,具体取决于您使用什么工具来构建。

Hope that helps.希望有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM