with.md (1427B)
1 ---
2 title: with
3 # linktitle: with
4 description: Rebinds the context (`.`) within its scope and skips the block if the variable is absent or empty.
5 date: 2017-02-01
6 publishdate: 2017-02-01
7 lastmod: 2017-03-12
8 categories: [functions]
9 menu:
10 docs:
11 parent: "functions"
12 keywords: [conditionals]
13 signature: ["with INPUT"]
14 workson: []
15 hugoversion:
16 relatedfuncs: []
17 deprecated: false
18 ---
19
20 An alternative way of writing an `if` statement and then referencing the same value is to use `with` instead. `with` rebinds the context (`.`) within its scope and skips the block if the variable is absent, unset or empty.
21
22 The set of *empty* values is defined by [the Go templates package](https://golang.org/pkg/text/template/). Empty values include `false`, the number zero, and the empty string.
23
24 If you want to render a block if an index or key is present in a slice, array, channel or map, regardless of whether the value is empty, you should use [`isset`](/functions/isset) instead.
25
26 The following example checks for a [user-defined site variable](/variables/site/) called `twitteruser`. If the key-value is not set, the following will render nothing:
27
28 {{< code file="layouts/partials/twitter.html" >}}
29 {{with .Site.Params.twitteruser}}<span class="twitter">
30 <a href="https://twitter.com/{{.}}" rel="author">
31 <img src="/images/twitter.png" width="48" height="48" title="Twitter: {{.}}"
32 alt="Twitter"></a>
33 </span>{{end}}
34 {{< /code >}}