How to compare two strings in template of Meteor Blaze

Posted By : Parveen Kumar Yadav | 27-Dec-2016
Some time we need to compare equality of two string value for hiding and showing some attribute or doing any conditionally work like if statement.So normally you check that value very easily in angular.js and other front-end technologies. But in blaze you can't simply compare two string by only equals operator . You need to create a helper by the help of that helper you can achieve comparison on two string in template html. For example we have a template i.e test.html on which we want to show the button if the value of our session is equals to string i.e 'PACKED'.

Here you can use something like this:-
 
{{#if data == 'PACKED'}}
But this is not going to work because blaze template work only on Boolean true false we can't compare directly two string. So we need to do something like this:-
 
{{#if equals data 'PACKED'}}
In the helper section you need to create a helper i.e:-
 Template.test.helpers({
    equals: function(a, b) {
        return a == b;
    },
    data: function() {
        var paramsStatus = Router.current().params._status;
        return paramsStatus;
    }
})
 
You need to create a helper that is equals which take two argument and check equality if both are equal than it will return true. And in our template gets value of true and false accordingly and our if condition running perfectly.

Hope it will help Thanks!

About Author

Author Image
Parveen Kumar Yadav

Parveen is an experienced Java Developer working on Java, J2EE, Spring, Hibernate, Grails ,Node.js,Meteor,Blaze, Neo4j, MongoDB, Wowza Streaming Server,FFMPEG,Video transcoding,Amazon web services, AngularJs, javascript. He likes to learn new technologies

Request for Proposal

Name is required

Comment is required

Sending message..