首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pug.compile :意想不到的文本";pug_“

Pug.compile :意想不到的文本";pug_“
EN

Stack Overflow用户
提问于 2018-07-23 20:58:14
回答 1查看 911关注 0票数 0

我正在处理node.js快递应用程序中的密码重置功能,因为我打算编译一个pug模板,它的输出会以邮件的形式发送给用户。

Pug已经实现,并在快速应用程序中作为视图引擎工作,但是我在一些端点中直接使用它的API。

  • pug版本: 2.0.3 (也尝试了2.0.0)
  • 节点版本: 8.11.3

.js

代码语言:javascript
复制
var locals = {name: user.name, resetLink: link};

var template = pug.compileFile(path.join(__dirname, "../views/emails/reset.pug")),
    html = pug.render(template, locals)

.pug模板

代码语言:javascript
复制
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,所有的尝试都失败了,这种错误并没有给我带来任何结果。来自我和网络的几个模板已经被用来用相同的结果来测试它。

代码语言:javascript
复制
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)

关于这个问题的文档很少,其中一条轨道是在模板文件中的本地或块周围跟踪空间,我尝试删除它们,但没有成功。

当与快速视图引擎一起使用时,使用的模板被编译并呈现得很好。

提前谢谢你,麦克斯

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-20 08:40:22

compileFile返回一个呈现函数,而不是一个裸模板。

来自他们的文档

代码语言:javascript
复制
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>'

所以你的代码应该是:

代码语言:javascript
复制
var locals = {name: user.name, resetLink: link};

var template = pug.compileFile(path.join(__dirname, "../views/emails/reset.pug")),
    html = template(locals);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51487148

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档