artTemplate 模板引擎 添加自定义函数方法(项目中jsp页面经常会使用到)-----小案例一枚

项目过程中,经常有时候后端返回的数据需要转化,但是使用模板引擎不知道怎么转,因此有这笔记。标红色是定义和执行关键

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>demo</title>
        <style>
            table{border-collapse:collapse ;}
            td,th{border: red solid 1px; width: 80px; text-align: left;}
        </style>
    </head>
    <body>
        <div class="studentWrapp">
            <script id="studentTp" type="text/html">
                {{each studentList sl}}
                <fieldset> 
                <legend>{{sl.clas | clasFilter}}</legend>
                    <table >
                        <thead>
                            <tr>
                                <th>姓名</th>
                                <th>年龄</th>
                            </tr>
                        </thead>
                        <tbody>
                            {{each sl.arr slarr}}
                            <tr>
                                <td>{{slarr.name}}</td>
                                <td>{{slarr.age}}</td>
                            </tr>
                            {{/each}}
                        </tbody>
                    </table>
                </fieldset>
                {{/each}}
            </script>
        </div>
        <script src="lib/jquery-3.2.1.min.js"></script>
        <script src="lib/template-web.js" ></script>
        <script>
            //模拟后端数据服务
            function getSudent(){
                var result=[
                    {
                        clas:1,
                        arr:[{id:201701,name:'丁少',age:20},{id:201703,name:'张三',age:16}]
                    },
                    {
                        clas:2,
                        arr:[{id:201702,name:'王新',age:21},{id:201704,name:'李四',age:18}]
                    }
                ];
                return result;
            }
            
            //过滤器
            template.defaults.imports.clasFilter=function (clas) {
                var temp='未知';
                if(clas==1){
                    temp='一年级'
                }else if(clas==2){
                    temp='二年级'
                }
                return temp;
            }
            
            
            //渲染至dom
            var studentList=getSudent();
            var tempData={studentList:studentList};
            $('.studentWrapp').html(template('studentTp', tempData));
        </script>
    </body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值