Kentico IF else if else statement

Dominic Carvalho asked on August 12, 2018 11:32

Hey, I am struggling to get a simple else if statement to work, What is the correct structure for the statement ? It works with a normal if else statement but when I try add another if in it, it breaks. I am currently trying it like this:

{% if(menuName == "{$copyright$}") { %}

  • <a href="/terms-conditions#copyright" {%gtmIdFooter%}>{%menuName%}
  • {% } else if(menuName == "{$affiliates$}") { %}
  • <a href="different like" {%gtmIdFooter%}>{%menuName%}
  • {% } else { %}
  • <a href="{% GetDocumentUrl %}" {%gtmIdFooter%}>{%menuName%}
  • {% } %}

    Any help will be greatly appreciated :)

    Correct Answer

    Peter Mogilnitski answered on August 12, 2018 14:53

    You have anchors not closed and assuming your gtmIdFooter and menuName are defined somewhere, it should be:

    {% 
    result = "<a href='" + GetDocumentUrl() + "' " + gtmIdFooter + ">" + menuName + "</a>"
    
    if(menuName == GetResourceString("copyright")) { result = "<a href='/terms-conditions#copyright' " + gtmIdFooter+ ">" + menuName + "</a>" }
    if(menuName == GetResourceString("affiliates")) { result = "<a href='different like' " + gtmIdFooter+ ">" + menuName + "</a>" }
    
    return result;
    %}
    

    Or add lmbda to improve readability:

    {% 
    makeUrl = ((url, gtm, name) =>  "<a href='" + url + "' " + gtm + ">" + name + "</a>"); 
    result = makeUrl(GetDocumentUrl(), gtmIdFooter, menuName);
    
    if(menuName == GetResourceString("copyright")) { result = makeUrl("/terms-conditions#copyright", gtmIdFooter, menuName) }
    if(menuName == GetResourceString("affiliates")) { result = makeUrl("someurl", gtmIdFooter, menuName) }
    
    return result;
    #%}
    
    0 votesVote for this answer Unmark Correct answer

       Please, sign in to be able to submit a new answer.