0

I have the following in an html.erb file, using link_to to get a hyperlink and t() to internationalize my text. But it looks very clunky:

<p><%= t('session.new_user') %><%= link_to(t('session.signup_now'), signup_path) %></p>

Splitting onto multiple lines seems wrong since the text will all appear on the same line on screen but is there a better syntax to avoid the two consecutive <%= %> blocks?

Stefan
  • 109,145
  • 14
  • 143
  • 218
Papadeltasierra
  • 233
  • 1
  • 9

2 Answers2

1

I would probably go for line breaks:

<p>
  <%= t('session.new_user') %>
  <%= link_to t('session.signup_now'), signup_path %>
</p>

or you could set variables before the actual code

<% new_user_text = t('session.new_user') %>
<% link = link_to t('session.signup_now'), signup_path %>
<p><%= new_user_text %><%= link %></p>

or you could set instance variables in the controller. I wouldn't like that for view stuff like this.

Extra: if you like tidy code you may like haml

%p
  = t('session.new_user')
  = link_to t('session.signup_now'), signup_path

now it is actualle readable!

froderik
  • 4,642
  • 3
  • 33
  • 43
0

You can add a hyphen before the closing tag to prevent a newline being appended to the output.

<% ... -%>

Please note that this feature is Rails specific.

lorefnon
  • 12,875
  • 6
  • 61
  • 93