2014-03-28 26 views
16

我在到處定義模板助手,簡單地測試文檔屬性的平等與恆定的,所以我可以做這樣的事情在我的模板:流星:在Blaze中測試兩個值的相等性的最佳方式是什麼(例如{{#if someVar =='someVal'}})?

{{#if fruitIsPineapple}}...{{/if}} 

而且在我的模板助手的樣子:

Template.example.helpers({ 
     fruitIsPineapple: function() { return this.document.fruit === 'pineapple'; } 
    }); 

我該如何從創建所有這些幫助者中拯救自己?如果我們在Blaze擁有一個平等運營商,那就太好了......

+0

定義了很多方便的助手,其中包括一個用來測試平等包:https://github.com/raix/Meteor-handlebar-helpers –

+0

爲什麼有人對此表示讚賞嗎?我有同樣的問題,答案沒有記錄在任何地方。 – foobarbecue

回答

44

我在Meteor Devshop回答了我的問題。事實證明,你可以定義一個把手幫手,就像這樣:

Template.registerHelper('equals', function (a, b) { 
     return a === b; 
    }); 

然後用它,在這樣的前綴位置:

{{#if equals fruit 'pineapple'}}...{{/if}} 
+0

更多信息:https://github.com/meteor/meteor/wiki/Using-Blaze –

+0

現在不一定是'UI.registerHelper'嗎? –

+0

是的,@ChristianFritz。謝謝。 –

15

無需任何繁瑣的代碼,您可以通過安裝raix:handlebar-helpers實現這一點,並做一些事情像這樣:

{{#if $eq a b}} 
    ... 
{{ /if }} 
相關問題