在node環(huán)境下parse Smarty模板的使用示例代碼
因為某種原因,我們的項目必須采用smarty模板,一直沒找到好用的npm包
不得已扒開了fis3-smarty, gulp-smarty的代碼,找到了一個npm包
smarty4Js
然后解決了node環(huán)境渲染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>
結(jié)果如下:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
node.js平臺下的mysql數(shù)據(jù)庫配置及連接
本文主要介紹了node.js平臺下的mysql數(shù)據(jù)庫配置及連接的相關(guān)知識。具有很好的參考價值,下面跟著小編一起來看下吧2017-03-03nodeJS與MySQL實現(xiàn)分頁數(shù)據(jù)以及倒序數(shù)據(jù)
這篇文章主要介紹了nodeJS與MySQL實現(xiàn)分頁數(shù)據(jù)以及倒序數(shù)據(jù),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06解決node.js含有%百分號時發(fā)送get請求時瀏覽器地址自動編碼的問題
這篇文章主要介紹了解決node.js含有%百分號時發(fā)送get請求時瀏覽器地址自動編碼的問題,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-11-11Node.js中使用mongoose操作mongodb數(shù)據(jù)庫的方法
如何利用mongoose將數(shù)據(jù)寫入mongodb數(shù)據(jù)庫呢?操作方法很簡單,下面小編給大家分享Node.js中使用mongoose操作mongodb數(shù)據(jù)庫的方法,感興趣的朋友一起看看吧2017-09-09nodejs結(jié)合Socket.IO實現(xiàn)的即時通訊功能詳解
這篇文章主要介紹了nodejs結(jié)合Socket.IO實現(xiàn)的即時通訊功能,結(jié)合實例形式詳細分析了nodejs結(jié)合Socket.IO實現(xiàn)即時通訊的相關(guān)操作技巧與注意事項,需要的朋友可以參考下2018-01-01