[Checkins] SVN: five.pt/trunk/src/five/pt/ Implement pt_render for TTW templates
Leonardo Rochael Almeida
leorochael at gmail.com
Wed Feb 23 09:40:37 EST 2011
Log message for revision 120543:
Implement pt_render for TTW templates
Changed:
U five.pt/trunk/src/five/pt/pagetemplate.py
U five.pt/trunk/src/five/pt/patches.py
U five.pt/trunk/src/five/pt/tests/test_persistenttemplate.py
-=-
Modified: five.pt/trunk/src/five/pt/pagetemplate.py
===================================================================
--- five.pt/trunk/src/five/pt/pagetemplate.py 2011-02-23 14:22:09 UTC (rev 120542)
+++ five.pt/trunk/src/five/pt/pagetemplate.py 2011-02-23 14:40:36 UTC (rev 120543)
@@ -23,6 +23,9 @@
from .expressions import PythonExpr as SecurePythonExpr
+EXTRA_CONTEXT_KEY = '__five_pt_extra_context'
+
+
def get_physical_root(context):
method = aq_get(context, 'getPhysicalRoot', None)
if method is not None:
@@ -72,11 +75,13 @@
macro, parameters=context, **kw)
def _pt_get_context(self, instance, request, kwargs={}):
- if instance is None:
- namespace = {}
- else:
+ extra_context = kwargs.pop(EXTRA_CONTEXT_KEY, {})
+ namespace = dict(self.utility_builtins)
+
+ if instance is not None:
+ # instance namespace overrides utility_builtins
context = aq_parent(instance)
- namespace = dict(
+ namespace.update(
context=context,
request=request or aq_get(instance, 'REQUEST', None),
template=self,
@@ -91,8 +96,8 @@
DateTime=DateTime,
options=kwargs)
- for name, value in self.utility_builtins.items():
- namespace.setdefault(name, value)
+ # extra_context (from pt_render()) overrides the default namespace
+ namespace.update(extra_context)
return namespace
Modified: five.pt/trunk/src/five/pt/patches.py
===================================================================
--- five.pt/trunk/src/five/pt/patches.py 2011-02-23 14:22:09 UTC (rev 120542)
+++ five.pt/trunk/src/five/pt/patches.py 2011-02-23 14:40:36 UTC (rev 120543)
@@ -20,6 +20,7 @@
from five.pt.pagetemplate import ViewPageTemplateFile
from five.pt.pagetemplate import BaseTemplate
from five.pt.pagetemplate import BaseTemplateFile
+from five.pt.pagetemplate import EXTRA_CONTEXT_KEY
from Acquisition import aq_base
from Acquisition import aq_parent
@@ -87,9 +88,19 @@
return template
def call_template(self, *args, **kw):
+ # avoid accidental exposure of the extra context
+ kw.pop(EXTRA_CONTEXT_KEY, None)
template = self._get_five_pt_template()
return template(self, *args, **kw)
+def pt_render(self, source=False, extra_context=None):
+ if source:
+ return self._text
+ if extra_context is None:
+ extra_context = {}
+ template = self._get_five_pt_template()
+ return template(self, **{EXTRA_CONTEXT_KEY:extra_context})
+
def _get_five_pt_template_file_wrapped(self, *args, **kw):
template = getattr(self, '_v_template', _marker)
if template is _marker:
@@ -117,6 +128,7 @@
PageTemplateFile.macros = property(get_macros)
ZopePageTemplate._get_five_pt_template = _get_five_pt_template_wrapped
ZopePageTemplate._bindAndExec = call_template
+ZopePageTemplate.pt_render = pt_render
ZopePageTemplate.macros = ComputedAttribute(get_macros, 1)
try:
Modified: five.pt/trunk/src/five/pt/tests/test_persistenttemplate.py
===================================================================
--- five.pt/trunk/src/five/pt/tests/test_persistenttemplate.py 2011-02-23 14:22:09 UTC (rev 120542)
+++ five.pt/trunk/src/five/pt/tests/test_persistenttemplate.py 2011-02-23 14:40:36 UTC (rev 120543)
@@ -37,6 +37,19 @@
</tal:block>
""".strip()
+options_capture_update_base = """
+<metal:use use-macro="context/macro_outer/macros/master">
+ <metal:fills fill-slot="main_slot">
+ <tal:block define="dummy python: capture.update(%s)" />
+ </metal:fills>
+</metal:use>
+""".strip()
+
+def generate_capture_source(names):
+ params = ", ".join("%s=%s" % (name, name)
+ for name in names)
+ return options_capture_update_base % (params,)
+
class TestPersistent(ZopeTestCase):
def afterSetUp(self):
from Products.Five import zcml
@@ -64,3 +77,23 @@
inner = self._makeOne('macro_inner', macro_inner)
result = inner().strip()
self.assertEqual(result, u'Inner Slot')
+
+ def test_pt_render_with_macro(self):
+ # The pt_render method of ZopePageTemplates allows rendering the
+ # template with an expanded (and overriden) set of context
+ # variables.
+ # It's also used to retrieve the unrendered source for TTW
+ # editing purposes.
+ # Lets test with some common and some unique variables:
+ extra_context = dict(form=object(),
+ context=self.folder,
+ here=object(),)
+ capture = dict((name, None) for name in extra_context)
+ source = generate_capture_source(capture)
+ self._makeOne('macro_outer', macro_outer)
+ template = self._makeOne('test_pt_render', source)
+ self.assertEqual(template.pt_render(source=True), source)
+ extra_context['capture'] = capture
+ template.pt_render(extra_context=extra_context)
+ del extra_context['capture']
+ self.assertEquals(extra_context, capture)
More information about the checkins
mailing list