{{#with(expression)}}
Changes the context within a block.
{{#with(EXPRESSION)}}BLOCK{{/with}}
Renders BLOCK
with the result of EXPRESSION
added to the top of the can-view-scope.
{{#with(person.address)}}
Street: {{street}}
City: {{city}}
{{/with}}
Parameters
- EXPRESSION
{KeyLookup Expression|Call Expression}
:A lookup expression that will provide a value.
- BLOCK
{sectionRenderer(context, helpers)}
:A template that is rendered with the context of the
EXPRESSION
’s value.
{{#with(HASHES)}}BLOCK{{/with}}
Renders BLOCK
with the key-value pairs from a Hash Expression added to the top of the can-view-scope.
{{#with(innerStreet=person.address.street innerCity=person.address.city)}}
Street: {{innerStreet}}
City: {{innerCity}}
{{/with}}
Parameters
- HASHES
{Hash Expression}
:Any number of hash keys and values
- BLOCK
{sectionRenderer(context, helpers)}
:A template that is rendered with the hashes added to the context.
Use
{{#with()}}
renders a subsection with a new context added to the can-view-scope.
For example:
<!-- Template -->
{{#with(person.address)}}
Street: {{street}}
City: {{city}}
{{/with}}
/* Data */
{person: {address: {street: "123 Evergreen", city: "Springfield"}}}
<!-- Result -->
Street: 123 Evergreen
City: Springfield
The new context can be a lookup expression, or a set of hashes which are taken together to be a new context.
<!-- Template -->
{{#with(innerStreet=person.address.street innerCity=person.address.city)}}
Street: {{innerStreet}}
City: {{innerCity}}
{{/with}}
/* Data */
{person: {address: {street: "123 Evergreen", city: "Springfield"}}}
<!-- Result -->
Street: 123 Evergreen
City: Springfield
The difference between {{#with()}}
and the default {{#expression}}
is that the subsection BLOCK
is rendered no matter what:
<!-- Template -->
{{#with(person.address)}}
Street: {{street}}
City: {{city}}
{{/with}}
/* Data */
{person: {}}
<!-- Result -->
Street:
City: