Notes Golang Template

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

1.header.tmpl

1 {{define ' header '}}2 <HTML>3 <Head>4 <title>Presentation Information</title>5 </Head>6 <Body>7{{End}}

2. Content.tmpl

1 {{define "content"}}2 {{Template ' header '}}3 <H1>Demo nesting</H1>4 <ul>5 <Li>Nesting using define to define child templates</Li>6 <Li>Call using template</Li>7 </ul>8 {{Template ' footer '}}9{{End}}

3. Footer.tmpl

1 {{define ' footer '}} 2 </ Body > 3 </ HTML > 4 {{END}}

4. Main.go

1 Package Main2 3 Import (4     "FMT"5     "html/template"6     "OS"7     "Strings"8 )9 Ten type person struct { One UserName String A Emails []string -Friends []*Friend - } the  - type Friend struct { - Fname String - } +  - func emaildealwith (args ... interface{}) string { +OK: =false A var s string at     ifLen (args) = = 1 { -s, OK =Args[0]. (string) -     } -     if!ok { -s =FMT. Sprint (args) -     } inSubstrs: = Strings. Split (S,"@") -     ifLen (substrs)! = 2 { to         returns +     } -     return(Substrs[0] +" at"+ substrs[1]) the } *  $ Func Main () {Panax Notoginseng demo1 () - Demo2 () the Demo3 () + Demo4 () A } the  + func demo1 () { -T: = template. New ("FieldName Example") $T, _ = T.parse ("Hello {{. username}}!") $P: = person{username:"Zhangsan"} - t.execute (OS. Stdout, p) - } the  - func Demo2 () {WuyiF1: = Friend{fname:"minux.ma"} theF2: = friend{fname:"Xushiwei"} -T: = template. New ("FieldName Example") Wut = T.funcs (template. funcmap{"Emaildeal": Emaildealwith})//How template functions are used -T, _ =T.parse (' About Hello {{. username}}! $ {{range.} Emails}} -An email {{. |Emaildeal}} - {{end}} - {{with. Friends}} A {{range.}} +My friend name is {{. Fname}} the {{end}} - {{end}} ') $P: = person{username:"Astaxie", theEmails: []string{"astaxie@beego.me","astaxie@gmail.com"}, theFriends: []*friend{&f1, &F2}} the t.execute (OS. Stdout, p) the } -  in func Demo3 () { theTempty: = template. New ("Template Test") the//must its role is to detect if the template is correct, such as whether the braces match, whether the comment is closed correctly, and whether the variable is written correctly AboutTempty = template. Must (Tempty.parse ("NULL pipeline if Demo:{{if '}} does not return output. {{end}}\n")) the tempty.execute (OS. Stdout, nil) the  theTwithvalue: = template. New ("Template Test") +Twithvalue = template. Must (Twithvalue.parse ("Pipeline If Demo: {{if ' anything '}} I have the content and I will output it. {{end}}\n")) - twithvalue.execute (OS. Stdout, nil) the BayiTifelse: = template. New ("Template Test") theTifelse = template. Must (Tifelse.parse ("if-else demo:{{if ' anything '}} if section {{Else}} else part. {{end}}\n")) the tifelse.execute (OS. Stdout, nil) - } -  the func Demo4 () { theS1, _: = template. Parsefiles ("Header.tmpl","Content.tmpl","Footer.tmpl") theS1. Executetemplate (OS. Stdout,"Header", nil) the FMT. Println () -S1. Executetemplate (OS. Stdout,"content", nil) the FMT. Println () theS1. Executetemplate (OS. Stdout,"Footer", nil) the FMT. Println ()94 S1. Execute (OS. Stdout, nil) the}

Related Article

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.