mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 05:11:49 +08:00
04.4.md
This commit is contained in:
26
eBook/examples/chapter_15/template_ifelse.go
Normal file
26
eBook/examples/chapter_15/template_ifelse.go
Normal file
@@ -0,0 +1,26 @@
|
||||
// template_ifelse.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
func main() {
|
||||
tEmpty := template.New("template test")
|
||||
tEmpty = template.Must(tEmpty.Parse("Empty pipeline if demo: {{if ``}} Will not print. {{end}}\n")) //empty pipeline following if
|
||||
tEmpty.Execute(os.Stdout, nil)
|
||||
|
||||
tWithValue := template.New("template test")
|
||||
tWithValue = template.Must(tWithValue.Parse("Non empty pipeline if demo: {{if `anything`}} Will print. {{end}}\n")) //non empty pipeline following if condition
|
||||
tWithValue.Execute(os.Stdout, nil)
|
||||
|
||||
tIfElse := template.New("template test")
|
||||
tIfElse = template.Must(tIfElse.Parse("if-else demo: {{if `anything`}} Print IF part. {{else}} Print ELSE part.{{end}}\n")) //non empty pipeline following if condition
|
||||
tIfElse.Execute(os.Stdout, nil)
|
||||
}
|
||||
/* Output:
|
||||
Empty pipeline if demo:
|
||||
Non empty pipeline if demo: Will print.
|
||||
if-else demo: Print IF part.
|
||||
*/
|
Reference in New Issue
Block a user