Using ERb With Your Reports
NOTE: The following recipe is meant for use with Ruport 0.8
If you have a Report which you'd like to use an ERb template with, Report#erb provides a simple way to do this.
Essentially, any string or a filename which matches the pattern /\.r\w+$/ can be used as a template for your report, and the embedded code will be evaluated in the context of your Report.
The following example shows how you might make use of this feature. The rope project which implements this report is attached.
Simple Test ( test/test_report_with_template.rb )
require "test/unit"
require "lib/reports/report_with_template"
class TestReportWithTemplate < Test::Unit::TestCase
def test_template
rep = ReportWithTemplate.new
rep.template = "report.rtxt"
rep.name = "World"
assert_equal "Hello World\n", rep.run
end
end
Report definition ( lib/reports/report_with_template.rb )
require "lib/init"
class ReportWithTemplate < Ruport::Report
attr_accessor :template, :name
def generate
erb "templates/#{template}"
end
end
Template ( templates/report.rtxt )
Hello <%= name %>
Attachments
- erb_sample.zip (4.8 kB) - added by sandal on 03/09/07 20:26:20.
