mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-24 21:56:05 +02:00
@@ -93,3 +93,37 @@ Home: true
|
||||
|
||||
`)
|
||||
}
|
||||
|
||||
func TestTry(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
files := `
|
||||
-- config.toml --
|
||||
baseURL = 'http://example.com/'
|
||||
-- layouts/index.html --
|
||||
Home.
|
||||
{{ $g := try ("hello = \"Hello Hugo\"" | transform.Unmarshal) }}
|
||||
{{ with $g.Err }}
|
||||
Err1: {{ . }}
|
||||
{{ else }}
|
||||
Value1: {{ $g.Value.hello | safeHTML }}|
|
||||
{{ end }}
|
||||
{{ $g := try ("hello != \"Hello Hugo\"" | transform.Unmarshal) }}
|
||||
{{ with $g.Err }}
|
||||
Err2: {{ . | safeHTML }}
|
||||
{{ else }}
|
||||
Value2: {{ $g.Value.hello | safeHTML }}|
|
||||
{{ end }}
|
||||
Try upper: {{ (try ("hello" | upper)).Value }}
|
||||
Try printf: {{ (try (printf "hello %s" "world")).Value }}
|
||||
`
|
||||
|
||||
b := hugolib.Test(t, files)
|
||||
|
||||
b.AssertFileContent("public/index.html",
|
||||
"Value1: Hello Hugo|",
|
||||
"Err2: template: index.html:",
|
||||
"Try upper: HELLO",
|
||||
"Try printf: hello world",
|
||||
)
|
||||
}
|
||||
|
Reference in New Issue
Block a user