d88477eb3 Fix some more redirect alias loops 209735670 Update features.md d4ec6ef87 Remove redirect loop from functions/default ce14da435 Fix the AlternativeOutputFormats/OutputFormats examples f64a22469 Update cond.md 357a70a32 Remove redirect loop from templates/404 4ad7b01ba Remove some aliases that were causing infinite redirect loops fd378e66a Fix alias redirect loop ec3629f6f Revert "Set code background to white" ffc56b880 Revert "Make inline code gray" 2a964e261 Make inline code gray 44069e09c Set code background to white d39c790d0 Linked my own blog2md migration script 3e55267be Release 0.46 7b6cfdd8b releaser: Prepare repository for 0.47-DEV d4cb54806 releaser: Add release notes to /docs for release of 0.46 6518ac7e7 releaser: Bump versions for release of 0.46 e54334d5c Merge commit '766085c2dc6fc95ac30fda2a9ebde2355fc12554' git-subtree-dir: docs git-subtree-split: d88477eb3a1959e2764d6025f5aa7a57a4a611e7
2.4 KiB
title | description | qref | godocref | date | publishdate | lastmod | keywords | categories | menu | toc | signature | workson | hugoversion | relatedfuncs | deprecated | draft | aliases | needsexamples | |||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
default | Allows setting a default value that can be returned if a first value is not set. | Returns a default value if a value is not set when checked. | 2017-02-01 | 2017-02-01 | 2017-02-01 |
|
|
|
|
false | false | false |
default
checks whether a given value is set and returns a default value if it is not. Set in this context means different things depending on date type:
- non-zero for numeric types and times
- non-zero length for strings, arrays, slices, and maps
- any boolean or struct value
- non-nil for any other types
default
function examples reference the following content page:
{{< code file="content/posts/default-function-example.md" >}}
title: Sane Defaults seo_title: date: 2017-02-18 font: oldparam: The default function helps make your templating DRYer. newparam:
{{< /code >}}
default
can be written in more than one way:
{{ index .Params "font" | default "Roboto" }}
{{ default "Roboto" (index .Params "font") }}
Both of the above default
function calls return Roboto
.
A default
value, however, does not need to be hard coded like the previous example. The default
value can be a variable or pulled directly from the front matter using dot notation:
{{< code file="variable-as-default-value.html" nocopy="true" >}} {{$old := .Params.oldparam }}
{{ .Params.newparam | default $old }}
{{< /code >}}Which would return:
<p>The default function helps make your templating DRYer.</p>
And then using dot notation
{{< code file="dot-notation-default-value.html" >}}
{{< /code >}}Which would return
{{< output file="dot-notation-default-return-value.html" >}}
{{< /output >}}The following have equivalent return values but are far less terse. This demonstrates the utility of default
:
Using if
:
{{< code file="if-instead-of-default.html" nocopy="true" >}}
=> Sane Defaults {{< /code >}}Using with
:
{{< code file="with-instead-of-default.html" nocopy="true" >}}
=> Sane Defaults {{< /code >}}