Golang Echo Code detailed template Chapter

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. Using the template in echo must first register a, if not registered will be reported the following error "" Json{"Time": "2017-12-12t23:03:57.939138716+08:00", "Level": "Error", " Prefix ":" echo "," File ":" Echo.go "," line ":" 284 "," message ":" Renderer not Registered "}" is registered to echo. The Renderer is assigned a value. The Renderer property of ECHO is an interface ' Gorenderer interface {Render (IO). Writer, String, interface{}, Context) error} ' # # # One, using the standard library template Echo's document gives the way to register using the official template "go//implementation Renderer interface type Template str UCT {templates *template. Template}func (t *template) Render (w io. Writer, name string, data interface{}, C echo. Context) Error {return t.templates.executetemplate (w, name, data)}//processing the corresponding template for file generation in the View directory//About Executetemplate and PARSEGL Ob method can be viewed//https://wizardforcel.gitbooks.io/golang-stdlib-ref/content/67.html#Template.ExecuteTemplatet: = & Template{templates:template. Must (template. Parseglob ("public/views/*.html")),}//assignment E. Renderer = T ' # # # II, using Pongo2 ' Gopkg.in/flosch/pongo2.v3 ' is a very good template engine, many times it will choose to render the template. The first is to implement the Renderer interface ' Gotype Template struct {//here use a map to store the pre-placeThe modeled version of Tmplmap Map[string]*pongo2. Template}func (t *template) Render (w io. Writer, templatename string, Data interface{}, C echo. Context) Error {//Here according to the name from the Tmplmap to find the template to render//Note the Excutewriter parameter must be map[string]interface{} datamap: = data. map[string]interface{}) template, exist: = t.tmplmap[templatename]if!exist {return errors. New ("template" + templatename + "not Found")}return template. Executewriter (DataMap, W)} "and then precompiled template" go//read the file in the directory preprocessing func precompile (dir string) *template {tmplmap: = Make (map[ String]*pongo2. Template) Dirpath: = FilePath. Dir (dir) Fileinfos, _: = Ioutil. ReadDir (Dirpath) for _, FileInfo: = Range Fileinfos {T, err: = Pongo2. FromFile (path. Join (dir, Fileinfo.name ())) if err! = Nil {log. Fatalf ("\"%s\ ":%v", Fileinfo.name (), err)}tmplmap[strings. Replace (Fileinfo.name (), Path. Ext (Fileinfo.name ()), "",-1)] = T}return &template{tmplmap}} ' last assignment ' ' Gofunc newtemplates (dir string) *template { Return Precompile (dir)}t: = Newtemplates ("./views/") E.renderer = t "", such as custom RThe Enderer is finished. Original address: [Laily.net] (https://laily.net/article/golang%20echo%20%E4%BB%A3%E7%A0%81%E8%AF%A6%E8%A7%A3%E4%B9%8B%E6%A8 %a1%e7%89%88%e7%af%87) 1172 reads  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.