本文整理汇总了Golang中html/template.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。

在下文中一共展示了New函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: main

func main() {
    // apiAuth guards access to api group
    apiAuth := mw.HTTPAuth("API", func(user, pass string) bool {
        return pass == "Secret"
    })
    // dashboardAuth guards access to dashboard group
    dashboardAuth := mw.HTTPAuth("Dashboard", func(user, pass string) bool {
        return pass == "Password"
    })

    // set up root router with Logger, Recovery and LocalStorage middleware
    w := wok.Default()

    // Index page
    idxTpl := template.Must(template.New("index").Parse("<h1>Hello</h1>"))
    w.GET("/", render.Template(idxTpl))(index)

    // api is a group of routes with common authentication and result rendering
    api := w.Group("/api", apiAuth, render.JSON)
    {
        api.GET("/")(apiIndex)
        api.GET("/:id")(apiDetail)
    }

    // dash is an example of another separate route group
    dash := w.Group("/dash", dashboardAuth)
    {
        tpl, _ := template.New("dash").Parse("<h1>Hello {{ .User }}</h1>")
        dash.GET("/", render.Template(tpl))(dashIndex)
    }

    http.ListenAndServe(":8080", w)
}

开发者ID:andviro,项目名称:noodle,代码行数:33,代码来源:main.go

示例2: main

func main() {

    data := map[string]interface{}{
        "Title": "Hello World!",
    }

    t := template.New("HELLO")

    err := t.ExecuteTemplate(os.Stdout, "Template-Html.tmpl", data)
    // not working, empty output.
    // So the template file MUST be specified twice!
    var templates = template.Must(t.ParseFiles("Template-Html.tmpl"))
    err = templates.ExecuteTemplate(os.Stdout, "Template-Html.tmpl", data)
    // working
    fmt.Println("\n\n")

    // This option, need to use the template file twice, once for
    // `ParseFiles`, then for `ExecuteTemplate`. That looks awkward and
    // unnecessary. The following one looks straightforward to me.

    t = template.New("Test template")
    t, err = t.Parse("<title>{{ .Title }}</title>")
    // working
    t, err = template.ParseFiles("Template-Html.tmpl")
    // working now! Note the difference!!
    checkError(err)

    err = t.Execute(os.Stdout, data)

    fmt.Println("\n\nTemplate name is : ", t.Name())

}

开发者ID:suntong,项目名称:lang,代码行数:32,代码来源:Template-Html.go

示例3: transactionStatus

//transactionStatus handles the transactionStatusRequest as defined in the WSDL
//response is the XML version of the client callback
func (c2b *C2B) transactionStatus(data interface{}) http.HandlerFunc {
    return func(rw http.ResponseWriter, r *http.Request) {
        parsed := data.(*TransactionStatusRequest)

        validate(rw,
            validAuthDetails("", parsed.Header.CheckoutHeader),
            validPassedConfirmTrxID(parsed.Body.TransactionStatus.TransID, parsed.Body.TransactionStatus.MerchantTransID),
        )

        trx := c2b.idExists(parsed.Body.TransactionStatus.MerchantTransID, parsed.Body.TransactionStatus.TransID)

        if trx == nil {
            resp := new(ProcessCheckoutResponse)
            resp.ReturnCode = transactionMismatch
            resp.Description = "transaction details are different from original captured request details."
            resp.TransactionID = ""
            resp.ConfirmTrx = true
            tpl, _ := template.New("response").Parse(processCheckOutRespTPL)
            tpl.Execute(rw, resp)
            return
        }
        tpl, _ := template.New("response").Parse(callBackRespTPL)
        tpl.Execute(rw, trx)
    }
}

开发者ID:Raphaeljunior,项目名称:mock-pesa,代码行数:27,代码来源:api.go

示例4: sendMail

// Sends email based on the result of all batches
func (r *GlobalResults) sendMail() {

    funcMap := template.FuncMap{
        "odd": func(i int) bool { return i%2 == 0 },
    }

    tHeader := template.Must(template.New("header").Parse(emailTemplateHtmlHeader))
    tBody := template.Must(template.New("body").Funcs(funcMap).Parse(emailTemplateHtmlBody))

    var docHeader bytes.Buffer
    tHeader.Execute(&docHeader, r.Author)

    for _, br := range r.BatchResultList {
        var docBody bytes.Buffer

        tBody.Execute(&docBody, br)

        TraceActivity.Printf("----------------------------------------------------------------------------- \n")
        TraceActivity.Printf("GeneratedCOntent : \n")
        TraceActivity.Printf("%s\n", docBody.String())
        TraceActivity.Printf("----------------------------------------------------------------------------- \n")

        var buffer bytes.Buffer
        buffer.WriteString(docHeader.String())
        buffer.WriteString(docBody.String())
        br.Smtp.sendEmailHtml(br, buffer.String())
    }
}

开发者ID:nirekin,项目名称:mysqlbatch,代码行数:29,代码来源:result.go

示例5:

func main () {
    html_t, err := gaml.GamlToHtml(gaml_template_1)
    if err != nil {
        fmt.Printf("error: %s", err.Error())
    }
    template,err := template.New("test_template").Parse(html_t)
    template.Execute(os.Stdout, "Hello World!")

    html_t, err = gaml.GamlToHtml(gaml_template_2)
    if err != nil {
        fmt.Printf("error: %s", err.Error())
    }
    template,err = template.New("test_template2").Parse(html_t)
    if err != nil {
        fmt.Printf("error: %s", err.Error())
    }
    template.Execute(os.Stdout, People)


    html_t, err = gaml.GamlToHtml(gaml_template_3)
    if err != nil {
        fmt.Printf("error: %s", err.Error())
    }
    template,err = template.New("test_template3").Parse(html_t)
    if err != nil {
        fmt.Printf("error: %s", err.Error())
    }
    template.Execute(os.Stdout, People)

}

开发者ID:a2800276,项目名称:gaml,代码行数:30,代码来源:template.go

最后编辑: kuteng  文档更新时间: 2021-08-23 19:14   作者:kuteng