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