Class: Tilt::CSVTemplate

Inherits:
Template show all
Defined in:
lib/tilt/csv.rb

Overview

CSV Template implementation. See: ruby-doc.org/stdlib/libdoc/csv/rdoc/CSV.html

Example

# Example of csv template
tpl = "  # header\n  csv << ['NAME', 'ID']\n\n  # data rows\n  @people.each do |person|\n    csv << [person[:name], person[:id]]\n  end\n"

@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, #compiled_method, default_mime_type, default_mime_type=, #eval_file, #fixed_locals?, #initialize, #metadata, metadata, #name, #render

Constructor Details

This class inherits a constructor from Tilt::Template

Instance Method Details

#precompiled(locals) ⇒ Object



47
48
49
50
# File 'lib/tilt/csv.rb', line 47

def precompiled(locals)
  source, offset = super
  [source, offset + 1]
end

#precompiled_template(locals) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/tilt/csv.rb', line 39

def precompiled_template(locals)
  "    \#{@outvar} = CSV.generate(**\#{@options}) do |csv|\n      \#{@data}\n    end\n  RUBY\nend\n"

#prepareObject



35
36
37
# File 'lib/tilt/csv.rb', line 35

def prepare
  @outvar = @options.delete(:outvar) || '_csvout'
end