# ERB Template
# Displaying Data
<h1>
Hello <%= name %>!
</h1>
2
3
This <%= %> tag will be replaced by the templating engine by evaluating the Ruby code inside it.
It’s like string interpolation!
Notice the equals sign in <%= %>.
That tells ERB to render the contents of this tag
# Control Structures
If you want to write a loop or an if statement in ERB you want to leave out the equals sign so ERB doesn’t render things that you don’t need.
<% if @favorite_food == "chocolate" %>
Are you a chocolate lover? Here are some of our best PREMIUM chocolate bars!
<% else %>
Here are our top 10 snacks that people bought this month.
<% end %>
2
3
4
5
<% @books.each do |book| %>
<%= book.title %>
<%= book.author %>
<br>
<% end %>
2
3
4
5
# Structuring Layouts
Yield
Within the context of a layout,
yieldidentifies a section where content from the view should be inserted. The simplest way to use this is to have a singleyield, into which the entire contents of the view currently being rendered is inserted:<html> <head> </head> <body> <%= yield %> </body> </html>1
2
3
4
5
6
7You can also create a layout with multiple yielding regions:
<html> <head> <%= yield :head %> </head> <body> <%= yield %> </body> </html>1
2
3
4
5
6
7
8The main body of the view will always render into the unnamed
yield. To render content into a namedyield, you use thecontent_formethod.content_for
The
content_formethod allows you to insert content into a namedyieldblock in your layout. For example, this view would work with the layout that you just saw:<% content_for :head do %> <title>A simple page</title> <% end %> <p>Hello, Rails!</p>1
2
3
4
5Partials
Partial templates (partials) are a way of breaking the rendering process into more manageable chunks. Partials allow you to extract pieces of code from your templates to separate files and also reuse them throughout your templates.
To create a partial, create a new file that begins with an underscore:
_form.html.erbTo render a partial as part of a view, use the render method within the view:
<%= render "form" %>A partial can use its own layout file, just as a view can use a layout. For example, you might call a partial like this:
<%= render partial: "link_area", layout: "graybar" %>1This would look for a partial named
_link_area.html.erband render it using the layout_graybar.html.erb.You can also pass local variables into partials, making them even more powerful and flexible.
<h1>New zone</h1> <%= render partial: "form", locals: {zone: @zone} %>1
2<%= form_with model: zone do |form| %> <p> <b>Zone name</b><br> <%= form.text_field :name %> </p> <p> <%= form.submit %> </p> <% end %>1
2
3
4
5
6
7
8
9Object Partials
Objects that respond to
to_partial_pathcan also be rendered, as in<%= render @post %>. By default, for ActiveRecord models, this will be something likeposts/post, so by actually rendering@post, the fileviews/posts/_post.html.erbwill be rendered.A local named
postwill be automatically assigned. In the end,<%= render @post %>is a short hand for<%= render 'posts/post', post: @post %>.Collections of objects that respond to
to_partial_pathcan also be provided, such as<%= render @posts %>. Each item will be rendered consecutively.
# Linking Files
Linking Javascript Files
The
javascript_include_taghelper returns an HTMLscripttag for each source provided.You can specify a full path relative to the document root, or a URL, if you prefer. For example, to link to a JavaScript file that is inside a directory called
javascriptsinside of one ofapp/assets,lib/assetsorvendor/assets, you would do this:<%= javascript_include_tag "main" %>1Rails will then output a
scripttag such as this:<script src='/assets/main.js'></script>1To include multiple files such as
app/assets/javascripts/main.jsandapp/assets/javascripts/columns.jsat the same time:<%= javascript_include_tag "main", "columns" %>1To include
http://example.com/main.js:<%= javascript_include_tag "http://example.com/main.js" %>1Linking to Feeds
The
auto_discovery_link_taghelper builds HTML that most browsers and feed readers can use to detect the presence of RSS, Atom, or JSON feeds. It takes the type of the link (:rss,:atom, or:json), a hash of options that are passed through to url_for, and a hash of options for the tag:<%= auto_discovery_link_tag(:rss, {action: "feed"}, {title: "RSS Feed"}) %>1
2There are three tag options available for the
auto_discovery_link_tag::relspecifies therelvalue in the link. The default value is "alternate".:typespecifies an explicit MIME type. Rails will generate an appropriate MIME type automatically.:titlespecifies the title of the link. The default value is the uppercase:typevalue, for example, "ATOM" or "RSS".
Linking to CSS files
The
stylesheet_link_taghelper returns an HTML<link>tag for each source provided.You can specify a full path relative to the document root, or a URL. For example, to link to a stylesheet file that is inside a directory called
stylesheetsinside of one ofapp/assets,lib/assets, orvendor/assets, you would do this:<%= stylesheet_link_tag "main" %>1Linking to Image File
The
image_taghelper builds an HTML<img />tag to the specified file. By default, files are loaded frompublic/images<%= image_tag "header.png" %> <%= image_tag "icons/delete.gif" %> <%= image_tag "icons/delete.gif", {height: 45} %> <%= image_tag "home.gif", size: "50x20" %> <%= image_tag "home.gif", alt: "Go Home", id: "HomeImage", class: "nav_bar" %>1
2
3
4
5
6
7
8
9
10
11.
Linking Video File
The
video_taghelper builds an HTML5<video>tag to the specified file. By default, files are loaded frompublic/videos<%= video_tag "movie.ogg" %>1.
Like an
image_tagyou can supply a path, either absolute, or relative to thepublic/videosdirectory. Additionally you can specify thesize: "#{width}x#{height}"option just like animage_tag. Video tags can also have any of the HTML options specified at the end (id,classet al).The video tag also supports all of the
<video>HTML options through the HTML options hash, including:poster: "image_name.png", provides an image to put in place of the video before it starts playing.autoplay: true, starts playing the video on page load.loop: true, loops the video once it gets to the end.controls: true, provides browser supplied controls for the user to interact with the video.autobuffer: true, the video will pre load the file for the user on page load.
You can also specify multiple videos to play by passing an array of videos to the
video_tag:<%= video_tag ["trailer.ogg", "movie.ogg"] %>1Linking Audio File
The
audio_taghelper builds an HTML5<audio>tag to the specified file. By default, files are loaded frompublic/audios.<%= audio_tag "music.mp3" %>1You can also supply a hash of additional options, such as
:id,:class, etc.Like the
video_tag, theaudio_taghas special options:autoplay: true, starts playing the audio on page loadcontrols: true, provides browser supplied controls for the user to interact with the audio.autobuffer: true, the audio will pre load the file for the user on page load.