Class: Inversion::Template::FragmentTag

Inherits:
Tag
  • Object
show all
Includes:
ContainerTag
Defined in:
lib/inversion/template/fragmenttag.rb

Overview

Inversion ‘fragment’ tag.

This tag provides a way to generate a fragment of content once in a template as an attribute, and then reuse it later either in the same template or even outside of it.

Syntax

<?fragment subject ?>Receipt for Order #<?call order.number ?><?end subject ?>

Constant Summary

Constants inherited from Tag

Tag::TAG_PLUGIN_PATTERN

Instance Attribute Summary collapse

Attributes included from ContainerTag

#subnodes

Attributes inherited from Tag

#body

Attributes inherited from Node

#colnum, #linenum

Instance Method Summary collapse

Methods included from ContainerTag

#<<, #is_container?, #render_subnodes

Methods inherited from Tag

#as_comment_body, create, #derivatives, inherited, load, load_all, #tagname, types, #types

Methods included from MethodUtilities

#singleton_attr_accessor, #singleton_attr_reader, #singleton_attr_writer

Methods included from AbstractClass

included

Methods included from AbstractClass::ClassMethods

#inherited, #pure_virtual

Methods inherited from Node

#after_appending, #after_rendering, #as_comment_body, #before_appending, #before_rendering, #is_container?, #location

Constructor Details

#initialize(body, line = nil, column = nil) ⇒ FragmentTag

Create a new FragmentTag with the given ‘body`.



24
25
26
27
28
29
30
31
# File 'lib/inversion/template/fragmenttag.rb', line 24

def initialize( body, line=nil, column=nil )
	super

	key = self.body[ /^([a-z]\w+)$/ ] or
		raise Inversion::ParseError,
			"malformed key: expected simple identifier, got %p" % [ self.body ]
	@key = key.to_sym
end

Instance Attribute Details

#keyObject (readonly)

The fragment key; corresponds to the name of the attribute that will be set by the rendered contents of the fragment.



40
41
42
# File 'lib/inversion/template/fragmenttag.rb', line 40

def key
  @key
end

Instance Method Details

#render(renderstate) ⇒ Object

Render the fragment and store it as an attribute.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/inversion/template/fragmenttag.rb', line 44

def render( renderstate )
	self.log.debug "Publishing %d nodes as %s" % [ self.subnodes.length, self.key ]
	rendered_nodes = []
	renderstate.with_destination( rendered_nodes ) do
		sn = self.render_subnodes( renderstate )
		# self.log.debug "  subnodes are: %p" % [ sn ]
		sn
	end

	# self.log.debug "  rendered nodes are: %p" % [ rendered_nodes ]
	renderstate.add_fragment( self.key, rendered_nodes )

	return nil
end