如何不做帮手?
How to NOT a helper?
执行 NOT 运算的最佳方法是什么?
例如,这里是 javascript
Template.mytemplate.helper({
myhelper : function(){
return true;
}
});
现在如何在模板中获取 !myhelper?我一直在做
<template name="mytemplate">
{{#if myhelper}}
{{else}}
myhelper is false
{{/if}}
</template>
但一定有更好的方法。我可以创建一个平等助手并执行 {{#if equal myhelper true}}
但这看起来不太干净。
有没有人有更好的主意?
我认为您应该可以使用 #unless
空格键来获得您想要的内容:
<template name="mytemplate">
{{#unless myhelper}}
myhelper is false
{{/unless}}
</template>
我建议只在您的助手名称中使用 isNot
。
比较:
<template name="mytemplate">
<!--
✓ Easy to read
– Looks not clean
-->
{{#if isActive}}
{{else}}
item is not active
{{/if}}
<!--
- Not very easy to read
✓ Looks clean
-->
{{#unless isActive}}
item is not active
{{/unless}}
</template>
Template.mytemplate.helpers({
isActive: function() {
return Session.get('isActive');
},
});
对比:
<template name="mytemplate">
<!--
✓ Easy to read
✓ Looks clean
-->
{{#if isNotActive}}
item is not active
{{/if}}
</template>
Template.mytemplate.helpers({
isNotActive: function() {
return ! Session.get('isActive');
}
});
执行 NOT 运算的最佳方法是什么?
例如,这里是 javascript
Template.mytemplate.helper({
myhelper : function(){
return true;
}
});
现在如何在模板中获取 !myhelper?我一直在做
<template name="mytemplate">
{{#if myhelper}}
{{else}}
myhelper is false
{{/if}}
</template>
但一定有更好的方法。我可以创建一个平等助手并执行 {{#if equal myhelper true}}
但这看起来不太干净。
有没有人有更好的主意?
我认为您应该可以使用 #unless
空格键来获得您想要的内容:
<template name="mytemplate">
{{#unless myhelper}}
myhelper is false
{{/unless}}
</template>
我建议只在您的助手名称中使用 isNot
。
比较:
<template name="mytemplate">
<!--
✓ Easy to read
– Looks not clean
-->
{{#if isActive}}
{{else}}
item is not active
{{/if}}
<!--
- Not very easy to read
✓ Looks clean
-->
{{#unless isActive}}
item is not active
{{/unless}}
</template>
Template.mytemplate.helpers({
isActive: function() {
return Session.get('isActive');
},
});
对比:
<template name="mytemplate">
<!--
✓ Easy to read
✓ Looks clean
-->
{{#if isNotActive}}
item is not active
{{/if}}
</template>
Template.mytemplate.helpers({
isNotActive: function() {
return ! Session.get('isActive');
}
});