• golang template使用


    <html>
    <head>
        <title>邮件标题</title>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
        <meta name="description" content="alert results." />
        <style type="text/css">
            body
            {
                margin: 10px 20px;
                font-size: 14px;
                font-family: "Arial","Microsoft YaHei","黑体","宋体",sans-serif;
            }
            /*table*/
            /*{*/
            /*border-collapse: collapse;*/
            /*text-align: center;*/
            /*font-size: 14px;*/
            /*}*/
            /*table td, table th*/
            /*{*/
            /*border: 2px solid #cc6f4a;*/
            /*color: #666;*/
            /*height: 20px;*/
            /*text-align: center;*/
            /*padding: 3px 3px;*/
            /*}*/
            /*table thead th*/
            /*{*/
            /**/
            /* 100px;*/
            /*}*/
            /*table tr:nth-child(odd)*/
            /*{*/
            /*background: #fff;*/
            /*}*/
            /*table tr:nth-child(even)*/
            /*{*/
            /*background: #c9dafa;*/
            /*}*/
            .successBtn {
                 60px;
                padding:3px;
                background-color: #58ab48;
                border-color: #58ab48;
                color: #fff;
                -moz-border-radius: 10px;
                -webkit-border-radius: 10px;
                border-radius: 10px; /* future proofing */
                -khtml-border-radius: 10px; /* for old Konqueror browsers */
                text-align: center;
                vertical-align: middle;
                border: 1px solid transparent;
                font-weight: 500;
                /*font-size:125%*/
            }
            .failBtn{
                 60px;
                padding:3px;
                background-color: #ab2e2d;
                border-color: #ab2e2d;
                color: #fff;
                -moz-border-radius: 10px;
                -webkit-border-radius: 10px;
                border-radius: 10px; /* future proofing */
                -khtml-border-radius: 10px; /* for old Konqueror browsers */
                text-align: center;
                vertical-align: middle;
                border: 1px solid transparent;
                font-weight: 500;
                /*font-size:125%*/
            }
        </style>
        <style>
            /* Border styles */
            .tabNoBorder thead, .tabNoBorder tr {
                border-top- 1px;
                border-top-style: solid;
                border-top-color: rgb(211, 202, 221);
            }
            .tabNoBorder {
                border-bottom- 1px;
                border-bottom-style: solid;
                border-bottom-color: rgb(211, 202, 221);
            }
    
            /* Padding and font style */
            .tabNoBorder td, .tabNoBorder th {
                padding: 5px 10px;
                font-size: 14px;
                font-family: Verdana;
                color: rgb(95, 74, 121);
            }
    
            /* Alternating background colors */
            .tabNoBorder tr:nth-child(even) {
                background: rgb(223, 216, 232)
            }
            .tabNoBorder tr:nth-child(odd) {
                /*background: #FFF*/
                background: rgb(223, 216, 232)
            }
        </style>
    
    </head>
    <body>
    <h2>Summary</h2>
    <span>预警任务《{{.TaskName}}》警告。 。详情如下:</span>
    <h2>Detail</h2>
    <table class="tabNoBorder" style="border-top-1px;
                border-top-color: rgb(211, 202, 221);">
        <tr class="columnHeadings" style="border-top-1px;
                border-top-style: solid;
                border-top-color: rgb(211, 202, 221);background: rgb(223, 216, 232)">
            <th style="padding: 5px 10px;
                font-size: 14px;
                font-family: Verdana;
                color: rgb(95, 74, 121);">详细数据</th>
            <th style="padding: 5px 10px;
            font-size: 14px;
            font-family: Verdana;
            color: rgb(95, 74, 121);">对比实际值</th>
            <th style="padding: 5px 10px;
                font-size: 14px;
                font-family: Verdana;
                color: rgb(95, 74, 121);">阈值</th>
            <th style="padding: 5px 10px;
                font-size: 14px;
                font-family: Verdana;
                color: rgb(95, 74, 121);">对比结果</th>
        </tr>
        {{ range $Item := .Data }}
            {{ range $i,$Item1 := $Item }}
                {{if eq $i 0}}
                <tr style="border-top-1px;
                border-top-style: solid;
                border-top-color: rgb(211, 202, 221);background: rgb(223, 216, 232)">
                    <td rowspan="{{len $Item}}" style="padding: 5px 10px;
                font-size: 14px;
                font-family: Verdana;
                color: rgb(95, 74, 121);">
            
                    </td>
                    <td style="padding: 5px 10px;
                font-size: 14px;
                font-family: Verdana;
                color: rgb(95, 74, 121);">{{$Item1.realValue}}</td>
                    <td style="padding: 5px 10px;
                font-size: 14px;
                font-family: Verdana;
                color: rgb(95, 74, 121);">{{$Item1.threshold}}</td>
                    <td rowspan="{{len $Item}}" style="padding: 5px 10px;
                font-size: 14px;
                font-family: Verdana;
                color: rgb(95, 74, 121);"><div class="failBtn">Alert</div></td>
                </tr>
                    {{else}}
                    <tr style="border-top-1px;
                border-top-style: solid;
                border-top-color: rgb(211, 202, 221);background: rgb(223, 216, 232)">
                        <td style="padding: 5px 10px;
                font-size: 14px;
                font-family: Verdana;
                color: rgb(95, 74, 121);">{{$Item1.realValue}}</td>
                        <td style="padding: 5px 10px;
                font-size: 14px;
                font-family: Verdana;
                color: rgb(95, 74, 121);">{{$Item1.threshold}}</td>
                    </tr>
                    {{end}}
            {{end}}
        {{end}}
    
    </table>
    
    
    
    <br/><br/>
    
    
    
    </body>
    </html>                            
    

      

    var builtins = FuncMap{
    "and": and,
    "call": call,
    "html": HTMLEscaper,
    "index": index,
    "js": JSEscaper,
    "len": length,
    "not": not,
    "or": or,
    "print": fmt.Sprint,
    "printf": fmt.Sprintf,
    "println": fmt.Sprintln,
    "urlquery": URLQueryEscaper,


    // Comparisons
    "eq": eq, // ==
    "ge": ge, // >=
    "gt": gt, // >
    "le": le, // <=
    "lt": lt, // <
    "ne": ne, // !=
    }
    template内置函数,使用方法为ps: {{printf "%d" var1}}

    func parseNotifyTemplate(templateFileName string, msg Message) string {
    	//tmpl, err := template.New("notify").Parse(notifyTemplate)
    
    	//tmpl, err := template.ParseFiles("../../templates/reportTemplate.html")
    
    	var funMaps = template.FuncMap{
    		"addonehour":          AddOneHour,
    		"getColumnTitleByKey": GetColumnTitleByKey,
    		"noKexuejishu":        NoKexuejishu,
    	}
    	fmt.Println(funMaps)
    	//根据模板目录配置+配置文件名
    	tmpl, err := template.New(templateFileName).Funcs(funMaps).ParseFiles(app.Cfg.Templatesdir + templateFileName)
    
    	//根据模板目录配置+配置文件名
    	//tmpl, err := template.ParseFiles(app.Cfg.Templatesdir + templateFileName)
    	if err != nil {
    		fmt.Println("定义通知模板失败: %s", err)
    		return fmt.Sprintf("定义通知模板失败: %s", err)
    	}
    	//tmpl = tmpl.Funcs(template.FuncMap{"getValueByKey": GetValueByKey})
    
    	var buf bytes.Buffer
    	err = tmpl.Execute(&buf, map[string]interface{}{
    		"TaskId":   msg["task_id"],
    		"TaskName": msg["name"],
    		"Status":   msg["status"],
    		"Result":   msg["output"],
    		"Data":     msg["Data"],
    		"UpdateAt": msg["updateAt"],
    	})
    	//template.HTMLEscapeString(buf.String())
    	if err != nil {
    		fmt.Println("解析通知模板失败: %s", err)
    		return fmt.Sprintf("解析通知模板失败: %s", err)
    	}
    	//return buf.String()
    	return strings.Replace(buf.String(), "
    ", "", -1)
    }
    

      

  • 相关阅读:
    PHP中单引号与双引号的区别分析
    utf8_unicode_ci与utf8_general_ci的区别
    [mysql-Ver5.6.23] windows版my.ini配置
    Gateway/Worker模型 数据库使用示例
    php 字符串 以 开头 以结尾 startWith endWith
    MySQL错误ERROR 2002 (HY000): Can't connect to local MySQL server
    vim变ide
    在Vue中使用样式
    Vue指令之`v-model`和`双向数据绑定
    Vue指令之事件修饰符
  • 原文地址:https://www.cnblogs.com/zipon/p/12165500.html
Copyright © 2020-2023  润新知