Pug.compile:意外文本“;pug_”

Pug.compile : unexpected text ";pug_"

我正在研究 node.js Express 应用程序中的密码重置功能,为此我打算编译一个哈巴狗模板,它的输出将通过重置令牌作为邮件发送给用户。

Pug 已经在 Express 应用程序中实现并作为视图引擎工作,但我在某些端点中直接使用它 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)

.哈巴狗模板

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)

关于这个问题的文档很少,其中一条轨道是模板文件中局部变量或块周围的尾随空格,我尝试删除它们但没有成功。

使用的模板在与 express 视图引擎一起使用时编译和呈现良好。

提前致谢, 最大值

compileFile returns 渲染函数,而不是裸模板。

来自他们的documentation

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);