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 %
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 %>
Forgive the vague title, I'm having a hard time figuring out the correct phrase for what I am trying to do.
I have a number of custom snippets I have written to support writing ERB for Ruby on Rails. For this example, I am trying to use an if tag that can be on a single line:
<% if something %>Content Here<% end %>
or multiple lines with indented content:
<% if something %>
Content Here
<% end %>
I am able to get the first (single line) format working fine with the following snippet:
<![CDATA[<% if $1 %>${2:$SELECTION}<% end %>$0]]>
The problem is that if I type in the trigger, hit tab, type in the conditional for $1, hit tab, then hit return, Sublime does exactly what I type and puts the following:
<% if something %>
[cursor]<% end %>
Is there a way in the snippet to instead treat the if and end tags like HTML tags:
<% if something %>
[cursor]
<% end %>
Thanks!
The easiest way is to define a new snippet with a new tabTrigger. It will look something like:
<snippet>
<content><![CDATA[<% if $1 %>
${2:$SELECTION}
<% end %>$0]]>
</content>
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.
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
I'm using a js.erb template to render some jQuery. When editing an html.erb file in TextMate, I frequently use the convenient key combo, ctrl+>, to create and then toggle the following tags:
<%= %>
<% %>
<%- -%>
<%# %>
This shortcut doesn't work by default when editing js.erb files. In the Bundle Editor, I found a snippet called "Insert ERb’s <% .. %> or <%= .. %>" under "Ruby". By adding "source.js" to the scope selector I was able to get insertion to work, but when I pressed the key combo multiple times, instead of toggling the tag I got a tag inside of a tag like this:
<%= <%= %> %>
I've tried changing the scope of the command called "Toggle ERb Tags" but I can't seem to get toggling to work. Any suggestions?
Update November 19, 2010:
This is no longer a problem in the new version of Textmate that came out this week: 1.5.10 (1623).
One possible reasono why this is the case is that the snippet that generates the angle brackets for you is defined thus:
<%= $0 %>
This puts this text into your source after the tab-trigger occurs. The $0 is a placeholder for the cursor; it's final resting place after the snippet is completed. Since the cursor rests in the middle and this is a simple snippet, repeatedly performing the tab-trigger will nest these brackets.
To achieve what you want, you have to do it in a script. You can use any scripting language as long as you appropriately specify the shebang line. I am not a proficient scripter so I'll try to solve this using pseudocode.
if selected_text
if no_wrapping_angle_brackets
surround_with_angle_brackets
else
strip_angle_brackets
else
if no_wrapping_angle_brackets
surround_with_angle_brackets
else
strip_angle_brackets
It's not much but I hope this helps
This was fixed with Textmate update 1.5.10 (1623).
I just ran into this problem too, even with updated TextMate and bundles. I fixed it by adding source.js.rails to the scope selector of the snippet "Insert ERb’s <% .. %> or <%= .. %>". Make sure you don't change the scope selector for the similar command "Toggle ERb Tags". This inserts the ERb tags correctly and also toggles them as expected.
Your Ruby on Rails Textmate bundle may be outdated due to changes in Ruby 1.9.
Update your tmbundle and this problem should go away.