[Templates] FOREACH blah IN [blah, de, blah, blah]

Andy Wardley abw@andywardley.com
Fri, 25 Apr 2003 07:56:57 +0100


I received an email suggesting that it should be possible to write

  [% FOREACH item = list %]

as

  [% FOREACH item IN list %]

I think this is a Good Idea.  The following patch implements it.

--- parser/Grammar.pm.skel	2002/05/03 12:08:21	2.9
+++ parser/Grammar.pm.skel	2003/04/25 06:44:11
@@ -51,7 +51,7 @@
 @RESERVED = qw( 
 	GET CALL SET DEFAULT INSERT INCLUDE PROCESS WRAPPER BLOCK END
 	USE PLUGIN FILTER MACRO PERL RAWPERL TO STEP AND OR NOT DIV MOD
-	IF UNLESS ELSE ELSIF FOR NEXT WHILE SWITCH CASE META
+	IF UNLESS ELSE ELSIF FOR NEXT WHILE SWITCH CASE META IN
 	TRY THROW CATCH FINAL LAST RETURN STOP CLEAR VIEW DEBUG
     );
 
--- parser/Parser.yp	2002/08/08 11:52:24	2.15
+++ parser/Parser.yp	2003/04/25 06:44:12
@@ -180,6 +180,7 @@
 ;
 
 loopvar:    IDENT ASSIGN term args  { [ @_[1, 3, 4] ]                     }
+        |   IDENT IN term args      { [ @_[1, 3, 4] ]                     }
 	|   term args               { [ 0, @_[1, 2] ]                     }
 ;
 
If you apply the patch then be sure to run parser/yc to rebuild the grammar
and expect a shift/reduce and a reduce/reduce error.

So, should this patch go into the core?  The only possible side effect is
that IN (and possibly also 'in' if we allow the lower case version) would
become reserved words and would clash with any existing variables called
'IN' (or 'in').  But I suspect that 'IN' is not a popular name for variables 
anyway, so it's probably not a real problem.

Any objections, suggestions, thoughts?

A