All notes
Erb

Intro

startEllis.name: erb.

Command line



# See documentation for ERB
ri ERB

# -r: require.
erb -r abbrev -r ipaddr my-template.txt.erb  > new-file.txt

Difference between code block

SO.



#----------
<%= %>
# Evaluate enclosed code (an expression), and print the returned value into erb file.

#----------
<% %>
# Execute the ruby code (called scriptlet) within the brackets, but output nothing.
Hello, <%= @name %>.
Today is <%= Time.now.strftime('%A') %>.
<% for @item in @shopping_list %>
  <%= @item %>
<% end %>

#----------
<% -%>
# By default, a newline character is added to the page after the position of each tag.
# This avoids line break after expression. Extended by Rails.

<% for @item in @items -%>
  <%= @item %>
<% end -%>

#----------
<%# %>
# Comments out code within brackets; not sent to client
<%# This is just a comment %>

#---------- Transformation
# To use these features your code must include the module ERB::Util.

# HTML escaped:
<%= h(this & that) %>
# This will be JSON encoded:
<%= j(this & that) %>
# This will be converted to Textile markup:
<%= t(this & that) %>
# This will be URL encoded:
<%= u(this & that) %>

Escape quotes

SO: dealing with quotes in HTML erb.



# "j" or "escape_javascript" will escape quotes in a string and make it possible to add a string with quotes to an attribute on an element.
# "h" is short for html_escape.

<img src="<%=image%>" data-description="<%=h j(auto_link(step.description))%>"/>

How to debug



<%
is_myapp = false
p node # We can print variable here to stdout. Good for debug.
if (File.symlink?('/var/log/myapp') && File.readlink('/var/log/myapp') == node['my_log_dir'])
  is_myapp = true
end

syslog_dir = "/var/log"
if is_myapp
  syslog_dir += "/myapp"
end
%>