在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)文章
package.json與package-lock.json創(chuàng)建及使用詳解
這篇文章主要為大家介紹了package.json與package-lock.json創(chuàng)建及使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07
關(guān)于npm主版本升級及其相關(guān)知識點總結(jié)
npm是Node.js默認的包管理器,以javascript?編寫的軟件包管理系統(tǒng)用于分享和使用代碼,下面這篇文章主要給大家介紹了關(guān)于npm主版本升級及其相關(guān)知識點總結(jié)的相關(guān)資料,需要的朋友可以參考下2022-12-12
如何設(shè)置process.env.NODE_ENV生產(chǎn)環(huán)境模式
process.env.NODE_ENV默認只有兩種狀態(tài)即development和production,本文主要介紹了process.env.NODE_ENV設(shè)置生產(chǎn)環(huán)境模式,感興趣的可以了解一下2021-09-09
在Express中提供靜態(tài)文件的實現(xiàn)方法
這篇文章主要介紹了在Express中提供靜態(tài)文件的實現(xiàn)方法,將包含靜態(tài)資源的目錄的名稱傳遞給 express.static 中間件函數(shù),以便開始直接提供這些文件,感興趣的可以了解一下2019-10-10
node.js實現(xiàn)微信開發(fā)之獲取用戶授權(quán)
這篇文章主要介紹了node.js實現(xiàn)微信開發(fā)之獲取用戶授權(quán),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03

