在node環境下parse Smarty模板

由於某種緣由,咱們的項目必須採用smarty模板,一直沒找到好用的npm包
不得已扒開了fis3-smarty, gulp-smarty的代碼,找到了一個npm包
smarty4Js
而後解決了node環境渲染smarty的問題 html

代碼以下:node

// 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>

結果以下:
image.pnggit

相關文章
相關標籤/搜索