【问题标题】:How to identify different handlebar templates?如何识别不同的车把模板?
【发布时间】:2021-07-10 09:54:33
【问题描述】:

我的项目中有几个 html 文件,它们是电子邮件的模板,例如电子邮件验证、密码重置等...

我需要预编译这些文件并在正确的地方使用它们,例如必须在注册功能中发送电子邮件验证模板。问题是我如何识别要使用它们的模板,因为我不知道它们是哪种类型的?如何为模板创建某种标识符?

【问题讨论】:

  • 请避免添加抓屏并添加相关源代码。这在回答时很有帮助。

标签: html node.js typescript handlebars.js


【解决方案1】:

您可以将编译后的模板保存在某种数据结构中,然后以某种方式返回并使用它。

这里有一个基于您的处理函数并使用Map 作为数据存储的示例:

export function preCompileTemplates(globString: string) {

  const paths = getPathsFromGlob(globString)

  const preCompiledTemplatesPs = paths.map(path => {
      const templateName = path.substring(path.lastIndexOf('/') + 1, path.length - 5);
      return fs.promises.readFile(path).then(template => {
          return {name: templateName, content: Handlebars.precompile(template)}
      })
  })

  const templates = new Map()
  return Promises.all(preCompiledTemplatesPs, (preCompiledTemplates) => {
    preCompiledTemplates.forEach(template => {
      templates.set(template.name, template.content)
    })
    return templates
  })
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 2015-05-18
    相关资源
    最近更新 更多