Class: Arrow::Template::ForDirective
- Inherits:
-
BracketingDirective
- Object
- Object
- Node
- Directive
- AttributeDirective
- BracketingDirective
- Arrow::Template::ForDirective
- Includes:
- Parser::Patterns
- Defined in:
- lib/arrow/template/for.rb
Overview
The Arrow::Template::ForDirective class, a derivative of Arrow::Template::BracketingDirective. This is the class which defines the behaviour of the ‘for’ template directive.
Syntax
<?for <arglist> in <obj>?>...<?end for?>
This directive iterates over all the items in an Enumerable object (via the #entities method), rendering the contents once for each object. The specified #arglist is similar to Ruby’s argument lists: it supports defaults, as well as array (e.g., *rest
) and hash arguments.
While the contents are rendering, a special attribute named iterator is set to an Arrow::Template::Iterator object, which can be used to get information about the iteration itself. This directive doesn’t add anything to the output directly, but relies on its subnodes for content.
This directive only works with Enumerable objects; for other objects with iterators or blocks, use the <?yield?> directive.
Examples
<!-- Iterate over the headers in a request -->
<?for name, value in request.headers_in ?>
<strong><?attr name?>:</strong> <?attr value?><br/>
<?end for?>
<!-- Same thing, but this time in a table with alternating styles for each
row. -->
<table>
<?for name, value in request.headers_in ?>
<?if iterator.even? ?>
<tr class="even-row">
<?else?>
<tr class="odd-row">
<?end if?>
<td><?attr name?></td> <td><?attr value?></td>
</tr>
<?end for?>
</table>
<!-- Pair up words with their lengths and sort them shortest first, then
print them out with their lengths -->
<?for word, length in tests.
collect {|item| [item, item.length]}.
sort_by {|item| item[1]} ?>
<?attr word?>: <?attr length?>
<?end for?>
Authors
-
Michael Granger <[email protected]>
Please see the file LICENSE in the top-level directory for licensing details.
Constant Summary collapse
- IN =
The regexp for matching the ‘in’ part of the directive
WHITESPACE + /in/i + WHITESPACE
Constants included from Parser::Patterns
Parser::Patterns::ALTERNATION, Parser::Patterns::ARGDEFAULT, Parser::Patterns::ARGUMENT, Parser::Patterns::CAPTURE, Parser::Patterns::COMMA, Parser::Patterns::DBLQSTRING, Parser::Patterns::DOT, Parser::Patterns::EQUALS, Parser::Patterns::IDENTIFIER, Parser::Patterns::INFIX, Parser::Patterns::LBRACKET, Parser::Patterns::NUMBER, Parser::Patterns::PATHNAME, Parser::Patterns::QUOTEDSTRING, Parser::Patterns::RBRACKET, Parser::Patterns::REBINDOP, Parser::Patterns::REGEXP, Parser::Patterns::SLASHQSTRING, Parser::Patterns::SYMBOL, Parser::Patterns::TAGCLOSE, Parser::Patterns::TAGMIDDLE, Parser::Patterns::TAGOPEN, Parser::Patterns::TICKQSTRING, Parser::Patterns::VARIABLE, Parser::Patterns::WHITESPACE
Constants inherited from BracketingDirective
BracketingDirective::SVNId, BracketingDirective::SVNRev
Constants inherited from AttributeDirective
AttributeDirective::SVNId, AttributeDirective::SVNRev
Constants inherited from Directive
Directive::SVNId, Directive::SVNRev
Constants inherited from Node
Constants included from HTMLUtilities
HTMLUtilities::ARRAY_HTML_CONTAINER, HTMLUtilities::HASH_HTML_CONTAINER, HTMLUtilities::HASH_PAIR_HTML, HTMLUtilities::IMMEDIATE_OBJECT_HTML_CONTAINER, HTMLUtilities::IVAR_HTML_FRAGMENT, HTMLUtilities::OBJECT_HTML_CONTAINER, HTMLUtilities::THREAD_DUMP_KEY
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
The argument list for the iterator, with sigils and defaults, if any.
-
#pureargs ⇒ Object
readonly
The argument list for the iterator, with any sigils and defaults stripped away.
Attributes inherited from BracketingDirective
Attributes inherited from AttributeDirective
Attributes inherited from Node
Instance Method Summary collapse
-
#initialize(body, parser, state) ⇒ ForDirective
constructor
Create a new Arrow::Template::ForDirective object.
Methods inherited from BracketingDirective
#add_to_template, #inspect, #is_rendering_node?, #to_a, #to_html
Methods inherited from AttributeDirective
allows_format?, #before_rendering, #inspect, #is_rendering_node?, #render, #to_html
Methods inherited from Directive
create, derivativeDirs, #inspect, #render, #to_html
Methods inherited from Node
#add_to_template, #inspect, #is_rendering_node?, #render, #to_a, #to_html, #to_s
Methods included from HTMLUtilities
escape_html, make_html_for_object, make_object_html_wrapper
Methods inherited from Object
deprecate_class_method, deprecate_method, inherited
Constructor Details
#initialize(body, parser, state) ⇒ ForDirective
Create a new Arrow::Template::ForDirective object.
77 78 79 80 81 |
# File 'lib/arrow/template/for.rb', line 77 def initialize( body, parser, state ) @args = [] @pureargs = [] super end |
Instance Attribute Details
#args ⇒ Object (readonly)
The argument list for the iterator, with sigils and defaults, if any.
89 90 91 |
# File 'lib/arrow/template/for.rb', line 89 def args @args end |
#pureargs ⇒ Object (readonly)
The argument list for the iterator, with any sigils and defaults stripped away.
93 94 95 |
# File 'lib/arrow/template/for.rb', line 93 def pureargs @pureargs end |