mozilla / nunjucks

A powerful templating engine with inheritance, asynchronous control, and more (jinja2 inspired)
https://mozilla.github.io/nunjucks/
BSD 2-Clause "Simplified" License
8.48k stars 635 forks source link

Is there any way to detect data type of value? #1392

Open shivangipathak312 opened 2 years ago

shivangipathak312 commented 2 years ago

I have a requirements to use "float" and perform some calculation. But When input is alphanumeric value it is throwing an error. So I want to put some check, like If value is numeric than only perform calculation.

Can anyone help me with this using nunjuck ?

ogonkov commented 2 years ago

You can add custom test

environment.addTest('float', (value) => Number.isInteger(value) && value % 1 !== 0);

And use it like

{% if value is float %}