It would not be called on $testing, because $testing is not being inserted into the output. And it is not called on the variables within the loop, because in this example testing appears to be null/empty, so there is no attempt to render the body of the #foreach.
On Fri, Mar 30, 2012 at 6:45 AM, Jean-Francois Croteau <jfcrot...@8d.com> wrote: > Hi everyone, > quick question about the ReferenceInsertionEventHandler: is it supposed to > be called on a variable called into a #foreach loop (or even on the variable > defining the foreach loop)? > > For example: > > public class IsThisAVelocityBug { > > public static void main( String[] args ) { > String template1 = "${otherVariable}\n"; > String template2 = "#foreach($test in > $testing)\n${test.something}\n${otherVariable}\n#end"; > > VelocityEngine veloctiyEngine = new VelocityEngine(); > > VelocityContext velocityContext = new VelocityContext(); > EventCartridge eventCartridge = new EventCartridge(); > velocityContext.attachEventCartridge( eventCartridge ); > > eventCartridge.addReferenceInsertionEventHandler( new ReferenceInsertionEventHandler() > { > > @Override > public Object referenceInsert( String reference, Object value ) { > System.out.println( "Inserting : " + reference ); > return reference; > } > } ); > > System.out.println( "template1" ); > veloctiyEngine.evaluate( velocityContext, new StringWriter(), "template1", > template1 ); > System.out.println( "template2" ); > veloctiyEngine.evaluate( velocityContext, new StringWriter(), "template2", > template2 ); > } > } > > Here is the output: > > template1 > 09:43:39.568 [main] DEBUG org.apache.velocity - CommonsLogLogChute name is > 'org.apache.velocity' > 09:43:39.579 [main] DEBUG org.apache.velocity - Initializing Velocity, > Calling init()... > 09:43:39.579 [main] DEBUG org.apache.velocity - Starting Apache Velocity > v1.7 (compiled: 2010-11-19 12:14:37) > 09:43:39.580 [main] DEBUG org.apache.velocity - Default Properties File: > org/apache/velocity/runtime/defaults/velocity.properties > 09:43:39.580 [main] DEBUG org.apache.velocity - Trying to use logger class > org.apache.velocity.runtime.log.AvalonLogChute > 09:43:39.580 [main] DEBUG org.apache.velocity - Target log system for > org.apache.velocity.runtime.log.AvalonLogChute is not available > (java.lang.NoClassDefFoundError: org/apache/log/format/Formatter). Falling > back to next log system... > 09:43:39.581 [main] DEBUG org.apache.velocity - Trying to use logger class > org.apache.velocity.runtime.log.Log4JLogChute > 09:43:39.581 [main] DEBUG org.apache.velocity - Target log system for > org.apache.velocity.runtime.log.Log4JLogChute is not available > (java.lang.NoClassDefFoundError: org/apache/log4j/Layout). Falling back to > next log system... > 09:43:39.581 [main] DEBUG org.apache.velocity - Trying to use logger class > org.apache.velocity.runtime.log.CommonsLogLogChute > 09:43:39.582 [main] DEBUG org.apache.velocity - Using logger class > org.apache.velocity.runtime.log.CommonsLogLogChute > 09:43:39.606 [main] DEBUG org.apache.velocity - ResourceLoader instantiated: > org.apache.velocity.runtime.resource.loader.FileResourceLoader > 09:43:39.610 [main] DEBUG org.apache.velocity - Do unicode file > recognition: false > 09:43:39.611 [main] DEBUG org.apache.velocity - FileResourceLoader : adding > path '.' > 09:43:39.724 [main] DEBUG org.apache.velocity - ResourceCache: initialized > (class org.apache.velocity.runtime.resource.ResourceCacheImpl) with class > java.util.Collections$SynchronizedMap cache map. > 09:43:39.728 [main] DEBUG org.apache.velocity - Loaded System Directive: > org.apache.velocity.runtime.directive.Stop > 09:43:39.732 [main] DEBUG org.apache.velocity - Loaded System Directive: > org.apache.velocity.runtime.directive.Define > 09:43:39.733 [main] DEBUG org.apache.velocity - Loaded System Directive: > org.apache.velocity.runtime.directive.Break > 09:43:39.735 [main] DEBUG org.apache.velocity - Loaded System Directive: > org.apache.velocity.runtime.directive.Evaluate > 09:43:39.737 [main] DEBUG org.apache.velocity - Loaded System Directive: > org.apache.velocity.runtime.directive.Literal > 09:43:39.745 [main] DEBUG org.apache.velocity - Loaded System Directive: > org.apache.velocity.runtime.directive.Macro > 09:43:39.759 [main] DEBUG org.apache.velocity - Loaded System Directive: > org.apache.velocity.runtime.directive.Parse > 09:43:39.764 [main] DEBUG org.apache.velocity - Loaded System Directive: > org.apache.velocity.runtime.directive.Include > 09:43:39.769 [main] DEBUG org.apache.velocity - Loaded System Directive: > org.apache.velocity.runtime.directive.Foreach > 09:43:40.229 [main] DEBUG org.apache.velocity - Created '20' parsers. > 09:43:40.260 [main] DEBUG org.apache.velocity - Velocimacro : > "velocimacro.library" is not set. Trying default library: > VM_global_library.vm > 09:43:40.260 [main] DEBUG org.apache.velocity - Velocimacro : Default > library not found. > 09:43:40.260 [main] DEBUG org.apache.velocity - Velocimacro : allowInline = > true : VMs can be defined inline in templates > 09:43:40.260 [main] DEBUG org.apache.velocity - Velocimacro : > allowInlineToOverride = false : VMs defined inline may NOT replace previous > VM definitions > 09:43:40.261 [main] DEBUG org.apache.velocity - Velocimacro : > allowInlineLocal = false : VMs defined inline will be global in scope if > allowed. > 09:43:40.261 [main] DEBUG org.apache.velocity - Velocimacro : autoload off : > VM system will not automatically reload global library macros > Inserting : ${otherVariable} > template2 > > > Why is it never called on any inserted variable in the template2? > > Jean-Francois Croteau > jfcrot...@8d.com > Développeur Java > > 8D Technologies Inc. > 416, de Maisonneuve Ouest, suite 406 > Montréal, Québec, Canada, H3A 1L2 > Tél: 514-906-1212 Poste 339 > Fax: 514-906-1214 > http://www.8D.com/ > --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscr...@velocity.apache.org For additional commands, e-mail: user-h...@velocity.apache.org