我正在处理node.js快递应用程序中的密码重置功能,因为我打算编译一个pug模板,它的输出会以邮件的形式发送给用户。
Pug已经实现,并在快速应用程序中作为视图引擎工作,但是我在一些端点中直接使用它的API。
.js
var locals = {name: user.name, resetLink: link};
var template = pug.compileFile(path.join(__dirname, "../views/emails/reset.pug")),
html = pug.render(template, locals).pug模板
doctype html
html
head
title= "Password reset"
body
block content
p Hello #{name}!
p Here is your
a(href="#{resetLink}" target="_blank") reset link我尝试过pug.compile & pug.compileFile,所有的尝试都失败了,这种错误并没有给我带来任何结果。来自我和网络的几个模板已经被用来用相同的结果来测试它。
C:\DEV\Workspace\Node\express-auth\node_modules\pug-lexer\index.js:60
throw err;
^
Error: Pug:3:1
1| function template(locals) {var pug_html = "", pug_mixins = {}, pug_interp;var pug_debug_filename, pug_
debug_line;try {;var locals_for_with = (locals || {});(function (name, token) {;pug_debug_line = 1;pug_debug_
filename = "C:\\DEV\\Workspace\\Node\\express-auth\\views\\emails\\reset.pug";
2| pug_html = pug_html + "\u003Chtml\u003E";
> 3| ;pug_debug_line = 2;pug_debug_filename = "C:\\DEV\\Workspace\\Node\\express-auth\\views\\emails\\reset
.pug";
-------^
4| pug_html = pug_html + "\u003Chead\u003E";
5| ;pug_debug_line = 3;pug_debug_filename = "C:\\DEV\\Workspace\\Node\\express-auth\\views\\emails\\reset
.pug";
6| pug_html = pug_html + "\u003Ctitle\u003E";
unexpected text ";pug_"
at makeError (C:\DEV\Workspace\Node\express-auth\node_modules\pug-error\index.js:32:13)
at Lexer.error (C:\DEV\Workspace\Node\express-auth\node_modules\pug-lexer\index.js:59:15)
at Lexer.fail (C:\DEV\Workspace\Node\express-auth\node_modules\pug-lexer\index.js:1441:10)
at Lexer.advance (C:\DEV\Workspace\Node\express-auth\node_modules\pug-lexer\index.js:1501:15)
at Lexer.callLexerFunction (C:\DEV\Workspace\Node\express-auth\node_modules\pug-lexer\index.js:1456:23)
at Lexer.getTokens (C:\DEV\Workspace\Node\express-auth\node_modules\pug-lexer\index.js:1512:12)
at lex (C:\DEV\Workspace\Node\express-auth\node_modules\pug-lexer\index.js:12:42)
at Object.lex (C:\DEV\Workspace\Node\express-auth\node_modules\pug\lib\index.js:99:27)
at Function.loadString [as string] (C:\DEV\Workspace\Node\express-auth\node_modules\pug-load\index.js:44:
24)
at compileBody (C:\DEV\Workspace\Node\express-auth\node_modules\pug\lib\index.js:86:18)
at Object.exports.compile (C:\DEV\Workspace\Node\express-auth\node_modules\pug\lib\index.js:242:16)
at handleTemplateCache (C:\DEV\Workspace\Node\express-auth\node_modules\pug\lib\index.js:215:25)
at Object.exports.render (C:\DEV\Workspace\Node\express-auth\node_modules\pug\lib\index.js:396:10)
at RandomBytes.ondone (C:\DEV\Workspace\Node\express-auth\routes\index.js:124:18)关于这个问题的文档很少,其中一条轨道是在模板文件中的本地或块周围跟踪空间,我尝试删除它们,但没有成功。
当与快速视图引擎一起使用时,使用的模板被编译并呈现得很好。
提前谢谢你,麦克斯
发布于 2018-08-20 08:40:22
compileFile返回一个呈现函数,而不是一个裸模板。
来自他们的文档:
var pug = require('pug');
// Compile a function
var fn = pug.compileFile('path to pug file', options);
// Render the function
var html = fn(locals);
// => '<string>of pug</string>'所以你的代码应该是:
var locals = {name: user.name, resetLink: link};
var template = pug.compileFile(path.join(__dirname, "../views/emails/reset.pug")),
html = template(locals);https://stackoverflow.com/questions/51487148
复制相似问题