Ruby's ERB templates are handy things you can use in a variety of places beyond using for web / Ruby on Rails context.
Since ERB
is built into Ruby no additional gems / dependencies needs to be installed beyond Ruby.
Quick ref guide to enable ruby / ruby output / comments in ERB template file:
<% %> # execute Ruby code inside brackets (no output)
<%= %> # execute Ruby code and prints the output into template
<%== %> # execute and prints something verbatim (i.e. w/o escaping)
<% -%> # executes / avoid line break after expression
<%# %> # comments out code within the brackets
Below is output from when I asked Chat GPT to provide a simple explanation with examples that shows how to create and use a Markdown ERb template in a Ruby script to pass simple data structures into a Markdown Erb to help speed run past the silly bumps in learning this concept.
1. Create the Markdown ERb Template
First, create a file named report-summary.md.erb. This file is your template that will mix static Markdown content with dynamic Ruby code. For example, your template might look like this:
---
time_generated: <%= time_generated %>
report_count: <%= reports.size %>
---
# Report Summary
| Name | Title |
| ---- | ----- |
<% reports.each do |report| %>
| [<%= report[:name] %>](#<%= report[:name].downcase.gsub(" ", "-") %>) | <%= report[:title] %> |
<% end %>
# Details
<% reports.each do |report| %>
## <a name="<%= report[:name].downcase.gsub(" ", "-") %>"></a><%= report[:name] %>
- **Title:** <%= report[:title] %>
- **Date:** <%= report[:date] %>
- **Pop:** <%= report[:pop] %>
<% end %>
Explanation:
- Metadata header: The top block outputs metadata (like when the report was generated and the number of reports).
- Markdown table: The first section creates a table listing each report’s name (as a link) and title.
- Details section: The lower section provides more detail for each report. Notice how we use Ruby code (with
<% ... %>
and<%= ... %>
) to loop over thereports
array and output the properties.
2. Setup Your Ruby Script
In your Ruby script, you need to require the ERB library, load the template, and set up the variables (such as reports
and time_generated
) that your template expects.
Here’s a simple example:
require 'erb'
require 'time'
# Assume 'reports' is already defined and populated, for example:
reports = [
{ name: "Alpha", title: "Report Alpha", date: "2025-03-11", pop: 123 },
{ name: "Beta", title: "Report Beta", date: "2025-03-10", pop: 456 }
]
# Set a variable for the current time to be used in the template.
time_generated = Time.now
# Read the Markdown ERB template from the file.
template_content = File.read("report-summary.md.erb")
# Create a new ERB instance with the template content.
erb_template = ERB.new(template_content)
# Render the template. The 'binding' makes local variables (reports, time_generated) available inside the template.
output = erb_template.result(binding)
Explanation:
- Require Libraries: The script requires
erb
(for templating) andtime
(to generate the current time). - Setup Data: Here, we assume
reports
is an array of hashes. We also settime_generated
to the current time. - Load and Render Template: We read the template file, create an ERB object, and call
.result(binding)
to process it. Thebinding
here captures all local variables so the ERb template can use them.
3. Write the Output to a File
After rendering the template, you’ll likely want to save the output as a Markdown file. You can do this with:
# Write the rendered output to a new file, e.g., report-summary.md.
File.open("report-summary.md", "w") do |file|
file.write(output)
end
puts "Markdown report generated: report-summary.md"
Explanation:
- File Output: This snippet opens a new file in write mode and writes the generated Markdown content into it.
- User Feedback: Optionally, it prints a confirmation message.
Summary
- Create your template: Write a Markdown file with embedded Ruby code (ERb) to produce dynamic content.
- Setup your script: Require the necessary libraries, prepare your data (like the
reports
array), and load your template. - Render and output: Process the template with
ERB.new
andbinding
, then write the result to a file.
This approach keeps your logic separate from presentation, and it’s a great introduction to using ERb with Markdown in Ruby.