Search code examples
node.jshandlebars.jsejs

How to convert handlebars to ejs


This is probably an easy question but I can't figure it out.

I want to change this handlebar code into ejs

  {{#if hasErrors}}
  <div class="alert alert-danger">
  {{#each messages}}
                <p>{{ this }}</p>
            {{/each}}
        </div>
    {{/if}}

I tried but i'm getting syntax error

 <%= if (Errors) {%>
 <div class="alert alert-danger">
 <%= each messages %>
 <p><%= this%></p>
 </div>
 <% }%>

Solution

  • If your "hasError" variable is of type boolean, and your messages variable is an array of message strings;

    <% if (hasErrors) {%>
     <div class="alert alert-danger">
         <% messages.forEach(function(message){ %>
           <p><%= message %></p>
         <% });%>
     </div>
    <% }%>