github.com/rabbouni145/gg@v0.47.1/docs/content/en/functions/default.md (about)

     1  ---
     2  title: default
     3  description: Allows setting a default value that can be returned if a first value is not set.
     4  qref: "Returns a default value if a value is not set when checked."
     5  godocref:
     6  date: 2017-02-01
     7  publishdate: 2017-02-01
     8  lastmod: 2017-02-01
     9  keywords: [defaults]
    10  categories: [functions]
    11  menu:
    12    docs:
    13      parent: "functions"
    14  toc:
    15  signature: ["default DEFAULT INPUT"]
    16  workson: []
    17  hugoversion:
    18  relatedfuncs: []
    19  deprecated: false
    20  draft: false
    21  aliases: []
    22  needsexamples: false
    23  ---
    24  
    25  `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:
    26  
    27  * non-zero for numeric types and times
    28  * non-zero length for strings, arrays, slices, and maps
    29  * any boolean or struct value
    30  * non-nil for any other types
    31  
    32  `default` function examples reference the following content page:
    33  
    34  {{< code file="content/posts/default-function-example.md" >}}
    35  ---
    36  title: Sane Defaults
    37  seo_title:
    38  date: 2017-02-18
    39  font:
    40  oldparam: The default function helps make your templating DRYer.
    41  newparam:
    42  ---
    43  {{< /code >}}
    44  
    45  `default` can be written in more than one way:
    46  
    47  ```
    48  {{ index .Params "font" | default "Roboto" }}
    49  {{ default "Roboto" (index .Params "font") }}
    50  ```
    51  
    52  Both of the above `default` function calls return `Roboto`.
    53  
    54  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:
    55  
    56  {{< code file="variable-as-default-value.html" nocopy="true" >}}
    57  {{$old := .Params.oldparam }}
    58  <p>{{ .Params.newparam | default $old }}</p>
    59  {{< /code >}}
    60  
    61  Which would return:
    62  
    63  ```
    64  <p>The default function helps make your templating DRYer.</p>
    65  ```
    66  
    67  And then using dot notation
    68  
    69  {{< code file="dot-notation-default-value.html" >}}
    70  <title>{{ .Params.seo_title | default .Title }}</title>
    71  {{< /code >}}
    72  
    73  Which would return
    74  
    75  {{< output file="dot-notation-default-return-value.html" >}}
    76  <title>Sane Defaults</title>
    77  {{< /output >}}
    78  
    79  The following have equivalent return values but are far less terse. This demonstrates the utility of `default`:
    80  
    81  Using `if`:
    82  
    83  {{< code file="if-instead-of-default.html" nocopy="true" >}}
    84  <title>{{if .Params.seo_title}}{{.Params.seo_title}}{{else}}{{.Title}}{{end}}</title>
    85  => Sane Defaults
    86  {{< /code >}}
    87  
    88  Using `with`:
    89  
    90  {{< code file="with-instead-of-default.html" nocopy="true" >}}
    91  <title>{{with .Params.seo_title}}{{.}}{{else}}{{.Title}}{{end}}</title>
    92  => Sane Defaults
    93  {{< /code >}}