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.

[View source]

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

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


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

def content
  @content
end

#defaultObject (readonly)

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


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

def default
  @default
end

#keyObject (readonly)

The name of the key the nodes will be published under


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

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.

[View source]

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

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.

[View source]

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

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.

[View source]

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

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.

[View source]

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

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.

[View source]

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

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