在node环境下parse Smarty模板的使用示例代码

这篇文章主要介绍了在node环境下parse Smarty模板的使用示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

因为某种原因,我们的项目必须采用smarty模板,一直没找到好用的npm包
不得已扒开了fis3-smarty, gulp-smarty的代码,找到了一个npm包
smarty4Js
然后解决了node环境渲染smarty的问题

代码如下:

 // index.js: const express = require('express'); const smarty4Js = require('smarty4Js'); const path = require('path'); const app = express(); const smartyHelper = new smarty4Js(); const basePath = path.resolve('./'); smartyHelper.setBasedir(basePath); const compiler = smartyHelper.compile('./index.tpl'); const html = 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('app is run at 3000 port'); });
 // index.tpl:   {%$title%} 

结果如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持html中文网。

以上就是在node环境下parse Smarty模板的使用示例代码的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » JavaScript 教程