Check if key exists in array in nunjucks template (Node JS)

I am creating an application in Node Js using the Nunjucks templating engine and I have to apply permissions on the pages to show links, edit and remove links.

For this, I have implemented an array of permissions as shown below:

var user_params = ['add_user', 'edit_user', 'delete_user'];

      

Now I want to check in the pages that add_user exists in the user_params array or not like we do in php

in_array('add_user', user_params)

      

But I can accomplish this task in nunjucks. So can anyone help me?

Thank you in advance

+3


source to share


1 answer


You should be able to do this:

{% if 'add_user' in user_params %}
   do stuff in html
{% endif %}

      



For indexOf I'm not sure what works, but even if it did, if zero evaluates to false, which is not very good if you are testing the first line. also would need to check> -1

+1


source







All Articles