Question: ejs - looking to add if condition in EJS template

Question

ejs - looking to add if condition in EJS template

Answers 1
Added at 2016-12-23 08:12
Tags
Question

Hi I am just start to use EJS. Now I need to check condition so I use if condition in EJS, but while running it shows error. The code I am trying is

<script id="insidecontentdata" type="text/x-ejs-template">
  <%for(var i=0;i < tooldata.section.length;i++){%>
    <% debugger; %>
      <div class="conductorHeading">
        <%= tooldata.section[i].tittle%>
      </div>
      <div class="conductorHeadingsub">setting :
        <%= tooldata.section[i].settingdepth%>
      </div>
      <div class="border" style="top:0px;">&nbsp;</div>
      <%if (<%= tooldata.section[i].x_over%>) {%>
        <div class="conductorHeadingsub">x-over:
          <%= tooldata.section[i].x_over%>
        </div>
        <%}%>
          <%}%>
</script>

I can print all div before if condition but the if condition is became wrong. What is the wrong in my code?

Answers
nr: #1 dodano: 2016-12-23 14:12

Inside your if condition you are using an expression <%= tooldata.section[i].x_over%> which is causing the problem.

You are already inside an scriptlet so just use normal JavaScript if condition

<script id="insidecontentdata" type="text/x-ejs-template">
  <%for(var i=0;i < tooldata.section.length;i++){%>
    <div class="conductorHeading">
      <%= tooldata.section[i].tittle%>
    </div>
    <div class="conductorHeadingsub">setting :
      <%= tooldata.section[i].settingdepth%>
    </div>
    <div class="border" style="top:0px;">&nbsp;</div>
      <%if (tooldata.section[i].x_over) {%>
        <div class="conductorHeadingsub">x-over:
          <%= tooldata.section[i].x_over%>
        </div>
      <%}%>
    </div>
  <%}%>
</script>
Source Show
◀ Wstecz