Conditional Routes in HTML

I want to change following HTMl code

<%= link to: Routes.job_opening_path(@conn, :index), class: "nav-link" do %>
....other div element
<% end %>

to

<%= link to: <%= if check_workspace_activation() , do: Routes.job_opening_path(@conn, :index), else: "#" %>, class: ""  do %>
...other div element
<% end %>

When I use last HTML code, I am getting following error

syntax error before: '<'

What I am missing here ? Your help will be appreciateed

As if/2 is an expression, you should be able to do something like this:

<%= link to: if check_workspace_activation() do Routes.job_opening(@conn, :index) else "#" end, class: "" do %>
  <!-- more stuff -->
<% end %>

Following I wrote

 <%= link to: if check_workspace_activation() do Routes.job_opening(@conn, :index) else "#" end, class: "nav-link" do %>

Still, getting following error

syntax error before: ','

Then we probably need the ugly parenthesized version of the if/2, but I can’t test right now, as I have no project in reach that would have EEx:

<%= link to: if(check_workspace_activation(), do: Routes.job_opening(@conn, :index), else: "#"), class: "" do %>
  <!-- … -->
<% end %>

But perhaps using a helper is the better strategy?

1 Like

Could you explain more, this new strategy you are talking about?

The strategy is to put parenthesis around if…

By the way, You could also write a helper function inside the view.

1 Like

This is exactly the strategy I’m talking about.

1 Like

Yes, view files should contain helper functions in order to simplify templates…

2 Likes