I've noticed that all of the rails APIs seem to use either of the following two notations for code nuggets in erb files;
<%Q ... %>
<%= ... %>
But seldom any mention of the vanilla
<% %>
for example, here;
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html
The book I've just read didn't cover the <%Q notation, so I'm assuming it's new. Is there any significance to the "Q" or is this just best practice now instead of just using <% ?
<% %> and <%= %> are features of erb. <% %> is used to embed some code in a template e.g. it would be used to embed an if statement
<% if #person.age > 18 %>
<%= %> is used to output a value within the template e.g
Name: <%= #person.name %>
%Q is a feature of the Ruby language and is not a Rails specific thing. As johnernaut writes it is used for double-quoted strings. e.g. instead of puts "Say \"Hello\"" where you need to escape the double-quotes you can write puts %Q(Say "Hello")
However the %Q you're seeing in some of the Rails documentation don't seem to be a use of this Ruby feature. If you look at the actual source of form_helper.rb it doesn't have them so I'm thinking it's a quirk of the rdoc documentation tool.
Update: As Frederick as posted whilst I was writing the above, it looks like it is an rdoc bug.
<% %> and <%= %> seem to be the common standard. I believe %Q is used for double quoted strings:
>> %Q(Joe said: "Frank said: "#{what_frank_said}"")
=> "Joe said: "Frank said: "Hello!"""
Found via relevant link.
The <%Q in the docs is the result of a bug in the rdoc tools being used, see this thread
Related
Rails 7 / Ruby 3
I'm currently working on a site that requires code examples to be displayed on a page - I can get these to display utilising the extra % character trick, however, for some of the examples I need to have a variable within them that is resolved (e.g. like the users' own API key etc...).
Consider I have #variable = "Resolved Variable"
<%%= link_to #variable, variable_path %>
Outputs on the page explicitly as
<%= link_to #variable, variable_path %>
But I really need the #variable to resolve and show on the page as:
<%= link_to "Resolved Variable", variable_path %>
I've tried all kinds of escaping the variable, but it seems that <%%= ensures that nothing following it can be resolved.
Any ideas?
Any text you haven't html_encodeed will be displayed as plain text.
My suggestion to you is to create a interpolated string that you could use to generate your intended result. For example:
output_text = "<%= link_to '#{#variable}', variable_path %>"
And, not sure if this is what you're looking for, but you can get a good UI by adding some Javascript library to format you code in the language in intend (in this case Ruby, it seems).
In case that's interesting to you, check the Prism lib, or check how to add it to your project here
I hope this helps.
With kind regards,
Rogerio
<%% in ERB will simply output <%, no more, no less. In particular, it won't attempt to parse the code after <%% as Ruby. However, this doesn't mean that you can't have another <%= ... %> after <%%:
require 'erb'
template = <<-EOD
<%%= link_to <%= #variable.inspect %>, variable_path %>
EOD
#variable = "Resolved Variable"
puts ERB.new(template).result
The inspect method will add quotes around your string and also escape certain characters as needed.
Output:
<%= link_to "Resolved Variable", variable_path %>
Sometimes it's more convenient to print in <%%>. How to do it in Rails?
http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-concat
Should be what you are looking for.
E.g. the following statement using concat:
<% concat "Output" %>
is equivalent to:
<%= "Output" %>
In ERB:
The <% %> signify that there is Ruby code here to be interpreted.
The <%= %> says output the ruby code, ie display/print the result.
So it seems you need to use the extra = sign if you want to output in a standard ERB file.
Otherwise, you could look at alternatives to ERB which require less syntax,.. maybe try something like HAML. http://haml-lang.com/tutorial.html
Example:
# ERB
<strong><%= item.title %></strong>
# HAML
%strong= item.title
Is that more convenient?
erb has two method to evaluate inline ruby expressions. The <% which evaluates the expression and the <%= which evaluates and prints. There is no global object to print to within the binding context.
As mentioned by Omar, there is a concat method, which is part of ActionView. This will do what you want.
Unlike a scripting language escape, there is no default output for erb. Since erb is simply a function, and given a template and binding will return a variable, it returns the values of text and functions recursively.
There is hot debate as to how much logic should be allowed in a view, but as little as possible is what most people aim for. If you are putting more code than text in the view, you may want to consider refactoring your code.
According to the Rails API,
If you need to find out whether a certain local variable has been assigned a value in a
particular render call, you need to use the following pattern:
<% if local_assigns.has_key? :headline %>
Headline: <%= headline %>
<% end %>
Testing using defined? headline will not work. This is an implementation restriction.
But when I tested it in my Rails projects, the defined? test worked just fine.
Could anyone explain, what exactly the restriction is and when it gets triggered?
I just took a look at http://ruby-doc.org/ruby-1.9/classes/ERB.html as well as http://ruby-doc.org/ruby-1.8/classes/ERB.html. I saw that the following is supported both in 1.8 and 1.9.
% a line of Ruby code
But after a tried it in a line of
% end ### changed from <% end %>
the browser simply shows % end in plain... Wondering what's the problem here?
(updated) another question, it seem when comment like #blabla appears in <%= %>, rails will get an error, any idea?
my code for another question:
<%= #page_title || 'Pragmatic Bookshelf' #magic #page_title; a if a is true, else b%>
Thanks
This is a comment in ERB:
<%# Where is pancakes house? %>
whereas this is an error:
<%= # I'll cook you some eggs, Margie. %>
You cannot combine a comment and the <%= %> syntax.
In the documentation you linked to, you might notice the optional -- see ERB.new note in here:
% a line of Ruby code -- treated as <% line %> (optional -- see ERB.new)
And then, ERB.new has this to say:
If *trim_mode* is passed a String containing one or more of the following modifiers, ERB will adjust its code generation as listed:
% enables Ruby code processing for lines beginning with %
So you probably don't have a *trim_mode* in your ERB.new options.
If *trim_mode* is passed a String containing one or more of the following modifiers, ERB will adjust its code generation as listed:
% enables Ruby code processing for lines beginning with %
Sometimes it's more convenient to print in <%%>. How to do it in Rails?
http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-concat
Should be what you are looking for.
E.g. the following statement using concat:
<% concat "Output" %>
is equivalent to:
<%= "Output" %>
In ERB:
The <% %> signify that there is Ruby code here to be interpreted.
The <%= %> says output the ruby code, ie display/print the result.
So it seems you need to use the extra = sign if you want to output in a standard ERB file.
Otherwise, you could look at alternatives to ERB which require less syntax,.. maybe try something like HAML. http://haml-lang.com/tutorial.html
Example:
# ERB
<strong><%= item.title %></strong>
# HAML
%strong= item.title
Is that more convenient?
erb has two method to evaluate inline ruby expressions. The <% which evaluates the expression and the <%= which evaluates and prints. There is no global object to print to within the binding context.
As mentioned by Omar, there is a concat method, which is part of ActionView. This will do what you want.
Unlike a scripting language escape, there is no default output for erb. Since erb is simply a function, and given a template and binding will return a variable, it returns the values of text and functions recursively.
There is hot debate as to how much logic should be allowed in a view, but as little as possible is what most people aim for. If you are putting more code than text in the view, you may want to consider refactoring your code.