mirror of
https://github.com/gohugoio/hugoDocs.git
synced 2025-09-10 08:44:40 -04:00
Add variable re-definition example (Hugo v0.48+)
This commit is contained in:
parent
21123967eb
commit
30b060dff7
@ -96,12 +96,28 @@ The custom variables need to be prefixed with `$`.
|
|||||||
```
|
```
|
||||||
|
|
||||||
{{% warning %}}
|
{{% warning %}}
|
||||||
Variables defined inside `if` conditionals and similar are not visible on the outside. See [https://github.com/golang/go/issues/10608](https://github.com/golang/go/issues/10608).
|
For Hugo v0.47 and older versions, variables defined inside `if`
|
||||||
|
conditionals and similar are not visible on the outside.
|
||||||
|
See [https://github.com/golang/go/issues/10608](https://github.com/golang/go/issues/10608).
|
||||||
|
|
||||||
Hugo has created a workaround for this issue in [Scratch](/functions/scratch).
|
Hugo has created a workaround for this issue in [Scratch](/functions/scratch).
|
||||||
|
|
||||||
{{% /warning %}}
|
{{% /warning %}}
|
||||||
|
|
||||||
|
For **Hugo v0.48** and newer, variables can be re-defined using the
|
||||||
|
new `=` operator (new in Go 1.11).
|
||||||
|
|
||||||
|
Below example will work only in these newer Hugo versions. The example
|
||||||
|
prints "Var is Hugo Home" on the home page, and "Var is Hugo Page" on
|
||||||
|
all other pages:
|
||||||
|
|
||||||
|
```go-html-template
|
||||||
|
{{ $var := "Hugo Page" }}
|
||||||
|
{{ if .IsHome }}
|
||||||
|
{{ $var = "Hugo Home" }}
|
||||||
|
{{ end }}
|
||||||
|
Var is {{ $var }}
|
||||||
|
```
|
||||||
|
|
||||||
## Functions
|
## Functions
|
||||||
|
|
||||||
Go Templates only ship with a few basic functions but also provide a mechanism for applications to extend the original set.
|
Go Templates only ship with a few basic functions but also provide a mechanism for applications to extend the original set.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user