chst365's blog chst365's blog
首页
  • Git
  • 网络
  • 操作系统
  • 浏览器
  • webpack
  • JavaScript
  • TypeScript
  • 性能
  • 工程化
  • React
  • 编程题
  • React技术揭秘
  • 算法
  • Node
  • 编码解码
  • NodeJS系列
  • Linux系列
  • JavaScript系列
  • HTTP系列
  • GIT系列
  • ES6系列
  • 设计模式系列
  • CSS系列
  • 小程序系列
  • 数据结构与算法系列
  • React系列
  • Vue3系列
  • Vue系列
  • TypeScript系列
  • Webpack系列
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

chst365

DIV工程师
首页
  • Git
  • 网络
  • 操作系统
  • 浏览器
  • webpack
  • JavaScript
  • TypeScript
  • 性能
  • 工程化
  • React
  • 编程题
  • React技术揭秘
  • 算法
  • Node
  • 编码解码
  • NodeJS系列
  • Linux系列
  • JavaScript系列
  • HTTP系列
  • GIT系列
  • ES6系列
  • 设计模式系列
  • CSS系列
  • 小程序系列
  • 数据结构与算法系列
  • React系列
  • Vue3系列
  • Vue系列
  • TypeScript系列
  • Webpack系列
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 浏览器

  • webpack

  • TypeScript

  • 性能

  • 工程化

  • React

  • JavaScript

  • 编程题

    • 实现一个trim方法
    • 实现一个deepClone方法
    • 实现add(1)(2)(3)
    • 大数相加
    • 拍平数组flat
    • 实现防抖、节流函数
    • 反转字符串
    • 数组去重
    • 实现千位分隔符
    • 回文数
    • 实现一个模板引擎
    • 判断一个数是否是素数
    • 获取n以内所有的素数
  • React技术揭秘

  • 算法

  • 前端
  • 编程题
chst365
2022-09-07

实现一个模板引擎

<!DOCTYPE html>
<html>

<head>
    <title>template</title>
</head>

<body>
    <div id="container"></div>
    <script type="text/html" id="user_tmpl">
        <%for ( var i = 0; i < users.length; i++ ) { %>
        <li>
            <a href="<%=users[i].url%>">
                <%=users[i].name%>
            </a>
        </li>
        <% } %>
    </script>
    <script>
        // 模板引擎第二版
        (function () {
            this.tmpl = function (str, data) {

                var str = document.getElementById(str).innerHTML;

                var fn = new Function("obj",

                    "var p = []; p.push('" +

                    str
                    .replace(/[\r\t\n]/g, "")
                    .replace(/<%=(.*?)%>/g, "');p.push($1);p.push('")
                    .replace(/<%/g, "');")
                    .replace(/%>/g, "p.push('") +
                    "');return p.join('');");

                return fn(data);
            };
        })();

        var results = document.getElementById("container");

        var users = [{
                "name": "Byron",
                "url": "http://localhost"
            },
            {
                "name": "Casper",
                "url": "http://localhost"
            },
            {
                "name": "Frank",
                "url": "http://localhost"
            }
        ]

        results.innerHTML = tmpl("user_tmpl", users);
    </script>
</body>

</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#前端#编程题
上次更新: 2022/09/07, 15:54:26
回文数
判断一个数是否是素数

← 回文数 判断一个数是否是素数→

最近更新
01
面试官
03-27
02
this&指针&作用域&闭包
03-27
03
前端
03-27
更多文章>
Theme by Vdoing | Copyright © 2019-2025 chst365 | 豫ICP备17031889号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式