package backendtest import ( "bytes" "fusenapi/model/gmodel" "fusenapi/utils/collect" "fusenapi/utils/format" "html/template" "log" "testing" ) func TestCaseTpl(t *testing.T) { priceInfo := []map[string]interface{}{ {"Price": "$10", "Num": 100}, {"Price": "$20", "Num": 200}, {"Price": "$30", "Num": 300}, {"Price": "$40", "Num": 400}, } priceHtml := ` {{range $i, $parr := .}} {{if lt $i 3}}

${{$parr.Price}}

{{$parr.Num}} Units

{{else}}

${{$parr.Price}}

{{$parr.Num}} Units

{{end}} {{end}}` tpl := template.Must(template.New("demoHtml").Parse(priceHtml)) buf := bytes.NewBufferString("") tpl.Execute(buf, priceInfo) html := buf.String() log.Println(html) } func Randomproduct() []*gmodel.FsQuotationProduct { var quotationId1 int64 = 1 name1 := "T恤" size1 := "S,M,L,XL,XXL" var cycle1 int64 = 7 var isGift1 int64 = 0 img1 := "https://xxx.jpg" var status1 int64 = 1 var ctime1 int64 = 1623311101 var sort1 int64 = 1 sid1 := "abcd1234" priceInfo1 := `[{"num":500,"price":0.1},{"num":1000,"price":0.06},{"num":1500,"price":0.06}]` remark1 := "" var num1 int64 = 100 var quotationId2 int64 = 1 name2 := "帽子" size2 := "通用" var cycle2 int64 = 3 var isGift2 int64 = 1 img2 := "https://xxx.jpg" var status2 int64 = 1 var ctime2 int64 = 1623311102 var sort2 int64 = 2 sid2 := "abcd1235" priceInfo2 := `[{"num":500,"price":0.1},{"num":1000,"price":0.06},{"num":1500,"price":0.06}]` remark2 := "" var num2 int64 = 50 var quotationId3 int64 = 2 name3 := "水壶" size3 := "350ml,500ml,650ml" var cycle3 int64 = 14 var isGift3 int64 = 0 img3 := "https://xxx.jpg" var status3 int64 = 1 var ctime3 int64 = 1623311103 var sort3 int64 = 1 sid3 := "abcd1236" priceInfo3 := `[{"num":500,"price":0.1},{"num":1000,"price":0.06},{"num":1500,"price":0.06}]` remark3 := "" var num3 int64 = 200 var quotationId4 int64 = 2 name4 := "手机壳" size4 := "iPhoneX,iPhoneXS,iPhoneXR" var cycle4 int64 = 5 var isGift4 int64 = 1 img4 := "https://xxx.jpg" var status4 int64 = 1 var ctime4 int64 = 1623311104 var sort4 int64 = 2 sid4 := "abcd1237" priceInfo4 := `[{"num":500,"price":0.1},{"num":1000,"price":0.06},{"num":1500,"price":0.06}]` remark4 := "可定制颜料颜色" var num4 int64 = 150 products := []*gmodel.FsQuotationProduct{ &gmodel.FsQuotationProduct{ Id: 1, QuotationId: "ationId1, Name: &name1, Size: &size1, Cycle: &cycle1, IsGift: &isGift1, Img: &img1, Status: &status1, Ctime: &ctime1, Sort: &sort1, Sid: &sid1, PriceInfo: &priceInfo1, Remark: &remark1, Num: &num1, }, &gmodel.FsQuotationProduct{ Id: 2, QuotationId: "ationId2, Name: &name2, Size: &size2, Cycle: &cycle2, IsGift: &isGift2, Img: &img2, Status: &status2, Ctime: &ctime2, Sort: &sort2, Sid: &sid2, PriceInfo: &priceInfo2, Remark: &remark2, Num: &num2, }, &gmodel.FsQuotationProduct{ Id: 3, QuotationId: "ationId3, Name: &name3, Size: &size3, Cycle: &cycle3, IsGift: &isGift3, Img: &img3, Status: &status3, Ctime: &ctime3, Sort: &sort3, Sid: &sid3, PriceInfo: &priceInfo3, Remark: &remark3, Num: &num3, }, &gmodel.FsQuotationProduct{ Id: 4, QuotationId: "ationId4, Name: &name4, Size: &size4, Cycle: &cycle4, IsGift: &isGift4, Img: &img4, Status: &status4, Ctime: &ctime4, Sort: &sort4, Sid: &sid4, PriceInfo: &priceInfo4, Remark: &remark4, Num: &num4, }, } return products } func TestTpl2(t *testing.T) { products := Randomproduct() tplcontent := ` {{$product_num := len .products}} {{$page := 3}} {{$page_total := add $page $product_num}} {{range $arr := .products}} {{$price := json_decode $arr.price_info}} {{$price_html := .priceHtml}} {{$price_html}} {{if gt $arr.is_gift 0 }}{{/* 赠品 */}} // 赠品

{{$arr.name}}

{{$arr.size}}

FREE!

{{$arr.num}} Units

Lead Time
{{$arr.cycle}}Days
FREE Design
FREE Storage
FREE Shipping
{{else}}{{/* 非赠品 */}} {{if $arr.size}} // 有尺寸的

{{$arr.name}}

{{$arr.size}}

Unit Price (Tax included)
Lead Time
{{$price_html}}
{{$arr.cycle}}Days
FREE Design
FREE Storage
FREE Shipping
{{else}}

{{$arr.name}}

Unit Price (Tax included)
Lead Time
{{$price_html}}
{{$arr.cycle}}Days
FREE Design
FREE Storage
FREE Shipping
{{end}} {{end}}
{{$page}}/{{$page_total}}
{{$page = add $page 1}} {{end}} ` tpl := template.New("demo") tpl.Funcs(format.TemplateFuncMap) tpl = template.Must(tpl.Parse(tplcontent)) buf := bytes.NewBufferString("") tpl.Execute(buf, map[string]interface{}{"products": collect.StructSliceJson2Maps(products)}) html := buf.String() log.Println(html) }