系统、网站、API站点,简单高效开发

平台介绍 >> 模板基础语法

模板基础语法

 
    

调用变量


    当前域变量:{{.t}}

    全局域变量,需要再子域中调用:{{$.tt}}

    {{$aa:="aaaaa"}}

    变量值: {{$aa}}

    {{range (slice .listInt 3 4) }}
    局部变量:{{.}}__全局变量{{$.t}}
    {{end}}
 
    

调用内置函数的两种方法,用空格隔开参数


    

1:{{len "aa"}} 推荐


    

2:{{"aa"|len}}


    

3:嵌套方法:{{len (substr "123456789" 2 4)}}


 
    

输出内容:


    {{html .content}}
 

    数组值:{{.listStr}}

    长度值:{{len .listStr}}

    截取值:{{slice .listStr 4 5}}

    索引位置对应的值:{{index .listStr 2}}

    get参数值:{{urlquery "上来的"}}

    字符串长度:{{len "abc"}}

    字符串长度(一个中文3个字节):{{len "上来的"}}

    字符串长度(一个中文为一个长度):{{length "上来的"}}

    字符串长度:{{len "1上"}}

    字符串长度:{{length "1上"}}

    substr:{{substr "上来的sdf" 2 3}} substr 字符串 长度



 

    

遍历


    

1: 遍历元素

        {{ range .listStr }}
        元素: {{ . }};
        {{ end }}
    



    

3: 遍历元素,函数截取数组

        {{range (slice .listStr 3 6) }}
        元素:{{.}};
        {{end}}

    


    

2: 遍历元素,带索引 

        {{ range $index,$item:= .listStr }}
        索引{{$index}},值:{{$item}};
        {{ end }}
    


    

3: 遍历元素,带索引,带索引值,函数截取数组

        {{range $index,$item:= (slice .listStr 3 6) }}
        索引{{$index}},值:{{$item}};
        {{end}}

    


    

4: 遍历空数组,显示没有数据的内容 

        {{ range (slice .listStr 3 3) }}
        元素:{{.}};
        {{ else }}
        遍历的数组没有数据
        {{ end }}
    


    

4: 遍历数组,调用全局变量 

        {{range .listStr }}
        局部变量:{{.}}__全局变量{{$.t}}
        {{end}}
    



    

if判断


    


        {{if eq .t 100}}
        等于100
        {{else if eq .t 200}}
        等于200
        {{else}}
        其他
        {{end}}
    


    


        {{if lt .t 1}}
        小于1
        {{end}}
    


    


        {{if le .t 0}}
        小于或等于0
        {{end}}
    


    


        {{if gt .t -1}}
        大于-1
        {{end}}
    


    


        {{if ge .t 0}}
        大于等于0
        {{end}}
    



    

复杂判断语句


    


        {{if or (eq .t 100) (eq .t 200)}}
        等于100或等于200
        {{end}}
    


    


        {{if and (eq .t 100) (eq .t 200)}}
        等于100且等于200
        {{end}}
    


    

2: 遍历元素,带索引 

        {{ range $index,$item:= .listStr }}
        {{if eq $index 0}}
        我是索引0;
        {{else if or (eq $index 1) (eq $index 2)}}
        我是索引1或2;
        {{end}}
        {{end}}
    


备案号:闽ICP备20003806号     厦门市湖里区陈朝能网络技术工作室