[Zope3-checkins] CVS: Zope3/src/zope/app/pagetemplate - engine.py:1.19

Stephan Richter srichter at cosmos.phy.tufts.edu
Thu Aug 21 11:19:58 EDT 2003


Update of /cvs-repository/Zope3/src/zope/app/pagetemplate
In directory cvs.zope.org:/tmp/cvs-serv8485/src/zope/app/pagetemplate

Modified Files:
	engine.py 
Log Message:
Final HEAD checkin of "Inline Code Support in TAL". For detailed messages 
during the development, see "srichter-inlinepython-branch". I tested the 
code with both, Python 2.2.3 and Python 2.3 and all works fine.

Here an example of what you can do:

  <script type="text/server-python">
    print "Hello World!"
  </script>

and

 <p tal:script="text/server-python">
   print "Hello World!"
 </p>

A more elaborate example would be:

<html><body>
  <script type="text/server-python">
    global x
    x = "Hello World!"
  </script>
  <b tal:content="x" />
</body></html>

This support is currently only available in "Templated Pages" after you 
activate the hook using the "Inline Code" screen.


=== Zope3/src/zope/app/pagetemplate/engine.py 1.18 => 1.19 ===
--- Zope3/src/zope/app/pagetemplate/engine.py:1.18	Tue Jul  1 13:30:59 2003
+++ Zope3/src/zope/app/pagetemplate/engine.py	Thu Aug 21 10:19:26 2003
@@ -37,9 +37,14 @@
 from zope.security.builtins import RestrictedBuiltins
 from zope.i18n.translate import Translator
 
+from zope.app import zapi
+from zope.app.i18n import ZopeMessageIDFactory as _
 from zope.app.traversing.adapters import Traverser
 
 
+class InlineCodeError(Exception):
+    pass
+
 def zopeTraverser(object, path_items, econtext):
     """Traverses a sequence of names, first trying attributes then items.
     """
@@ -88,6 +93,33 @@
         translator = Translator(request.locale, domain, self.context)
         return translator.translate(msgid, mapping, default=default)
 
+    evaluateInlineCode = False
+
+    def evaluateCode(self, lang, code):
+        if not self.evaluateInlineCode:
+            raise InlineCodeError, \
+                  _('Inline Code Evaluation is deactivated, which means that '
+                    'you cannot have inline code snippets in your Page '
+                    'Template. Activate Inline Code Evaluation and try again.')
+        service = zapi.queryService(self.context, 'Interpreter')
+        if service is None:
+            raise InlineCodeError, \
+                  _('No interpreter service was found. This should never '
+                    'happen, since Zope defines a global interpreter service.')
+        interpreter = service.queryInterpreter(lang)
+        if interpreter is None:
+            error = _('No interpreter named "${lang_name}" was found.')
+            error.mapping = {'lang_name': lang}
+            raise InlineCodeError, error
+                  
+        globals = self.vars.copy()
+        result = interpreter.evaluateRawCode(code, globals)
+        # Add possibly new global variables.
+        old_names = self.vars.keys()
+        for name, value in globals.items():
+            if name not in old_names:
+                self.setGlobal(name, value)
+        return result
 
 class ZopeEngine(ExpressionEngine):
 




More information about the Zope3-Checkins mailing list