Handlebars 范围问题:无法从嵌入式“each”访问模板变量
Handlebars Scope Issue: Can't access template variable from embedded `each`
我想弄清楚为什么我无法从车把模板(在我的 keystone 项目中呈现)的嵌入式 each
语句中访问我的模板变量。我在 Whosebug 上阅读了很多类似问题的问题,但是 none 解决了我的特定用例。
我有一个简单的数据对象被传递到 Handlebars 模板,看起来[大致]如下:
{
rootPath: '../../../',
navigation: { all: { 'a': { sublinks: [Object] }
}
我的问题是:
为什么 rootPath
模板变量在 {{#each navigation.all}}
中完美打印,但无法从 {{#each sublinks}}
中访问?
这是我的 Handlebars 模板:
<!-- rootPath prints fine here -->
Here is your rootPath variable: {{rootPath}}
{{#each navigation.all}}
<!-- rootPath prints fine here -->
top-level rootPath: {{../rootPath}}
{{#if sublinks}}
{{#each sublinks}}
<!-- WHY WON'T ROOTPATH PRINT HERE?? -->
<!-- Obviously, I am trying the three possible scope paths -->
sub-level rootPath attempt 1: {{../../rootPath}}
sub-level rootPath attempt 2: {{../rootPath}}
sub-level rootPath attempt 3: {{rootPath}}
{{/each}}
{{/if}}
{{/each}}
我得到以下输出 (请注意所有子级别尝试如何无法访问 rootPath
模板变量):
Here is your rootPath variable: ../../../
top-level rootPath: ../../../
sub-level rootPath attempt 1:
sub-level rootPath attempt 2:
sub-level rootPath attempt 3:
我需要的是 rootPath
随处可用,所以我可以得到这样的东西:
Here is your rootPath variable: ../../../
top-level rootPath: ../../../
sub-level rootPath: ../../../
那么我在这里缺少什么范围问题或语法?
我使用的是 Handlebars 版本 2.0.0
,因为这是 keystone 预先打包的版本。
我能够使用 @root
令牌解决问题。
只需从嵌入式 {{#each sublinks}}
中调用 @root.rootPath
就足以解决我的问题。
这是正确的模板标记:
Here is your rootPath variable: {{rootPath}}
{{#each navigation.all}}
top-level rootPath 1: {{../rootPath}} <!-- works -->
top-level rootPath 2: {{@root.rootPath}} <!-- works -->
{{#if sublinks}}
{{#each sublinks}}
sub-level rootPath 1: {{@root.rootPath}} <!-- works -->
sub-level rootPath 2: {{../../rootPath}} <!-- doesn't work -->
{{/each}}
{{/if}}
{{/each}}
产生以下输出:
Here is your rootPath variable: ../../../
top-level rootPath 1: ../../../
top-level rootPath 2: ../../../
sub-level rootPath 1: ../../../
sub-level rootPath 2:
注意: 用于顶级 rootPath(在 {{#each navigation.all}}
中,我可以使用其中之一:
{{../rootPath}} <!-- works -->
{{@root.rootPath}} <!-- works -->
但是,在嵌入式 {{#each sublinks}}
中,只有 @root.rootPath
似乎有效:
{{@root.rootPath}} <!-- works -->
{{../../rootPath}} <!-- doesn't work -->
我想弄清楚为什么我无法从车把模板(在我的 keystone 项目中呈现)的嵌入式 each
语句中访问我的模板变量。我在 Whosebug 上阅读了很多类似问题的问题,但是 none 解决了我的特定用例。
我有一个简单的数据对象被传递到 Handlebars 模板,看起来[大致]如下:
{
rootPath: '../../../',
navigation: { all: { 'a': { sublinks: [Object] }
}
我的问题是:
为什么 rootPath
模板变量在 {{#each navigation.all}}
中完美打印,但无法从 {{#each sublinks}}
中访问?
这是我的 Handlebars 模板:
<!-- rootPath prints fine here -->
Here is your rootPath variable: {{rootPath}}
{{#each navigation.all}}
<!-- rootPath prints fine here -->
top-level rootPath: {{../rootPath}}
{{#if sublinks}}
{{#each sublinks}}
<!-- WHY WON'T ROOTPATH PRINT HERE?? -->
<!-- Obviously, I am trying the three possible scope paths -->
sub-level rootPath attempt 1: {{../../rootPath}}
sub-level rootPath attempt 2: {{../rootPath}}
sub-level rootPath attempt 3: {{rootPath}}
{{/each}}
{{/if}}
{{/each}}
我得到以下输出 (请注意所有子级别尝试如何无法访问 rootPath
模板变量):
Here is your rootPath variable: ../../../ top-level rootPath: ../../../ sub-level rootPath attempt 1: sub-level rootPath attempt 2: sub-level rootPath attempt 3:
我需要的是 rootPath
随处可用,所以我可以得到这样的东西:
Here is your rootPath variable: ../../../ top-level rootPath: ../../../ sub-level rootPath: ../../../
那么我在这里缺少什么范围问题或语法?
我使用的是 Handlebars 版本 2.0.0
,因为这是 keystone 预先打包的版本。
我能够使用 @root
令牌解决问题。
只需从嵌入式 {{#each sublinks}}
中调用 @root.rootPath
就足以解决我的问题。
这是正确的模板标记:
Here is your rootPath variable: {{rootPath}}
{{#each navigation.all}}
top-level rootPath 1: {{../rootPath}} <!-- works -->
top-level rootPath 2: {{@root.rootPath}} <!-- works -->
{{#if sublinks}}
{{#each sublinks}}
sub-level rootPath 1: {{@root.rootPath}} <!-- works -->
sub-level rootPath 2: {{../../rootPath}} <!-- doesn't work -->
{{/each}}
{{/if}}
{{/each}}
产生以下输出:
Here is your rootPath variable: ../../../ top-level rootPath 1: ../../../ top-level rootPath 2: ../../../ sub-level rootPath 1: ../../../ sub-level rootPath 2:
注意: 用于顶级 rootPath(在 {{#each navigation.all}}
中,我可以使用其中之一:
{{../rootPath}} <!-- works -->
{{@root.rootPath}} <!-- works -->
但是,在嵌入式 {{#each sublinks}}
中,只有 @root.rootPath
似乎有效:
{{@root.rootPath}} <!-- works -->
{{../../rootPath}} <!-- doesn't work -->