1

I'm trying to display 'n/a' instead of NaN in a popup. Something like:

    {% if value == NaN %} 
        n/a
    {% endif %}

I realize I can always catch it earlier on before the template is rendered but
Is there was a way to check for NaN values in the template?

Don
  • 3,876
  • 10
  • 47
  • 76
  • I suspect the NaN value may be rendered by the template itself from a null and/or undefined value but a conditional checking for null doesn't catch it. – Don May 23 '17 at 02:11

1 Answers1

1

Here is the logic for a custom filter since there is not a built in filter to check for NaN:

nunjucks.configure().addFilter('nan', function(num) {
     if (isNaN(num)){
        return 'n/a';
     }
     return num;
});

Then the usage is the same as for any filter:

{{ num | nan }}
Don
  • 3,876
  • 10
  • 47
  • 76