Class: LEWT::LiquidRenderer
- Defined in:
- lib/extensions/liquid-renderer.rb
Overview
The Liquid Renderer LEWT Extension handles rendering processed data to TEXT, HTML, and PDF formats using the liquid templating engine at its core. This allows for easy marking up of templates to be used with arbitrary LEWT extensions and processing them into multiple human readable formats on the fly.
Instance Attribute Summary collapse
-
#htmlTemplate ⇒ Object
readonly
Returns the value of attribute htmlTemplate.
-
#markup ⇒ Object
readonly
Returns the value of attribute markup.
-
#pdfTemplate ⇒ Object
readonly
Returns the value of attribute pdfTemplate.
-
#stylesheet ⇒ Object
readonly
Returns the value of attribute stylesheet.
-
#textTemplate ⇒ Object
readonly
Returns the value of attribute textTemplate.
Attributes inherited from Extension
#command_name, #customers, #enterprise, #lewt_settings, #lewt_stash, #options
Instance Method Summary collapse
-
#initialize ⇒ LiquidRenderer
constructor
Sets up this extension and registers its run-time options.
-
#load_templates(template) ⇒ Object
- Loads the plaint-text, html, & (optionally) pdf template files of the given template name and parses it with the Liquid class template [String]
-
The name of the template to load.
-
#render(options, data) ⇒ Object
Called on LEWT render cycle, this method outputs the data as per a pre-formated liquid template.
Methods inherited from Extension
#get_matched_customers, #lewt_extensions
Constructor Details
#initialize ⇒ LiquidRenderer
Sets up this extension and registers its run-time options.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/extensions/liquid-renderer.rb', line 20 def initialize () = { :method => { :definition => "Specify html, text, pdf, or any combination of the three to define output method", :default => "text", :short_flag => "-m", :type => String }, :save_path => { :definition => "Specify where to save the output file (required for PDFs)", :type => String }, :liquid_template => { :definition => "Override the template that liquid render should use. Defaults to the template which matches the processor name but you will want to override this if you are using multiple processors.", :type => String } } super({:cmd => "liquid_render", :options => }) end |
Instance Attribute Details
#htmlTemplate ⇒ Object (readonly)
Returns the value of attribute htmlTemplate.
17 18 19 |
# File 'lib/extensions/liquid-renderer.rb', line 17 def htmlTemplate @htmlTemplate end |
#markup ⇒ Object (readonly)
Returns the value of attribute markup.
17 18 19 |
# File 'lib/extensions/liquid-renderer.rb', line 17 def markup @markup end |
#pdfTemplate ⇒ Object (readonly)
Returns the value of attribute pdfTemplate.
17 18 19 |
# File 'lib/extensions/liquid-renderer.rb', line 17 def pdfTemplate @pdfTemplate end |
#stylesheet ⇒ Object (readonly)
Returns the value of attribute stylesheet.
17 18 19 |
# File 'lib/extensions/liquid-renderer.rb', line 17 def stylesheet @stylesheet end |
#textTemplate ⇒ Object (readonly)
Returns the value of attribute textTemplate.
17 18 19 |
# File 'lib/extensions/liquid-renderer.rb', line 17 def textTemplate @textTemplate end |
Instance Method Details
#load_templates(template) ⇒ Object
Loads the plaint-text, html, & (optionally) pdf template files of the given template name and parses it with the Liquid class
- template [String]
-
The name of the template to load.
42 43 44 45 46 |
# File 'lib/extensions/liquid-renderer.rb', line 42 def load_templates ( template ) @textTemplate = Liquid::Template::parse( File.open( File.( lewt_stash + "/templates/#{template}.text.liquid", __FILE__) ).read ) @htmlTemplate = Liquid::Template::parse( File.open( File.( lewt_stash + "/templates/#{template}.html.liquid", __FILE__) ).read ) @stylesheet = File.( lewt_stash + '/templates/style.css', __FILE__) end |
#render(options, data) ⇒ Object
Called on LEWT render cycle, this method outputs the data as per a pre-formated liquid template.
- options [Hash]
-
The options hash passed to this function by the Lewt program.
- data [Array]
-
An array of hash data to format.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/extensions/liquid-renderer.rb', line 51 def render ( , data ) output = Array.new # template name is always the same as processor name template = [:liquid_template] != nil ? [:liquid_template] : [:process] load_templates( template ) data.each_with_index do |d, i| if [:method].match "text" r = textTemplate.render(d) if [:save_path] save_name = format_save_name( , i ) File.open( save_name, 'w') {|f| f.write r } output << save_name else output << r end end if [:method].match "html" r = htmlTemplate.render(d) if [:save_path] save_name = format_save_name( , i ) File.open( save_name, 'w') {|f| f.write r } output << save_name else output << r end end if [:method].match "pdf" raise ArgumentError,"--save-file flag must be specified for PDF output in #{self.class.name}" if ![:save_path] save_name = format_save_name( , i ) html = htmlTemplate.render(d) kit = PDFKit.new(html, :page_size => 'A4') kit.stylesheets << @stylesheet file = kit.to_file( save_name ) output << save_name end end # if options[:dump_output] != false # output.each do |r| # puts r # end # end return output end |