changeset 30:54539dff18ca

Allow access to the calling (outer) context. Start parse_args implementation.
author Mikhail Kryshen <mikhail@kryshen.net>
date Fri, 15 May 2009 03:07:16 +0400
parents 2c6edd9cf677
children 3e77076621a8
files src/kryshen/tema/Context.java src/kryshen/tema/TemplateParser.java test/parse_args.tema
diffstat 3 files changed, 56 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/kryshen/tema/Context.java	Thu May 14 18:55:32 2009 +0400
+++ b/src/kryshen/tema/Context.java	Fri May 15 03:07:16 2009 +0400
@@ -21,8 +21,11 @@
 package kryshen.tema;
 
 import java.io.File;
+import java.io.IOException;
+import java.io.Writer;
 import java.util.HashMap;
 import java.util.Map;
+import kryshen.tema.io.TemplateReader;
 
 /**
  * Tema template context.
@@ -114,7 +117,32 @@
         
         return false;
     }
-    
+
+    int parseInContext(TemplateReader in, Writer out, Context outer)
+            throws IOException, TemplateException {
+
+        final String OUTER = "outer";
+
+        boolean hadOuter = definitions.containsKey(OUTER);
+        Object oldOuter = null;
+        if (hadOuter) {
+            oldOuter = definitions.get(OUTER);
+        }
+        
+        definitions.put(OUTER, outer);
+
+        try {
+            TemplateParser parser = new TemplateParser(this);
+            return parser.parse(in, out);
+        } finally {
+            if (hadOuter) {
+                definitions.put(OUTER, oldOuter);
+            } else {
+                definitions.remove(OUTER);
+            }
+        }
+    }
+
     /**
      * Returns base directory which should be used to resolve
      * relative path names in template.
--- a/src/kryshen/tema/TemplateParser.java	Thu May 14 18:55:32 2009 +0400
+++ b/src/kryshen/tema/TemplateParser.java	Fri May 15 03:07:16 2009 +0400
@@ -433,11 +433,10 @@
                 return 0;
             }
 
-            TemplateParser parser = new TemplateParser((Context) value);
-            return parser.parse(
-                    new TemplateReader(new StringReader(code),
-                    fdp.getTemplateReader()),
-                    out);
+            TemplateReader in = new TemplateReader(new StringReader(code),
+                    fdp.getTemplateReader());
+
+            return ((Context) value).parseInContext(in, out, context);
         }
 
         return parseValue(value, out);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/parse_args.tema	Fri May 15 03:07:16 2009 +0400
@@ -0,0 +1,23 @@
+<%define#parse_args
+  <%set:stop <%next_arg\%>%>
+  <%while#
+    <%super#<%has_more_data\%>%>
+    <%and not equal:<%set:name <%super#<%next_arg\%>%>%> <%stop\%>%>
+    <%and super#<%set:arg_<%outer#<%name\%>%> <%next_arg\%>%>%>
+  %>
+%>
+
+<%define#test
+  <%silent parse_args:g%>\
+  *<%arg_a\%>*
+  *<%arg_b\%>*
+  *<%arg_g\%>*
+  **<%data\%>**
+%>
+
+<%test:
+  a 1
+  b 2
+  g 3 4 5 6
+%>
+