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 = <<-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, #compiled_method, 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



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)
  <<-RUBY
    #{@outvar} = CSV.generate(**#{@options}) do |csv|
      #{@data}
    end
  RUBY
end

#prepareObject



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

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