art-template 模板引擎

版权声明:未经同意,不得随意转载转载 https://blog.csdn.net/lucky541788/article/details/83927579

art-template 模板引擎官网:Art-Template

模板引擎的实现方式有很多,最简单的是“置换型”模板引擎,这类模板引擎只是将指定模板内容(字符串)中的特定标记(子字符串)替换一下便生成了最终需要的业务数据(比如网页)。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>在浏览器中使用art-template</title>
</head>
<body>
<!--
1. 安装:cnpm i art-template
该命令在哪执行就会把包下载到哪里,默认会下载到 node_modules 目录中
node_modules 不要改,也不支持改

注意:在浏览器中需要引入 lib/template-web.js 文件

强调:模板引擎不关心你的字符串内容,只关心自己能认识的模板标记语法,例如 {{}}
{{}} 语法被称之为 mustache 语法,八字胡语法
-->
<script src="node_modules/art-template/lib/template-web.js"></script>
<script type="text/template" id="tpl">
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
    <h1>大家好,我叫:{{ name }}</h1>
    <p>我今年 {{ age }} 岁了</p>
    <p>我来自 {{ province }}</p>
    <p>我喜欢 {{ each hobbies }} {{ $value }} {{ /each }}</p>
    </body>
    </html>
</script>
<script>
    var ret = template('tpl', {
        name: 'John',
        age: 20,
        province: '北京市',
        hobbies: [
            '写代码',
            '唱歌',
            '打游戏',
            '游泳'
        ]
    });

    console.log(ret);
</script>
</body>
</html>

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/lucky541788/article/details/83927579