Issues

Issue #160 new

add source namespace to get_def() ?

Mike Bayer
repo owner created an issue

When render() is called, runtime will pull out the requested namespace after _populate_self_namespace is called and effectively run whatever named def relative to that namespace, such as self.

The feature would look like:

    class Template(object):
       # ...

       def get_def(self, name, via_namespace=None):
           # ...

Below is not the patch but this is the kind of tinkering we'd need

diff -r 6779081b32cfb237062da8bc623a27d47301ce4c mako/runtime.py
--- a/mako/runtime.py   Wed Jan 19 10:33:00 2011 -0500
+++ b/mako/runtime.py   Thu Feb 10 17:12:30 2011 -0500
@@ -575,6 +575,14 @@
                             **_kwargs_for_callable(callable_, data))
     return context._pop_buffer().getvalue()

+def _get_self(tmpl, callable_, args, data, as_unicode=False):
+    buf = util.StringIO()
+    context = Context(buf, **data)
+    context._outputting_as_unicode = as_unicode
+    context._with_template = tmpl
+    (inherit, lclcontext) = _populate_self_namespace(context, tmpl)
+    return buf, context._data['self']
+
 def _kwargs_for_callable(callable_, data):
     argspec = inspect.getargspec(callable_)
     # for normal pages, **pageargs is usually present
@@ -609,7 +617,7 @@
         # otherwise, call the actual rendering method specified
         (inherit, lclcontext) = _populate_self_namespace(context, tmpl.parent)
         _exec_template(callable_, context, args=args, kwargs=kwargs)
- 
+
 def _exec_template(callable_, context, args=None, kwargs=None):
     """execute a rendering callable given the callable, a
     Context, and optional explicit arguments
diff -r 6779081b32cfb237062da8bc623a27d47301ce4c mako/template.py
--- a/mako/template.py  Wed Jan 19 10:33:00 2011 -0500
+++ b/mako/template.py  Thu Feb 10 17:12:30 2011 -0500
@@ -304,7 +304,15 @@
                                 context, 
                                 *args, 
                                 **kwargs)
- 
+
+    def get_self(self, *args, **data):
+        return runtime._get_self(
+            self, 
+            self.callable_,
+            args, 
+            data
+        )
+
     def has_def(self, name):
         return hasattr(self.module, "render_%s" % name)