Class: Arrow::Template::YieldDirective

Inherits:
BracketingDirective show all
Includes:
Parser::Patterns
Defined in:
lib/arrow/template/yield.rb

Overview

The Arrow::Template::YieldDirective class, a derivative of Arrow::Template::BracketingDirective. This is the class which defines the behaviour of the ‘yield’ template directive.

Syntax

 <?yield <args> from <attribute>.<block_method> ?>

The args portion is similar to Ruby argument lists: it supports defaults, *vars, and hash arguments.

Examples

 <!-- Iterate over the incoming headers of an Apache::Request -->
 <?yield name, value from request.headers_in.each ?>
   <?attr name ?>: <?escape value ?>
 <?end yield?>

 

Authors

  • Michael Granger

Please see the file LICENSE in the top-level directory for licensing details.

Constant Summary

FROM =

The regexp format of the ‘yield’ part of the directive tag.

WHITESPACE + /from/i + WHITESPACE

Constants included from Parser::Patterns

ALTERNATION, ARGDEFAULT, ARGUMENT, CAPTURE, COMMA, DBLQSTRING, DOT, EQUALS, IDENTIFIER, INFIX, LBRACKET, NUMBER, PATHNAME, QUOTEDSTRING, RBRACKET, REBINDOP, REGEXP, SLASHQSTRING, SYMBOL, TAGCLOSE, TAGMIDDLE, TAGOPEN, TICKQSTRING, VARIABLE, WHITESPACE

Constants inherited from BracketingDirective

SVNId, SVNRev

Constants inherited from AttributeDirective

SVNId, SVNRev

Constants inherited from Directive

SVNId, SVNRev

Constants inherited from Node

SVNId, SVNRev

Constants included from Arrow::HTMLUtilities

ARRAY_HTML_CONTAINER, HASH_HTML_CONTAINER, HASH_PAIR_HTML, IMMEDIATE_OBJECT_HTML_CONTAINER, IVAR_HTML_FRAGMENT, OBJECT_HTML_CONTAINER, THREAD_DUMP_KEY

Instance Attribute Summary

Class Method Summary

Instance Method Summary

Methods inherited from BracketingDirective

#add_to_template, #inspect, #is_rendering_node?, #render_subnodes, #to_a, #to_html

Methods inherited from AttributeDirective

#before_rendering, #call_methodchain, #inspect, #is_rendering_node?, #render, #to_html

Methods inherited from Directive

create, derivativeDirs, #inspect, #render, #to_html

Methods inherited from Node

#add_to_template, #css_class, #inspect, #is_rendering_node?, #render, #to_a, #to_html, #to_s

Methods included from Arrow::HTMLUtilities

#escape_html, #make_html_for_object, #make_object_html_wrapper

Methods inherited from Arrow::Object

deprecate_class_method, deprecate_method, inherited

Methods included from Arrow::Loggable

#log

Constructor Details

- (YieldDirective) initialize(type, parser, state)

Initialize a new YieldDirective object with the specified type, parser, and state.



55
56
57
58
59
# File 'lib/arrow/template/yield.rb', line 55

def initialize( type, parser, state )
  @args = []
  @pureargs = []
  super
end

Instance Attribute Details

- (Object) args (readonly)

The argument list for the yield block, with sigils and defaults, if any.



67
68
69
# File 'lib/arrow/template/yield.rb', line 67

def args
  @args
end

- (Object) pureargs (readonly)

The argument list for the callback, with any sigils and defaults stripped away.



71
72
73
# File 'lib/arrow/template/yield.rb', line 71

def pureargs
  @pureargs
end

Class Method Details

+ (Boolean) allows_format?

Returns false; disallows prepended formats.

Returns:

  • (Boolean)


44
45
46
# File 'lib/arrow/template/yield.rb', line 44

def self::allows_format?
  false
end

Instance Method Details

- (Object) build_rendering_proc(template, scope) (protected)

Build a Proc object that encapsulates the execution necessary to render the directive.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/arrow/template/yield.rb', line 93

def build_rendering_proc( template, scope )
  code = %q{
    Proc.new {|__item, __callback|
      res = []
      __item%s {|%s| res << __callback.call(%s)}
      res
    }
  } % [ self.methodchain, self.args.join(","), self.pureargs.join(",") ]
  code.untaint

  #self.log.debug "Rendering proc code is: %p" % code
  desc = "[%s (%s): %s]" %
    [ self.class.name, __FILE__, self.methodchain ]

  return eval( code, scope.get_binding, desc, __LINE__ )
end

- (Object) parse_directive_contents(parser, state) (protected)

Parse the contents of the directive, looking for an optional format for tags like , then a required identifier, then an optional methodchain attached to the identifier.



81
82
83
84
85
86
87
88
# File 'lib/arrow/template/yield.rb', line 81

def parse_directive_contents( parser, state )
  @args, @pureargs = parser.scan_for_arglist( state )
  return nil unless @args
  state.scanner.skip( FROM ) or
    raise Arrow::ParseError, "no 'from' for yield"

  super
end

- (Object) render_contents(template, scope) (protected)

Pass a callback to our inner node-rendering method as the block for the specified. Builds a callback which is yielded to from within the block passed to whatever this directive is calling, which in turn renders each of its subnodes with the arguments specified by the yield.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/arrow/template/yield.rb', line 116

def render_contents( template, scope )
  #self.log.debug "Bulding callback for rendering subnodes..."
  callback = Proc.new {|*blockArgs|
    res = []
    attributes = {}
    blockArgs.zip( self.pureargs ) do |pair|
      attributes[ pair[1] ] = pair[0]
    end
    #self.log.debug "  override attributes are: %p" % [ attributes ]
    template.with_overridden_attributes( scope, attributes ) do |template|
      res << template.render( @subnodes, scope )
    end

    res
  }

  #self.log.debug "calling method chain; callback: %p" % callback
  self.call_methodchain( template, scope, callback )
end