How to write a nested IF then ELSE statement in my template

Kim Tompkins asked on May 4, 2021 01:03

I am using this statement inside a Static Text webpart. it works if I don't include the outer IF. It doesn't render anything if I include it. Please help with syntax. thank you

 {% if(Section2Header.lenth>0){ %} 
  {% if(Section1HeadingType == 1){ %}

     <div class="row purple-bg">
     <div class="col-md-1">&nbsp;</div>

    <div class="col-md-10 mx-auto" style="max-width: 1200px;">
      <p class="white-text my-2 {% Section1HeadingAlignment %}" ><strong>{% Section1Header %}</strong></p>
     </div>

     <div class="col-md-1">&nbsp;</div>
   </div>
 {% } else { %}
    <div class="col-md-12 mx-auto" style="max-width: 1200px;">
      <h1 class="h5 {% Section1HeadingAlignment %}" >{% Section1Header #%}</h1>
    </div>

{% }#%} {% }#%}

Correct Answer

Juraj Ondrus answered on May 4, 2021 06:10

I would rather recommend considering writing a custom macro which will have the logic written in C# and then just output desired HTML. It might be cleaner solution and also better for performance.
I would also try not to use the open condition and use it just like nested loops and conditions:
{%if (condition A) {if (condition B) {...}} else {...}%}

0 votesVote for this answer Unmark Correct answer

Recent Answers


Kim Tompkins answered on May 4, 2021 17:44

Thank you. Custom macros are a little out of my realm at the moment. But I am very interested in learning how to write and use them.

0 votesVote for this answer Mark as a Correct answer

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