Class: Malt::Engine::Nokogiri

Inherits:
Abstract show all
Defined in:
lib/malt/engines/nokogiri.rb

Overview

Nokogiri builder-like templates.

Constant Summary collapse

DOCUMENT_HEADER_XML =
/^<\?xml version=\"1\.0\"\?>\n?/
DOCUMENT_HEADER_HTML =
/^<\!DOCTYPE html PUBLIC \".*?\">/

Instance Attribute Summary

Attributes inherited from Abstract

#settings

Instance Method Summary collapse

Methods inherited from Abstract

#cache?, default, #initialize, register, type

Constructor Details

This class inherits a constructor from Malt::Engine::Abstract

Instance Method Details

#create_engine(params = {}) ⇒ Object

Nokogiri engine cannot be cached as it keeps a copy the rendering internally. (Unless there is a way to clear it?)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/malt/engines/nokogiri.rb', line 60

def create_engine(params={})
  into = parameters(params, :to) || :html

  opts = engine_options(params)

  #cached(into, opts) do
    case into
    when :html
      ::Nokogiri::HTML::Builder.new(opts)
    else
      ::Nokogiri::XML::Builder.new(opts)
    end
  #end
end

#prepare_engine(params = {}, &content) ⇒ Object


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/malt/engines/nokogiri.rb', line 35

def prepare_engine(params={}, &content)
  text, file, scope, locals = parameters(params, :text, :file, :scope, :locals)

  scope, locals = make_external(scope, locals, &content)

  engine = create_engine(params)

  locals.each do |k,v|
    engine.instance_eval("@#{k} = v")
  end

  scope.instance_variables.each do |k|
    next if k == "@target"
    v = scope.instance_variable_get(k)
    engine.instance_eval("#{k} = v")    
  end

  engine.instance_eval(text, file || inspect)

  engine
end

#render(params = {}, &content) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/malt/engines/nokogiri.rb', line 21

def render(params={}, &content)
  into = parameters(params, :to) || :html

  case into.to_sym
  when :html
    prepare_engine(params, &content).to_html.sub(DOCUMENT_HEADER_HTML,'')
  when :xml, :xhtml
    prepare_engine(params, &content).to_xml.sub(DOCUMENT_HEADER_XML,'')
  else
    super(params, &content)
  end
end