Author: dmeyer
Date: Sun Jan 29 11:43:46 2006
New Revision: 1138
Modified:
trunk/cherrypy/src/__init__.py
trunk/cherrypy/src/controller.py
Log:
add Template class to use a template outside an exposed function
Modified: trunk/cherrypy/src/__init__.py
==============================================================================
--- trunk/cherrypy/src/__init__.py (original)
+++ trunk/cherrypy/src/__init__.py Sun Jan 29 11:43:46 2006
@@ -34,4 +34,4 @@
# kaa.cherrypy imports
from config import config
from server import start
-from controller import expose
+from controller import expose, Template
Modified: trunk/cherrypy/src/controller.py
==============================================================================
--- trunk/cherrypy/src/controller.py (original)
+++ trunk/cherrypy/src/controller.py Sun Jan 29 11:43:46 2006
@@ -31,14 +31,14 @@
#
# -----------------------------------------------------------------------------
-__all__ = [ 'expose' ]
+__all__ = [ 'expose', 'Template' ]
# python imports
import types
import os
# kaa imports
-from kaa.notifier import MainThreadCallback
+from kaa.notifier import MainThreadCallback, is_mainthread
engines = []
@@ -54,7 +54,7 @@
class KidTemplate(object):
name = 'kid'
-
+
def detect(self, template):
if type(template) == str and template.endswith('.kid'):
return True
@@ -67,7 +67,7 @@
if type(template) == types.ModuleType:
return template.Template(**args).serialize(output='xhtml')
return kid.Template(file=template,
**args).serialize(output='xhtml')
-
+
engines.append(KidTemplate())
except ImportError:
@@ -83,7 +83,7 @@
class CheetahTemplate(object):
name = 'cheetah'
-
+
def detect(self, template):
if type(template) == str and template.endswith('.tmpl'):
return True
@@ -92,7 +92,7 @@
template.__KaaCherrypyTemplate = getattr(template, c)
return True
return False
-
+
def parse(self, template, args):
if type(template) == types.ModuleType:
return str(template.__KaaCherrypyTemplate(searchList=[args]))
@@ -122,7 +122,7 @@
return e
raise RuntimeError('unable to detect template engine for %s' % template)
-
+
def expose(template=None, engine=None, mainloop=True):
"""
Expose function / wrapper. It adds the possiblity to define a template
@@ -136,7 +136,7 @@
def newfunc(self, *args, **kwargs):
_function = _execute_func
- if mainloop:
+ if mainloop and not is_mainthread():
_function = MainThreadCallback(_execute_func)
_function.set_async(False)
return _function(self, template, engine, func, args, kwargs)
@@ -152,6 +152,7 @@
return decorator
+
def _execute_func(self, filename, engine, func, args, kwargs):
"""
Helper function to call the function and handle kid. This whole function
@@ -160,3 +161,31 @@
if not filename:
return func(self, *args, **kwargs)
return engine.parse(filename, func(self, *args, **kwargs))
+
+
+
+
+class Template(object):
+ """
+ A class wrapping a template. It has a __call__ function to execute
+ the template and an exposed render function to use the template as
+ full working side in the webserver.
+ """
+ def __init__(self, template, engine=None):
+ self.engine = _get_engine(template, engine)
+ self.template = template
+
+
+ def __call__(self, **attributes):
+ """
+ Render the template with the given attributes.
+ """
+ return self.engine.parse(self.template, attributes)
+
+
+ @expose()
+ def render(self, **attributes):
+ """
+ Render the template with the given attributes.
+ """
+ return self.engine.parse(self.template, attributes)
-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems? Stop! Download the new AJAX search engine that makes
searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog