Author: chrisz
Date: Sun Jan 27 09:03:01 2008
New Revision: 4059
URL: http://trac.turbogears.org/changeset/4059

Log:
Merged [3968] from 1.0 to 1.1 branch: Improved error message when rendering 
templates too early (#1277).

Modified:
   branches/1.1/turbogears/view/base.py
   branches/1.1/turbogears/widgets/base.py
   branches/1.1/turbogears/widgets/tests/test_widgets.py

Modified: branches/1.1/turbogears/view/base.py
==============================================================================
--- branches/1.1/turbogears/view/base.py        (original)
+++ branches/1.1/turbogears/view/base.py        Sun Jan 27 09:03:01 2008
@@ -348,6 +348,13 @@
     return root_vars
 
 def load_engines():
+    """Load and initialize all templating engines.
+
+    This is called during startup after the configuration has been loaded.
+    You can call this earlier if you need the engines before startup;
+    the engines will then be reloaded with the custom configuration later.
+
+    """
     conf = turbogears.config.get
     engine_options = {
         "cheetah.importhooks": conf("cheetah.importhooks", False),

Modified: branches/1.1/turbogears/widgets/base.py
==============================================================================
--- branches/1.1/turbogears/widgets/base.py     (original)
+++ branches/1.1/turbogears/widgets/base.py     Sun Jan 27 09:03:01 2008
@@ -271,8 +271,14 @@
         self.update_params(params)
         # update_data has been deprecated
         self.update_data(params)
-
-        output = view.engines.get('kid').transform(params, self.template_c)
+        try:
+            transform = view.engines['kid'].transform
+        except (KeyError, AttributeError):
+            # this can happen if you render a widget before application startup
+            # when view.load_engines() has not yet been called
+            raise RuntimeError("Trying to render a widget,"
+                " but Kid templating engine is not yet loaded.")
+        output = transform(params, self.template_c)
         if request_available() and request.tg_template_enginename == 'genshi' \
                 and len(getattr(request, 'tg_widgets_path', [])) == 1:
             import genshi

Modified: branches/1.1/turbogears/widgets/tests/test_widgets.py
==============================================================================
--- branches/1.1/turbogears/widgets/tests/test_widgets.py       (original)
+++ branches/1.1/turbogears/widgets/tests/test_widgets.py       Sun Jan 27 
09:03:01 2008
@@ -2,7 +2,7 @@
 
 import cherrypy
 
-from turbogears import widgets, validators
+from turbogears import widgets, validators, view
 from turbogears.testutil import catch_validation_errors
 
 import sets
@@ -22,6 +22,20 @@
     global oldrequest
     cherrypy.request = oldrequest
 
+def test_rendering_without_engine():
+    """Helpful error when rendering widgets with no templating engine loaded"""
+    from turbogears import view
+    engines = view.engines
+    view.engines = {}
+    try:
+        widgets.CSSLink("foo")()
+    except Exception, msg:
+        msg = str(msg)
+    else:
+        msg = 'No error'
+    view.engines = engines
+    assert 'templating engine is not yet loaded' in msg
+
 def test_label():
     """Tests simple labels"""
     label = widgets.Label("foo")

Reply via email to