10

I am creating an application in Node Js using Nunjucks template engine and I have to apply permissions on pages to show add, edit and delete links.

For this I have implemented an array of permissions like below :

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

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

in_array('add_user', user_params)

But I am being able to perform this task in nunjucks. So can anyone help me out ?

Thanks in advance

Ankit
  • 627
  • 1
  • 9
  • 22

2 Answers2

11

You should be able to do this:

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

For indexOf, I'm not sure that works but even if it did, if zero evaluates to false that's not good either if you're testing the first row. would also need to check > -1

WakeskaterX
  • 1,408
  • 9
  • 21
4

The only way I found is looping through the array like so:

{% for param in user_params %}
    {% if param==='add_user' %}
        do stuff in html
    {% endif %}
{% endfor %}

Ugly and full of holes, but will fit most use cases.

You're probably better off making a custom filter

Sjeiti
  • 2,468
  • 1
  • 31
  • 33