0

我正在使用翡翠创建电子邮件模板,因此我为每种类型的电子邮件和每种语言都有一个翡翠文件。我希望能够从翡翠模板中设置电子邮件主题,可以通过读取模板中定义的变量或通过查找标题标签的内容来设置,但我无法执行其中任何一项。有没有办法将 html 标签绑定到函数,以便我可以获取它的内容?或者只是能够访问翡翠模板中定义的变量?

谢谢!

4

1 回答 1

1

这就是我所做的。

首先,我扩展了jade.Compiler 来创建我自己的编译器,重写了visitTag 方法,以便能够在使用title 标记时进行捕获。

subjects = {}

EmailCompiler = (node, options) ->
  jade.Compiler.call(this, node, options)
EmailCompiler::__proto__ = jade.Compiler.prototype;

EmailCompiler::visitTag = (tag) ->
  if tag.name is 'title'
    subjects[@options.filename] = @getText tag.block.nodes[0]
  jade.Compiler.prototype.visitTag.call(this, tag)

EmailCompiler::getText = (nodes, glue='') ->
  [].map.call(nodes.nodes, (node) -> node.val).join glue

然后jade编译器是这样调用的:

fs.readFile filePath, (err, str) ->
  jade.compile str,
    compiler: EmailCompiler
    filename: filePath

希望它可以帮助某人!

于 2013-07-21T02:52:24.747 回答