Conditional statement "except" in EJS code

I want to show a separate layout for specific page.tag.

My isList contains unique tags namely: aaaaa, bbbbb, ccccc. These tags are also in the “normal” tags.

With below code, both layouts one and two show for above unique tags.

What I want to write is something like: “Show layout/two only for ALL those tags that are NOT included in isList”. How to write?

<% if (isList(page.tag)) { %>
<%- partial('_layout/one') %>
<% } %>
<% if (page.tag) { %>
<%- partial('_layout/two') %>
<% } %>

Assuming from the description isList is an array of IDs

<% if (isList.includes(page.tag)) { %>
  // layout 1
<% } else { %>
  // layout 2
<% } %>

@DanCouper - Thanks, Dan. Unfortunately it throws me an error saying “isList.includes is not a function”.

I have to edit my problem a bit.
//layout-1 and //layout-2 are actually the same.
What happens now is that the layout shows TWICE for my “normal” tags and also when I have only “isList” tags. This is because my “isList” tags are also included in my “normal” tags.
So I basically need to show the layout only ONCE when it concerns “normal” tags or when it concerns “isList” tags.
Sorry for the confusion.

What is isList? In your code, it’s a function. Do you have, in your code, accessible, a list of all of the pages that have that tag? Or even easier, just a list of tags for the current page:

<% if (pageTags.includes('isList')) { %>
// Do isList stuff
<% } else { %>
// Don't do isList stiff
<% } %>

At the minute, you’re also not doing an else, so there’s no switching going on: it’ll always render twice because that’s what you’re telling it to do: if there’s an isList tag, render the layout. If there’s a tag (any tag), render the layout.

@DanCouper - Unfortunately I do not have a list of pages with those unique tags.

I understand your solution, it would be perfect… hmmm… not sure what to do now…

Surely you.know what the tags for the page are, otherwise you can’t do anything, so second solution (put in a list and if (pageTags.includes('isList')) then render

@DanCouper - OK, cool, I try that. Thank you very much for your help! :slightly_smiling_face:

1 Like