出于某种原因,这根本不起作用.
{{user_slugged username}}
这{{username}}是模板可用的变量.但是,它在助手中给出了一个null/undefined值.
这是我的帮手代码
UI.registerHelper('user_slugged', function(username) {
... other stuff... return things.
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,当我尝试这样的事情时,{{user_slugged 'Hello'}}它会做正确的事情并返回预期的结果.
但是,当我尝试{{user_slugged username}}它时似乎不起作用,即使我可以轻松地显示{{username}}在同一行代码中.
这看起来很奇怪,现在我正在考虑向把手发送参数的方式助手可能已经改变了Meteor 0.8.0.如果是这样,如果有人能指出我正确的方向或给我一个问题的答案,那就太棒了.
编辑:澄清我能够使用{{username}}相同的行,{{user_slugged username}}因此这样的工作
<a href="{{user_slugged username}}">{{username}}</a>
username 是一个对象属性,在模板中可用,并且我试图将其作为参数发送给帮助程序.
我在布局文件中有一个用于铁路由器的通用{{> yield}},它呈现我的页面,这是模板.
在我的一个页面中,我有一个侧边菜单,根据此菜单中的选择,我想在此页面中加载与此页面相关的不同模板.
我怎样才能做到这一点?
我有以下代码:
<div class="form-group {{#if afFieldIsInvalid name='latitude' OR name='longitude'}}has-error{{/if}}">......</div>
Run Code Online (Sandbox Code Playgroud)
如果空格键模板的条件如何,我如何使用AND/OR?
我在Spacebars模板中有这段代码:
1.
<select class="form-group">
{{#each choices}}
<option>{{this}}</option>
{{/each}}
</select>
Run Code Online (Sandbox Code Playgroud)
我想重复这个N次,每次增加数量,如下:
1.
<select class="form-group">
{{#each choices}}
<option>{{this}}</option>
{{/each}}
</select>
2.
<select class="form-group">
{{#each choices}}
<option>{{this}}</option>
{{/each}}
</select>
3.
<select class="form-group">
{{#each choices}}
<option>{{this}}</option>
{{/each}}
</select>
Run Code Online (Sandbox Code Playgroud)
我希望能够将N传递给自定义模板标签来处理这个问题(例如{{choices 3}}).这样做有什么好干的方法?我有一个模糊的概念,我可以写一个模板助手,但我不知道从哪里开始.
如何使用模板助手编辑传递给使用pathForiron-router方法创建的路由的参数值?
我有这个模板助手:
Template.registerHelper('slugify', function(obj){
return _.slugify(obj);
});
Run Code Online (Sandbox Code Playgroud)
在我的.html文件中我有这个:
{{#each menuItemsFromDB}}
{{#each arrayOfMenuItems}}
<a class="item category" href="">
{{this}}
</a>
{{/each}}
{{/each}}
Run Code Online (Sandbox Code Playgroud)
在{{this}}上面的代码返回一个字符串,该类别的名称.因为我已经注册了模板助手,所以我可以通过以下方式来解决这个类别:
{{slugify this}}
Run Code Online (Sandbox Code Playgroud)
然后我有这条路线:
this.route('List',{
path: '/list/:category_slug',
template: 'list',
controller: 'ListController'
});
Run Code Online (Sandbox Code Playgroud)
我可以链接到此路由并通过以下方式将参数传递给此路由:
{{pathFor 'List' category_slug='the-value-i-passed'}}
Run Code Online (Sandbox Code Playgroud)
但那将是难以编码的,它无法达到我想要的预期效果.
我希望它是动态的,使用'slugify'模板助手和铁路由器的pathFor方法,并使用值{{this}}.
我想要实现的是这样的,虽然下面的代码不起作用:
{{pathFor 'List' category_slug={{slugify this}} }}
Run Code Online (Sandbox Code Playgroud)
通过以上线路实现我正在努力实现的目标是什么?
我希望我可以这样做:
{{pathFor 'List' category_slug=slugify(this) }}
Run Code Online (Sandbox Code Playgroud)
要么
{{pathFor 'List' category_slug='{{slugify this}}' }}
Run Code Online (Sandbox Code Playgroud) 刚刚开始使用Meteor,所以我可能会遗漏一些基本的东西.在Meteor 1.2中,他们有{{@index}}指令.
在模板中,如果我有:
...
{{#each items}}
{{@index}}
{{> childTemplate}}
{{/each}}
...
<template name="childTemplate">
{{@index}}
</template>
Run Code Online (Sandbox Code Playgroud)
该@index主模板将工作,但一个在childTemplate不会.我使用它的工作是调用childTemplate传入@index:
{{> childTemplate @index=@index}}
Run Code Online (Sandbox Code Playgroud)
这是正确的方法吗?还是有更多的东西?
虽然这是按预期呈现的:
{{#each Items}} // a collection
{{title}}
{{/each}}
{{#each types}} // another ollection
{{refId}}
{{/each}}
Run Code Online (Sandbox Code Playgroud)
如果我把它放在一起:
{{#each Items}}
{{title}}
{{#each types}}
{{refId}}
{{/each}}
{{/each}}
Run Code Online (Sandbox Code Playgroud)
这#each types是空的.
模板助手:
Template.menuItems.helpers({
restMenuItems: function() {
return RestMenuItems.find({restRefId: this._id}, {sort:Template.instance().sort.get()});
},
restMenuSideItems: function() {
return RestMenuSideItems.find({restRefId: this._id}, {sort:Template.instance().sort.get()});
}
});
Template.menuItems.onCreated( function(){
this.subscribe('restMenuItems');
this.subscribe('restMenuSideItems');
});
Run Code Online (Sandbox Code Playgroud)
还有一些模板代码:
{{#each restMenuItems}}
<div>
<select class="list-group select_side_addons">
{{#each restMenuSideItems}}
<option>{{restRefId}}</option>
{{/each}}
</select>
</div>
{{/each}}
Run Code Online (Sandbox Code Playgroud)
即使更换时{{#each restMenuSideItems}}通过{{#each ../restMenuSideItems}},似乎什么都没有.
怎么了?
我会用这个:http://handlebarsjs.com/expressions.html#subexpressions
{{outer-helper (inner-helper 'abc') 'def'}}
但流星给我一个错误...有一些解决方案或解决方法使用嵌套助手?
谢谢!
因此,当我使用Meteor时,我正试图在我的Spacebars模板中保持高效和干净.但我对处理复选框和选择选项的方式感到困惑.假设我想要一个复选框设置为是否选中,具体取决于我的某个集合中的文档中的标志.我似乎无法执行以下操作:
<input type='checkbox' id='item-{{this.item_id}}' {{#if checked}}checked{{/if}} />
Run Code Online (Sandbox Code Playgroud)
当我尝试这个时,我收到以下错误:
A template tag of type BLOCKOPEN is not allowed here.
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试以下选项,它们都会导致复选框被检查,即使标志是false:
<input type='checkbox' id='item-{{this.item_id}}' checked='{{#if checked}}true{{/if}}' />
<input type='checkbox' id='item-{{this.item_id}}' checked='{{#if checked}}true{{else}}false{{/if}}' />
Run Code Online (Sandbox Code Playgroud)
我selected在我的选择选项中遇到了同样的问题,所以我最终做了类似下面的事情来解决它,这看起来很冗长且容易出错:
<select id='option-{{this.item_id}}'>
{{#if option_60}}
<option value='60' selected>1 hour</option>
{{else}}
<option value='60'>1 hour</option>
{{/if}}
{{#if option_90}}
<option value='90' selected>90 mins</option>
{{else}}
<option value='90'>90 mins</option>
{{/if}}
{{#if option_120}}
<option value='120' selected>2 hours</option>
{{else}}
<option value='120'>2 hours</option>
{{/if}}
</select>
Run Code Online (Sandbox Code Playgroud) {{#e}}}中{{#if}}的这个简单示例会产生意外(对我而言)结果:
HTML:
<head>
<title>test</title>
</head>
<body>
{{> test yes=true}}
</body>
template name="test">
{{#if yes}}
<span>yes</span>
{{else}}
<span>no</span>
{{/if}}
<ul>
{{#each testItems}}
{{#if yes}}
<li>yes</li>
{{else}}
<li>no</li>
{{/if}}
{{/each}}
</ul>
</template>
Run Code Online (Sandbox Code Playgroud)
JS:
Template.test.helpers({
testItems: [1,2,3]
});
Run Code Online (Sandbox Code Playgroud)
输出:
是
我期待一个3 x的列表是......
这段代码有什么问题?