Question

Let's say you have a partial of some sort, which uses a global variable:

<a href="/" class="logo"><img src="{{assets}}/logo.png"></a>

Once you include the partial with specific data, either {{parseJSON}} or external JSON data:

{{#parseJSON '{"demo": true}'}}
{{>navigation}}
{{/parseJSON}}

all global variables like {{assets}} "killed" or at least overridden. Is there any chance to have still access?

Thanks in advance!

Était-ce utile?

La solution

this is a known "issue" with Handlebar's partials in that the context inside a partial only includes the passed in context and no parent context, etc.. you can read more about the issue here.

fortunately the Assemble team provides a {{partial}} helper that will allow access to a more "expected" context, including global properties. once this helper is installed, you use it in a slightly different way than a normal partial:

{{#parseJSON '{"demo": true}'}}
  {{partial "navigation"}}
{{/parseJSON}}

note that the partial helper is invoked with the name of the partial to include as a string.

that all being said, a new version of Handlebars was just released (v2.0.0-alpha.1) that may provide support for this natively. check out the more recent posts of issue thread i linked to above.

hope this helps.

Autres conseils

As a workaround, the other way to cope with this is to pass the parent context explicitly to the partial:

{{#..}}
  {{>partial}}
{{/..}}

Yeah.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top