SubscribeTag

class
Superclass
Inversion::Template::Tag

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>

Attributes

content[R]

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

default[R]

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

key[R]

The name of the key the nodes will be published under

Public Class Methods

anchor
new( body, line=nil, column=nil )

Create a new SubscribeTag with the given body.

# File lib/inversion/template/subscribetag.rb, line 31
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

Public Instance Methods

anchor
before_rendering( renderstate )

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

# File lib/inversion/template/subscribetag.rb, line 63
def before_rendering( renderstate )
        @content.clear
        renderstate.subscribe( self.key, self )
end
anchor
inspect()

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

# File lib/inversion/template/subscribetag.rb, line 97
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
anchor
publish( *nodes )

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

# File lib/inversion/template/subscribetag.rb, line 77
def publish( *nodes )
        self.log.debug "Adding published nodes %p to %p" % [ nodes, @content ]
        @content.push( *nodes )
end
anchor
render( renderstate )

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

# File lib/inversion/template/subscribetag.rb, line 70
def render( renderstate )
        return self
end
anchor
to_s()

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

# File lib/inversion/template/subscribetag.rb, line 85
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