A with action temporarily changes the template’s current dot to a specified value inside a block. This shortens repeated accessors and keeps the block focused on one part of the input. section 11.5.1 section 11.6
Use {{with .Date}} ... {{end}} to make .Date the local dot. Fields and methods then use shorter names such as .Start and .End; after {{end}}, the original dot is restored. section 11.5.1 section 11.6
Restrict a date display to its local value
Without a with action: <p>This period covers {{.Date.Start}} -> {{.Date.End}}.</p>\n\nWith a with action: {{with .Date}}<p>This period covers {{.Start}} -> {{.End}}.</p>{{end}}
Apply the action when:
- A template receives several values through a map, such as Habits and Date, and one block repeatedly accesses a nested value. section 11.5.1
- A conditional block should render only when its selected value is present; use if separately when hiding an empty habits list. section 11.5.1
