如何编译翡翠模板文件获取字符串? [英] How to compile jade template file to get string?
本文介绍了如何编译翡翠模板文件获取字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的JADE模板文件中有一个视图逻辑。如何将model传入jade并获取html,以便通过邮件进一步发送?
推荐答案
您可以尝试以下操作:
var jade = require('jade'),
fs = require('fs');
fs.readFile('template.jade', 'utf8', function (err, data) {
if (err) throw err;
console.log(data);
var fn = jade.compile(data);
var html = fn({name:'Oleg'});
console.log(html);
});
其中template.jade
是模板的路径。它看起来是这样的:
!!!
html
head
title= 'Hello world'
body
p Hello #{name}!
因此您将模型作为fn()函数的输入传递,其输出将是html。
<!DOCTYPE html><html><head><title>Hello world</title></head><body><p>Hello Oleg!</p></body></html>
这篇关于如何编译翡翠模板文件获取字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文