Class: Tilt::NokogiriTemplate

Inherits:
Template
  • Object
show all
Defined in:
lib/tilt/nokogiri.rb

Overview

Nokogiri template implementation. See: nokogiri.org/

Constant Summary collapse

DOCUMENT_HEADER =
/\A<\?xml version=\"1\.0\"\?>\n?/

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

#evaluate(scope, locals) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/tilt/nokogiri.rb', line 12

def evaluate(scope, locals)
  if @data.respond_to?(:to_str)
    if block_given?
      super(scope, locals){yield.sub(DOCUMENT_HEADER, "")}
    else
      super
    end
  else
    ::Nokogiri::XML::Builder.new(&@data).to_xml
  end
end

#precompiled_postamble(locals) ⇒ Object



29
30
31
# File 'lib/tilt/nokogiri.rb', line 29

def precompiled_postamble(locals)
  "xml.to_xml"
end

#precompiled_preamble(locals) ⇒ Object



24
25
26
27
# File 'lib/tilt/nokogiri.rb', line 24

def precompiled_preamble(locals)
  return super if locals.include? :xml
  "xml = ::Nokogiri::XML::Builder.new { |xml| }\n#{super}"
end

#precompiled_template(locals) ⇒ Object



33
34
35
# File 'lib/tilt/nokogiri.rb', line 33

def precompiled_template(locals)
  @data.to_str
end