Class: Inversion::Template::PublishTag

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

Overview

Inversion publish tag.

The publish tag exports one or more subnodes to enclosing templates.

Syntax

<!-- Outer template -->
<html>
  <head>
    <?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 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

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

Constructor Details

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

Create a new PublishTag with the given body.



35
36
37
38
39
40
41
42
# File 'lib/inversion/template/publishtag.rb', line 35

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 name of the key the nodes will be published under



50
51
52
# File 'lib/inversion/template/publishtag.rb', line 50

def key
  @key
end

Instance Method Details

#as_comment_bodyObject

Render the tag as the body of a comment, suitable for template debugging.



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

def as_comment_body
  return "Published %d nodes as %s" % [ self.subnodes.length, self.key ]
end

#render(renderstate) ⇒ Object

Render the published subnodes in the context of the given renderstate, but save them for publication after the render is done.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/inversion/template/publishtag.rb', line 55

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.publish( self.key, *rendered_nodes ) unless rendered_nodes.empty?

  return nil
end