Class: VaporTiltAdapter::Renderer
- Inherits:
-
Object
- Object
- VaporTiltAdapter::Renderer
- Defined in:
- lib/vapor_tilt_adapter.rb
Instance Method Summary collapse
-
#render(view_dir, template_filename, output_path, context, default_layout = false) ⇒ Object
Parses context data, sets instance variables, renders the view, and writes it to the specified file.
Instance Method Details
#render(view_dir, template_filename, output_path, context, default_layout = false) ⇒ Object
Parses context data, sets instance variables, renders the view, and writes it to the specified file.
The values for these parameters will typically come straight from TiltRenderer via ARGV. They are:
- view_dir
-
The absolute path to the directory where your views are located. The last character should be a
/
. - template_filename
-
The filename of the template being rendered, including extension.
- output_path
-
The absolute path of the file to which the rendered template will be written.
- context
-
A JSON string containing all the data (primarily the values of instance variables) to be used in your templates
- default_layout
-
Either a string containing the filename of the default layout template, or
+false+ to have the default be to render without a layout.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/vapor_tilt_adapter.rb', line 22 def render(view_dir, template_filename, output_path, context, default_layout = false) json = JSON.parse(context, symbolize_names: true) || {} json[:layout] = default_layout unless json.key? :layout ivars = json.select { |k, _| k[0] == '@' } ivars&.each do |k, v| instance_variable_set(k, v) end template = Tilt.new(view_dir + template_filename) outstr = if (layout = json[:layout]) Tilt.new(view_dir + layout).render(self) { template.render(self) } else template.render(self) end FileUtils.mkdir_p output_path.sub(%r{/[^/]*$}, '') File.write(output_path, outstr) rescue write_error(output_path, $ERROR_INFO) raise $ERROR_INFO end |