Class: Tilt::CSVTemplate
Overview
CSV Template implementation. See: ruby-doc.org/stdlib/libdoc/csv/rdoc/CSV.html
Example
# Example of csv template
tpl = <<-EOS
# header
csv << ['NAME', 'ID']
# data rows
@people.each do |person|
csv << [person[:name], person[:id]]
end
EOS
@people = [
{:name => "Joshua Peek", :id => 1},
{:name => "Ryan Tomayko", :id => 2},
{:name => "Simone Carletti", :id => 3}
]
template = Tilt::CSVTemplate.new { tpl }
template.render(self)
Instance Attribute Summary
Attributes inherited from Template
#compiled_path, #data, #file, #line, #options
Instance Method Summary collapse
Methods inherited from Template
#basename, default_mime_type, default_mime_type=, #eval_file, #initialize, metadata, #metadata, #name, #render
Constructor Details
This class inherits a constructor from Tilt::Template
Instance Method Details
#precompiled(locals) ⇒ Object
46 47 48 49 |
# File 'lib/tilt/csv.rb', line 46 def precompiled(locals) source, offset = super [source, offset + 1] end |
#precompiled_template(locals) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/tilt/csv.rb', line 38 def precompiled_template(locals) <<-RUBY #{@outvar} = CSV.generate(**#{}) do |csv| #{data} end RUBY end |
#prepare ⇒ Object
34 35 36 |
# File 'lib/tilt/csv.rb', line 34 def prepare @outvar = .delete(:outvar) || '_csvout' end |