A robust, safe, and feature-rich wrapper for ERB (Embedded Ruby) template rendering outside of Rails.
Stellar::Erb provides a sophisticated yet easy-to-use solution for working with ERB templates in Ruby applications. It offers enhanced error handling, clean backtraces, and a flexible API that makes template rendering both powerful and maintainable.
- Safe Template Rendering: Built-in protection against common template injection vulnerabilities
- Rich Error Context: Detailed error messages with line numbers and surrounding code context
- Flexible Variable Passing: Support for both global and per-render local variables
- String Template Support: Render ERB directly from strings in addition to files
- Reusable Views: Create view instances for efficient template reuse
- Minimal Dependencies: Works standalone without requiring Rails
Add to your Gemfile:
gem 'stellar-erb'
Or install directly:
$ gem install stellar-erb
# template.erb
<div class="greeting">
<h1>Hello, <%= name %>!</h1>
<% if show_date %>
<p>Today is <%= Date.today %></p>
<% end %>
</div>
# Ruby code
result = Stellar::Erb.render('template.erb',
name: 'John',
show_date: true
)
template_string = <<~ERB
<ul class="items">
<% items.each do |item| %>
<li><%= item.name %> - $<%= item.price %></li>
<% end %>
</ul>
ERB
result = Stellar::Erb.render_string(template_string,
items: [
OpenStruct.new(name: 'Widget', price: 9.99),
OpenStruct.new(name: 'Gadget', price: 19.99)
]
)
# Create a reusable view instance
header = Stellar::Erb::View.new('partials/header.erb',
company: 'Acme Corp',
logo_url: '/images/logo.png'
)
# Render with different page-specific variables
page1 = header.render(title: 'Home Page')
page2 = header.render(title: 'About Us')
begin
result = Stellar::Erb::View.render('template.erb', user: current_user)
rescue Stellar::Erb::Error => e
puts "Template Error: #{e.message}"
# Show context around the error
puts "\nError Context:"
puts e.context_lines.join("\n")
# Access original error details
puts "Line Number: #{e.line_number}"
puts "Template Path: #{e.template_path}"
end
# Setup development environment
bin/setup
# Run tests
rake test
# Run specific test file
ruby -Ilib:test test/stellar/erb/view_test.rb
# Generate documentation
yard doc
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Add tests for your changes
- Make your changes
- Ensure tests pass (
rake test
) - Commit your changes (
git commit -am 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Create a Pull Request
flowchart TD
A[Fork Repository] --> B[Clone Locally]
B --> C[Install Dependencies]
C --> D[Run Tests]
D --> E{Tests Pass?}
E -->|Yes| F[Create Feature Branch]
E -->|No| D
F --> G[Make Changes]
G --> H[Run Tests]
H --> I{Tests Pass?}
I -->|Yes| J[Submit PR]
I -->|No| G
Released under the MIT License. See LICENSE for details.
- Documentation: RubyDoc
- Issues: GitHub Issues
- Discussions: GitHub Discussions
Professional support, custom development, and training services are available from Durable Programming, LLC.