How to substitute macro value into the link helper text

Greetings,

I would like to generate a link with the link helper that includes a value from the current connection. The result would show “Sign Out nickname” where nickname is resolved from the assign fields. I’m using Pow library, but I think it’s a general question. In Pow, I have access to the current user as @current_user and can find the nickname as @current_user.nickname. I’ve tried just using the macro directly in the link text like this:

<span class="menu-link"><%= link "Sign Out @current_user.nickname"), to: Routes.pow_session_path(@conn, :delete), method: :delete %></span>

And also with some embedded evaluation, which has a syntax error.

<span class="menu-link"><%= link EEx.eval_string("Sign Out <%= @current_user %>"), to: Routes.pow_session_path(@conn, :delete), method: :delete %></span>

Can someone kindly put me on the right track to evaluating such expressions? Thanks!

Hello and welcome,

You can interpolate like this…

"Sign Out #{@current_user.nickname}"

Be careful here because there is a wrong parens

<span class="menu-link"><%= link "Sign Out @current_user.nickname"), to: Routes.pow_session_path(@conn, :delete), 
method: :delete %></span>

# It should be

<span class="menu-link">
<%= link "Sign Out #{@current_user.nickname}", to: Routes.pow_session_path(@conn, :delete), method: :delete %>
</span>
1 Like

Thank you so much! That worked perfectly; and I apologize - I searched for lots of ways but didn’t try “interpolate”. Ha, it reminds me of Velocity Template Language with the # substitution.