2013-07-18 156 views
0

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

谢谢!

+0

如果只有一个变量,你可以把它放在模板的名字里面。即template_en.jade – fabrizioM

回答

1

这就是我所做的。

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

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 

然后玉编译器叫这样的:

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

希望它可以帮助别人!

相关问题