Sha256: 81d5b297e9e6378e467747208fc428314de4de6135172116078221f84b1452b7

Contents?: true

Size: 903 Bytes

Versions: 2

Compression:

Stored size: 903 Bytes

Contents

require 'tilt/template'

module Tilt
  # Nokogiri template implementation. See:
  # http://nokogiri.org/
  class NokogiriTemplate < Template
    self.default_mime_type = 'text/xml'

    def self.engine_initialized?
      defined? ::Nokogiri
    end

    def initialize_engine
      require_template_library 'nokogiri'
    end

    def prepare; end

    def evaluate(scope, locals, &block)
      block &&= proc { yield.gsub(/^<\?xml version=\"1\.0\"\?>\n?/, "") }

      if data.respond_to?(:to_str)
        super(scope, locals, &block)
      else
        ::Nokogiri::XML::Builder.new.tap(&data).to_xml
      end
    end

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

    def precompiled_postamble(locals)
      "xml.to_xml"
    end

    def precompiled_template(locals)
      data.to_str
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tilt-1.3.1 lib/tilt/nokogiri.rb
tilt-1.3 lib/tilt/nokogiri.rb