Class: Inversion::Template::SubscribeTag

Inherits:
Tag
  • Object
show all
Defined in:
lib/inversion/template/subscribetag.rb

Overview

Inversion subscription tag.

The subscribe tag places one or more published nodes from subtemplates.

Syntax

<!-- Outer template -->
<html>
  <head>
    <title><?subscribe title || Untitled ?></title>
    <?subscribe headers ?>
  </head>
  <body><?attr body ?></body>
</html>

<!-- In the body template, add a stylesheet link to the outer
     template's <head> -->
<?publish headers ?>
   <link rel="stylesheet" ... />
<?end ?>
<div>(page content)</div>

Constant Summary

Constants inherited from Tag

Tag::TAG_PLUGIN_PATTERN

Instance Attribute Summary collapse

Attributes inherited from Tag

#body

Attributes inherited from Node

#colnum, #linenum

Instance Method Summary collapse

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, #is_container?, #location

Constructor Details

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

Create a new SubscribeTag with the given ‘body`.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/inversion/template/subscribetag.rb', line 32

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

	unless self.body =~ /^([a-z]\w+)(?:\s*\|\|\s*(.+))?$/
		raise Inversion::ParseError,
			"malformed subscribe: %p" % [ self.body ]
	end

	key, default = $1, $2

	@key = key.to_sym
	@content = []
	@default = default
end

Instance Attribute Details

#contentObject (readonly)

The content publish to the tag so far during the current render



59
60
61
# File 'lib/inversion/template/subscribetag.rb', line 59

def content
  @content
end

#defaultObject (readonly)

The tag’s default value if nothing matching its key is published



56
57
58
# File 'lib/inversion/template/subscribetag.rb', line 56

def default
  @default
end

#keyObject (readonly)

The name of the key the nodes will be published under



53
54
55
# File 'lib/inversion/template/subscribetag.rb', line 53

def key
  @key
end

Instance Method Details

#before_rendering(renderstate) ⇒ Object

Tell the ‘renderstate` that this tag is interested in nodes that are published with its key.



64
65
66
67
# File 'lib/inversion/template/subscribetag.rb', line 64

def before_rendering( renderstate )
	@content.clear
	renderstate.subscribe( self.key, self )
end

#inspectObject

Return a representation of the object in a String suitable for debugging.



98
99
100
101
102
103
104
105
106
# File 'lib/inversion/template/subscribetag.rb', line 98

def inspect
	return "#<%p:0x%016x key: %s, default: %p, content: %p>" % [
		self.class,
		self.object_id * 2,
		self.key,
		self.default,
		self.content,
	]
end

#publish(*nodes) ⇒ Object

Pub/sub callback. Called from the RenderState when a PublishTag publishes ‘nodes` with the same key as the current tag.



78
79
80
81
# File 'lib/inversion/template/subscribetag.rb', line 78

def publish( *nodes )
	self.log.debug "Adding published nodes %p to %p" % [ nodes, @content ]
	@content.push( *nodes )
end

#render(renderstate) ⇒ Object

Return the subscribe node itself to act as a placeholder for subscribed nodes.



71
72
73
# File 'lib/inversion/template/subscribetag.rb', line 71

def render( renderstate )
	return self
end

#to_sObject

Stringify and join all of the published nodes for this subscription and return them as a String.



86
87
88
89
90
91
92
93
94
# File 'lib/inversion/template/subscribetag.rb', line 86

def to_s
	if @content.empty?
		self.log.debug "Nothing published with the %p key, defaulting to %p" %
			[ self.key, @default ]
		return @default.to_s
	else
		return @content.map( &:to_s ).join( '' )
	end
end