Concept Flow - ERB template syntax
Start ERB Template
Read line
Check for ERB tags
Run Ruby
No Output
Next line or End
The ERB template reads each line, detects Ruby code tags, executes code, and inserts output if needed.
<h1>Welcome</h1>
<% name = "Alice" %>
<p>Hello, <%= name %>!</p>| Step | Line Read | ERB Tag | Action | Output Produced |
|---|---|---|---|---|
| 1 | <h1>Welcome</h1> | None | Output HTML as is | <h1>Welcome</h1> |
| 2 | <% name = "Alice" %> | <% %> | Run Ruby code, no output | |
| 3 | <p>Hello, <%= name %>!</p> | <%= %> | Run Ruby, insert output | <p>Hello, Alice!</p> |
| 4 | End of template | None | Stop processing |
| Variable | Start | After Step 2 | Final |
|---|---|---|---|
| name | undefined | "Alice" | "Alice" |
ERB template syntax: - <% code %> runs Ruby code, no output - <%= code %> runs Ruby and inserts output - Lines without tags output as HTML - Used to mix Ruby logic with HTML - Output builds line by line as template processes
<%= %> tag evaluates Ruby code and inserts the result into the HTML.<% %> runs Ruby code without output, <%# %> is a comment, and <%== %> is not standard ERB syntax.<% %> tag runs Ruby code but does not insert output into HTML.<%= %> outputs result, <%# %> is a comment, and <%== %> is invalid syntax.<ul>
<% items.each do |item| %>
<li><%= item.upcase %></li>
<% end %>
</ul>items = ['apple', 'banana']?item.upcase.<li> tags with uppercase text, so the list shows APPLE and BANANA.<ul>
<% items.each do |item| %>
<li><% item.upcase %></li>
<% end %>
</ul><% item.upcase %> which runs Ruby but does not output anything.<%= item.upcase %> instead.user.admin? is true. Which snippet correctly does this?<% if user.admin? %> starts an if block where literal text like "Welcome, admin!" is rendered only if true, ended by <% end %>.: '').Pass the test and this pattern moves unknown