Put IF-THEN statements in one line

I am trying to make my code more efficient.

I use EJS code.

What I have now…

<%if(post.alpha){%>
    <a target="_blank" href="<%- post.alpha %>">
    </a>
    <%}%>
        <%if(post.beta){%>
            <a target="_blank" href="<%- post.beta%>">
            </a>
            <%}%>
                <%if(post.gamma){%>
                    <a target="_blank" href="<%- post.gamma %>">
                    </a>
                    <%}%>

What the above code says is:
“If there is ALPHA then link points to ALPHA”.
“If there is BETA then link points to BETA”.

This works.

But to make this more clean and lean, I was thinking something like this…

<%if(post.alpha || post.beta || post.gamma){%>
    <a target="_blank" href="<%- post.alpha %>" || <%- post.beta %>" ||
        <%- post.gamma %>">
    </a>
    <%}%>

What the above is supposed to say is:
“If there is ALPHA or BETA or GAMMA, the link to ALPHA or BETA or GAMMA”.

I put this code block in the section within another IF-statement. So it will only find one variable (either ALPHA, or BETA or GAMMA), that works.
The only problem is in the HREF, there it does not work, because it has to select the ALPHA HREF when the IF statement finds ALPHA, and it has to select the BETA HREF when the IF statement finds BETA, etc etc.

Is my idea possible in some way? In my “idea” code block, I just need for the HREF to make ALPHA correspond to ALPHA, BETA to BETA etc.

What you’re looking for is a switch/select statement.

What does the object that represents a post look like? ie why do you need to do this - why can you not just render the post’s href without the logic?Just need a bit more context, because you should really avoid as much logic as possible in views

Thanks…

This is a new thing I hear of.

I get it more or less.

But how to write the expression in my case?

My cases are:
1: <a target="_blank" href="<%- post.alpha %>"> </a>
2: <a target="_blank" href="<%- post.beta %>"> </a>
3: <a target="_blank" href="<%- post.gamma %>"> </a>

To explain more…

post.alpha contains images like:
alpha.img1
alpha.img2
alpha.img3

post.beta contains images like:
beta.img1
beta.img2
beta.img3

Alpha is a post category about hotels for example.
Beta is a post category about cars for example.

But sometimes there is no beta.img2 or beta.img3, hence the IF statements.

Ofcourse, I could name all images just “img”, but then they are not tied to a category anymore.

So in my example… post.beta actually means post.beta.img1, etc.

Hi Dan… you are right; i make it too complicated, I will adjust my naming convention and forget this logic. Thanks for your help.

Topic can be closed. Sorry to confuse everybody.