Package org.apache.velocity.runtime.directive

# Class Foreach

java.lang.Object
org.apache.velocity.runtime.directive.Directive
org.apache.velocity.runtime.directive.Foreach

All Implemented Interfaces:
  `Cloneable`, `DirectiveConstants`

Direct Known Subclasses:
  `For`

public class Foreach extends Directive
Foreach directive used for moving through arrays, or objects that provide an Iterator.

Version:
  $Id$
Author:
  Jason van Zyl, Geir Magnusson Jr., Daniel Rall

## Field Summary

Fields
Modifier and Type
Field
Description
`protected Info`
`uberInfo`
immutable, so create in init

### Fields inherited from class org.apache.velocity.runtime.directive.Directive

`log, rsvc`

### Fields inherited from interface org.apache.velocity.runtime.directive.DirectiveConstants

`BLOCK, LINE`

## Constructor Summary

Constructors
Constructor
Description
`Foreach()`

## Method Summary

Modifier and Type
Method
Description
`void`
`checkArgs(ArrayList<Integer> argtypes, Token t, String templateName)`
We do not allow a word token in any other arg position except for the 2nd since we are looking for the pattern #foreach($foo in $bar).
`protected void`
`clean(InternalContextAdapter context, Object o)`
`protected Iterator`
`getIterator(Object iterable, Node node)`
Retrieve the contextual iterator.
`String`
`getName()`
Return name of this directive.
`int`
`getType()`
Return type of this directive.
`void`
`init(RuntimeServices rs, InternalContextAdapter context, Node node)`
simple init - init the tree and get the elementKey from the AST
`protected void`
`put(InternalContextAdapter context, String key, Object value)`
Extension hook to allow subclasses to control whether loop vars are set locally or not.
`boolean`
`render(InternalContextAdapter context, Writer writer, Node node)`
renders the #foreach() block
`protected void`
`renderBlock(InternalContextAdapter context, Writer writer, Node block)`

### Methods inherited from class org.apache.velocity.runtime.directive.Directive

`getColumn, getLine, getScopeName, getTemplate, getTemplateName, isScopeProvided, makeScope, postRender, preRender, setLocation, setLocation`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### uberInfo

protected Info uberInfo
immutable, so create in init

## Constructor Details

### Foreach

public Foreach()

## Method Details

### getName

public String getName()
Return name of this directive.

Specified by:
  `getName` in class `Directive`
Returns:
  The name of this directive.

### getType

public int getType()
Return type of this directive.

Specified by:
  `getType` in class `Directive`
Returns:
  The type of this directive.

### init

public void init(RuntimeServices rs, InternalContextAdapter context, Node node) throws TemplateInitException
simple init - init the tree and get the elementKey from the AST

Overrides:
  `init` in class `Directive`
Parameters:
  `rs` -
  `context` -
  `node` -
Throws:
  `TemplateInitException`

### put

protected void put(InternalContextAdapter context, String key, Object value)
Extension hook to allow subclasses to control whether loop vars are set locally or not. So, those in favor of VELOCITY-285, can make that happen easily by overriding this and having it use context.localPut(k,v). See VELOCITY-630 for more on this.

Parameters:
  `context` -
  `key` -
  `value` -

### getIterator

protected Iterator getIterator(Object iterable, Node node)
Retrieve the contextual iterator.

Parameters:
  `iterable` -
  `node` -
Returns:
  iterator

### render

public boolean render(InternalContextAdapter context, Writer writer, Node node) throws IOException
renders the #foreach() block

Specified by:
  `render` in class `Directive`
Parameters:
  `context` -
  `writer` -
  `node` -
Returns:
  True if the directive rendered successfully.
Throws:
  `IOException`

### renderBlock

protected void renderBlock(InternalContextAdapter context, Writer writer, Node block) throws IOException

Throws:
  `IOException`

### clean

protected void clean(InternalContextAdapter context, Object o)

### checkArgs

public void checkArgs(ArrayList<Integer> argtypes, Token t, String templateName) throws ParseException
We do not allow a word token in any other arg position except for the 2nd since we are looking for the pattern #foreach($foo in $bar).

Overrides:
  `checkArgs` in class `Directive`
Parameters:
  `argtypes` - type, Array of argument types of each argument to the directive for example StandardParserTreeConstants.JJTWORD
  `t` - token of directive
  `templateName` - the name of the template this directive is referenced in.
Throws:
  `ParseException`