Boy
Boy

Reputation: 612

how to use in_array in ejs template

How to use in_array or similar in inside an ejs template?

This is how I use it on php:

<?php
$cur_repository_id = array();
foreach($dbrepos as $row){
    $shared_ids = $row['shared_ids'];
    if(in_array($user_id, $shared_ids)){
        $selected = "selected";
        $cur_repository_id[] = $row['rep_id'];
    }else{
        $selected = "";
    }
?>
<option value="<?php echo $row['rep_id'];?>" <?php echo $selected;?>><?php echo $row['namespace'];?></option>
<?php
}
?>

Upvotes: 1

Views: 2718

Answers (1)

Marina
Marina

Reputation: 794

Equivalent of in_array in javascript is includes

So if for example you want to pass an array from Node.js to ejs file, you can do something like that:

res.render('index', {numbers : [1, 23, 44]});

And then inside ejs you can use the method includes, for example:

<body>
    <% if(numbers.includes(44)){ %>
        <h1>44 exists</h1>
    <% } else{ %>
        <h1>44 not exists</h1>
    <% } %>
</body>

Also you can use method indexOf, which return the index of given value in an array.

Upvotes: 2

Related Questions