在node环境下parse Smarty模板的使用示例代码
因为某种原因,我们的项目必须采用smarty模板,一直没找到好用的npm包
不得已扒开了fis3-smarty,gulp-smarty的代码,找到了一个npm包
smarty4Js
然后解决了node环境渲染smarty的问题
代码如下:
//index.js: constexpress=require('express'); constsmarty4Js=require('smarty4Js'); constpath=require('path'); constapp=express(); constsmartyHelper=newsmarty4Js(); constbasePath=path.resolve('./'); smartyHelper.setBasedir(basePath); constcompiler=smartyHelper.compile('./index.tpl'); consthtml=compiler.render({ title:'node-parse-smarty', body:'Smarty' }); console.log(html); app.get('/',(req,res)=>{ res.header('text/html'); res.end(html); }); app.listen(3000,()=>{ console.log('appisrunat3000port'); });
//index.tpl:{%$title%} {%$body%}