art-template 模板引擎的简单使用

模板引擎

模板引擎是第三方模块
让开发者以更加友好的方式拼接字符串,使项目代码更加清晰、更加易于维护。

art-template 官网
视频教程

在浏览器中使用

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>art-template</title>
    <!-- 引用 js 文件 -->
    <script src="./js/template-web.js"></script>

</head>

<body>

    <div id="box"></div>

    <!-- 定义模板 -->
    <script id="tpl" type="text/html">
        {{tit}}
        <ul>
            {{each list}}
                <li>
                    {{$index}} {{$value}}
                </li>
            {{/each}}
        </ul>
    </script>

    <!-- 结合数据编译 -->
    <script>
        const html = template('tpl', {
            list: ['kobe', 'james', 'curry', 'jordan'],
            tit: '数据组'
        })
        box.innerHTML = html
    </script>
</body>

</html>

node 中使用

  1. 通过命令 npm install art-template --save 进行安装
  2. 使用 const template = require( ‘art-template’ ) 引入模板引擎
  3. 使用 const html = template( ‘模板路径’, 数据 ) 进行编译

npm install art-template --save

const template = require('art-template')
const path = require('path')

const views = path.join(__dirname, 'test.art')
const tpl = template(views, {
  name: 'Tom',
  list: ['kobe', 'james', 'curry', 'jordan']
})

.art 文件 其实就是 html 格式的文件

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值