Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phoenix templates if conditional

Im migrating from rails, and expected this to work, in my template?

      <% if true do %>
        <li><p>hello</p></li>
      <% else %>
        <li><p>world</p></li>
      <% end %>

None of the condition seem to get rendered. How to acheive this?

Thank you

like image 522
coderVishal Avatar asked Apr 29 '16 07:04

coderVishal


1 Answers

You need to use <%= instead of <%

<%= if true do %>
...
<% end %>

From the EEx docs:

All expressions that output something to the template must use the equals sign (=). Since everything in Elixir is an expression, there are no exceptions for this rule. For example, while some template languages would special-case if clauses, they are treated the same in EEx and also require = in order to have their result printed:

like image 119
Gazler Avatar answered Oct 14 '22 07:10

Gazler