hugo/content/functions/default.md
Bjørn Erik Pedersen 50ec65fbe1 Squashed 'docs/' changes from 73f355ce..ef02e34e
ef02e34e Correct the mmark example frontmatter parameter
6e91e900 SectionPagesMenu > sectionPagesMenu
1a0db1a6 Adjust sectionPagesMenu
f9f87d9d Fix extension's missing period.
7062ae07 Remove Press and Articles page
771f2b38 Remove outdated and redudant content file for release notes
64cf47c3 Remove outdated note in docs contribution guide
bdb11b89 Fix typo
8324af70 Fixes broken link on Roadmap
d93f0992 functions: Add all missing binary comparison operators
fb7ae80a Fix typo in usage.md
fbdae08b Fix typo in content-management/taxonomies.md
66fab8d2 Make <title> less stuttery
b3cd4c22 Remove old temp release notes
5589ba96 Fix typos in templates/lists.md
af3a0807 http > HTTP
b2af90ae Remove formatting in description of blog article
6e2e60a9 Add blog article about Netlify files
0bb6f2f2 Use title in archetype file
7b2490ff Get the Archetypes up to new spec
f401d69b Load CSS and JS via HTTP/2 server push
4aef4944 Adjust titles
362acdb2 Fix typo in quickstart
c2440560 Remove inline icons from installation guide
d2edcbc3 Revert "Fix links to Disqus template documentation"
622f49cf Add a full commands section at the quick start end
752f065b Fix server command in README
93e08e19 Fix links to Disqus template documentation
5e0cfaa9 Adjust Linux install
d51397c2 Fix broken link in Quick Start
1fb39846 Add /quickstart alias to quickstart
7440616b Add new and simpler quickstart
b3ec6986 Let page title correspond to function name replaceRE
b44499c9 Add YouTube tutorial about taxonomies
88b9eb0e Add RSS templates example
6c0bde3f Update slice.md
6c212ea6 Reorder to match the following content order
d2122992 Complete "content" spelling under theme components
e4824eb3 Fix the output shortcode and its usage
0adfc945 Add archetypes YouTube video
638e9d9b Fix double "your" typo in taxonomies.md

git-subtree-dir: docs
git-subtree-split: ef02e34eaf296c3f94b4446b3c3347771e786057
2017-07-31 09:21:24 +02:00

2.4 KiB

title description qref godocref date publishdate lastmod 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
functions
docs
parent
functions
default DEFAULT INPUT
false false
/functions/default/
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 >}}