Class: Rind::Document

Inherits:
Object
  • Object
show all
Includes:
Equality
Defined in:
lib/rind/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Equality

#==, #eql?

Constructor Details

#initialize(template, options = {}) ⇒ Document

Parameter

template = file system path to the template

Options

  • base_namespace = namespace

    Allows you to provide a namespace to check before falling back to the default Rind namespace.

  • require = array of namespaces

    All namespaces that should be rendered must be listed.

  • type = “xml” or “html”

    This can be automatically detect based on the file extension. Unknown extensions will default to “xml”.

Example

Rind::Document.new( "template.tpl", {
  :type           => "html",
  :base_namespace => "core",
  :require        => ["forms","photos"]
})


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rind/document.rb', line 30

def initialize(template, options = {})
	@template = template
	raise 'No such template.' if not File.file? @template

	if options[:type]
		@type = options[:type]
	else
		@type = case File.extname(@template)
		when '.html', '.htm'
			'html'
		else
			'xml'
		end
	end

	if options[:base_namespace]
		@base_namespace = options[:base_namespace]
	else
		@base_namespace = ['rind', @type].join(':')
	end

	@dom = Rind.parse(@template, @type, @base_namespace, options[:require])
end

Instance Attribute Details

#base_namespaceObject (readonly)

Returns the value of attribute base_namespace.



5
6
7
# File 'lib/rind/document.rb', line 5

def base_namespace
  @base_namespace
end

#domObject (readonly)

Returns the value of attribute dom.



5
6
7
# File 'lib/rind/document.rb', line 5

def dom
  @dom
end

#templateObject (readonly)

Returns the value of attribute template.



5
6
7
# File 'lib/rind/document.rb', line 5

def template
  @template
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/rind/document.rb', line 5

def type
  @type
end

Instance Method Details

#render!Object

Renders the Document in place. This will recursively call render! on all the Document contents.



56
57
58
# File 'lib/rind/document.rb', line 56

def render!
	@dom.collect{|node| node.respond_to?(:render!) ? node.render! : node}.join('')
end

#rootObject

Return the root node of the Document.



61
62
63
64
65
# File 'lib/rind/document.rb', line 61

def root
	@dom.each do |node|
		return node if not node.is_a? Rind::DocType
	end
end

#s(path) ⇒ Object

Xpath search of the root node that returns a list of matching nodes.



72
73
74
# File 'lib/rind/document.rb', line 72

def s(path)
	root.s(path)
end

#sf(path) ⇒ Object

Xpath search returning only the first matching node in the list.



77
78
79
# File 'lib/rind/document.rb', line 77

def sf(path)
	root.sf(path)
end

#to_sObject



67
68
69
# File 'lib/rind/document.rb', line 67

def to_s
	@dom.to_s
end