Updates #9737
This commit is contained in:
Bjørn Erik Pedersen
2022-04-19 16:56:49 +02:00
parent 723e3f4342
commit 5d2cbee989
4 changed files with 71 additions and 2 deletions

View File

@@ -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",
)
}