Node.js 使用“帕格”在渲染时收集文档片段`

Node.js 使用“帕格”在渲染时收集文档片段`,node.js,email,pug,Node.js,Email,Pug,我使用pug从模板生成HTML电子邮件: doctype html html head title Hello #{name} body ... 标题是电子邮件的主题 目前,我通过解析pug呈现的HTML文档来提取标题文本内容。但这似乎不是一种非常有效的方法 pug中是否有一些功能或钩子可用于在呈现文档时收集部分文档?我考虑过,但据我所知,这些是不合适的,因为它们是在编译时触发的。不是在呈现文档时。我使用mixin找到了一个解决方案: mixin collect(nam

我使用
pug
从模板生成HTML电子邮件:

doctype html
html
  head
    title Hello #{name}
  body
    ...
标题是电子邮件的主题

目前,我通过解析
pug
呈现的HTML文档来提取
标题
文本内容。但这似乎不是一种非常有效的方法


pug
中是否有一些功能或钩子可用于在呈现文档时收集部分文档?我考虑过,但据我所知,这些是不合适的,因为它们是在编译时触发的。不是在呈现文档时。我使用mixin找到了一个解决方案:

mixin collect(name)
  -
    // This is just an ugly hack to
    // capture the inner block rendered
    // text
    const savedHtml = pug_html;
    pug_html = "";
    if (block) block();
    const innerHtml = pug_html;
    self[name]=innerHtml;

    pug_html = savedHtml+innerHtml;

html
  head
    title
      +collect('title')
        | Hello #{self.name}
显示:

<html><head><title>Hello Timothy</title></head></html>
{"name":"Timothy","title":"Hello Timothy"}
你好,蒂莫西 {“姓名”:“蒂莫西”,“头衔”:“你好,蒂莫西”}
collect()
mixin的代码并不特别漂亮,因为。因此,我必须处理内部未记录的
pug_html
变量

还是有更干净的方法来实现这一点

<html><head><title>Hello Timothy</title></head></html>
{"name":"Timothy","title":"Hello Timothy"}