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

时间:2021-05-25

因为某种原因,我们的项目必须采用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: <!doctype html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>{%$title%}</title></head><body><div id="footer">{%$body%}</div></body></html>

结果如下:

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

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章