If true false conditions in Templates


#1

Inside the template, I have

{{#if validation}}
   <span> First </span>
{{else}}
   <span> Second </span>
{{/if}}

where validation is the boolean value false
Why is it showing “First” instead of “Second” even though the value of validation is false?

Thanks.


#2

It shouldn’t display first, something seems to be wrong with your helper return value. Can you try to display it just to check that it is indeed set to false?


#3

@MazDev Thanks.
You are right. I guess the problem is with the helper itself.
Its not updating reactively. Any suggestions.


#4

What’s the helper function look like? Hard to diagnose something no one can see :slight_smile:


#5

Hi @nkrisc Thanks. The helper looks like this:

Template.relatedBlogsTemplate.helpers({
     validation: function() {
            var setting = BlogSettings.findOne({});
            if (setting) {
                return setting['validationStatus'];
            }
}

First I am getting the BlogSettings document. It will return the document related to the userId. So, only one document.

I am saving that document in setting. Then I am getting the validationStatus which is a boolean value.


#6

I recommend to have publications and subscriptions to handle it. Have a look at this repo to see how to achieve it in a reactive way.


#7

You need to use Tracker which is included in core.

Your conditional is not reactive, so you need to wrap it with tracker to make it react to the BlogSettings changes.

Template.relatedBlogsTemplate.helpers({
     validation: function() {
            var setting = BlogSettings.findOne({});
            Tracker.autorun(function () {
                if (setting) {
                    return setting['validationStatus'];
                }
           });
}

I haven’t tested it but it should run ok.


#8

No, now will the validation helper never return a value. And you never use a reactive variable in your autorun, so it will only run once. And helpers are reactive, so there’s no need to use Tracker.autorun at all.


#9

It seems you’re right, my bad.

Do you think something like this would do the trick then?

Template.relatedBlogsTemplate.helpers({
     validation: function() {
          return BlogSettings.findOne({});
}

#10

BlogSettings.findOne({}) is a reactive data source, so your helper should rerun everytime when any document is added/changed/removed with a collection. It should works. Are you shure you have any documents in a BlogSettings collection? Or founded document has a validationStatus field?


#11

Yes I have the documents. When I directly use

return BlogSettings.findOne({})['validationStatus'];

and when I open developer console, I see error message: cannot call validationStatus of undefined first but the template works.

Actually, the helper is being called multiple times even though there is only one template which is calling it.


#12

This is the way I would do it, you can use Fields to read the changes just what you need. if still fails sure the error is not in the helper

Template.relatedBlogsTemplate.helpers({
	validation: function () {
		var setting = BlogSettings.findOne({}, {fields: {validationStatus: 1}});
		return setting && setting.validationStatus;
	}
});

#13

Thats right! Blaze call your helper every time when any of reactive source is triggered. I gues that when your template is rendering for the first time there is no documents in a collection.
This one could be broken

return BlogSettings.findOne({})['validationStatus'];

So your first version of helper should works by default. You could set a breakpoint in the helper function and dig the data from a collection.


#14

@mrzafod @cottz

Thanks. The helper is reactive now and it is working. The problem is with the if else condition inside the template.

I dont understand why but the false condition is working and not true condition.


#15

Ok, I figured out the problem. The format of true or false in database is in String format and not in Boolean. This caused the problem till now.