rad*_*ual 2 html javascript templates handlebars.js keystonejs
我试图弄清楚为什么我无法从each我的手柄模板中的嵌入语句中访问我的模板变量(在我的keystone项目中呈现).我在StackOverflow上已经阅读了许多类似问题的问题,但没有一个解决了我的特定用例.
我有一个简单的数据对象,传递给Handlebars模板,看起来像[粗略]这样:
{
rootPath: '../../../',
navigation: { all: { 'a': { sublinks: [Object] }
}
Run Code Online (Sandbox Code Playgroud)
为什么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}}
Run Code Online (Sandbox Code Playgroud)
我得到以下输出(注意所有子级别尝试无法访问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令牌解决问题.
只需@root.rootPath从嵌入式内部调用{{#each sublinks}}即可解决我的问题.
这是正确的模板标记:
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}}
Run Code Online (Sandbox Code Playgroud)
产生以下输出:
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 -->
Run Code Online (Sandbox Code Playgroud)
但是,从内嵌{{#each sublinks}},只有@root.rootPath似乎工作:
{{@root.rootPath}} <!-- works -->
{{../../rootPath}} <!-- doesn't work -->
Run Code Online (Sandbox Code Playgroud)