Search code examples
javascriptxmlif-statementconditional-statementsblogger

How to break statement in a if statement in Blogger where both IF and ELSE are true?


I am changing some labels (tags) in my blog so that each post may have, at least, 2 labels.

The idea is that if the post have the LABEL 1, it loads a script; else, loads another one. So far, so good, but in the posts labeled with LABEL 1, both conditions are satisfied and the two scripts are loaded and I'll explain why.

An important point (that I know it would fix my problem, but I can't change) is that posts labeled with LABEL 1 must have another label (in addition to LABEL 1), that's why both conditions are satisfied (the if and the else).

In this case, is there any way to break the execution if the first condition is satisfied? i.e. (the post has LABEL 1 = true)

I tried the break, but it looks like that it doesn't work because I don't have a loop, but a conditional statement.

<b:loop values='data:post.labels' var='label'>
<b:if cond='(data:post.labels where (label => label.name in [LABEL 1"])).length gt 0'>
// WHAT TO DO IF THE POST HAS "LABEL 1"
// I WISH THE CODE BREAK HERE EVEN IF A POST WITH "LABEL 1" HAVE ANOTHER LABEL.

<b:else/>
// WHAT TO DO IF THE POST DOES NOT HAVE "LABEL 1"

</b:if>
</b:loop>

Solution

  • Try this code without using b:loop

    <b:if cond='data:post.labels any (label => label.name == "label 1")'>
     <!-- do something -->
    <b:else/>
     <!-- the post does not have "label 1" -->
    </b:if>