Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #198 (closed enhancement: fixed)

Opened 13 years ago

Last modified 12 years ago

[PATCH] relative template declaration

Reported by: dwayne@… Owned by: anonymous
Priority: normal Milestone: 0.9
Component: TurboGears Version:
Severity: normal Keywords:
Cc:

Description

I submit a patch to be able to declare template in expose decorator without declare the local package : before

@turbogears.expose(html="myproject.templates.welcome") def index(self):

...

after

@turbogears.expose(html=".templates.welcome") def index(self):

...

=> less work when :

  • duplicate a webapp
  • refactore package (rename, restructure in several package, ...)
  • copy/paste samples

the function to modify is _execute_func in turbogears.controllers.py

Index: turbogears/controllers.py =================================================================== --- turbogears/controllers.py (révision 293) +++ turbogears/controllers.py (copie de travail) @@ -220,5 +220,7 @@

output = _call_with_errors(func, self, *args, kw)

else:

output = func(self, *args, kw)

+ if html and html.startswith('.'): + html = func.module[:func.module.rfind('.')]+html

return _process_output(tg_format, output, html)

Change History

comment:1 Changed 13 years ago by anonymous

  • Type changed from defect to enhancement

comment:2 Changed 13 years ago by anonymous

(I reformat)

I submit a patch to be able to declare template in expose decorator without declare the local package :

before
    @turbogears.expose(html="myproject.templates.welcome")
    def index(self):
        ...
after
    @turbogears.expose(html=".templates.welcome")
    def index(self):
        ...

=> less work when :
 * duplicate a webapp
 * refactore package (rename, restructure in several package, ...)
 * copy/paste samples

the function to modify is _execute_func in turbogears.controllers.py

Index: turbogears/controllers.py ===================================================================
--- turbogears/controllers.py (révision 293)
+++ turbogears/controllers.py (copie de travail)
@@ -220,5 +220,7 @@
             output = _call_with_errors(func, self, *args, **kw)
     else:
         output = func(self, *args, **kw)
+    if html and html.startswith('.'):
+        html = func.__module__[:func.__module__.rfind('.')]+html
     return _process_output(tg_format, output, html)

comment:3 Changed 13 years ago by michele

  • Summary changed from relative template declaration to [PATCH] relative template declaration

comment:4 Changed 13 years ago by kevin

  • Status changed from new to closed
  • Resolution set to fixed
  • Milestone set to 0.9

Applied in [322]. Thanks!

comment:5 Changed 13 years ago by dwayne@…

  • Status changed from closed to reopened
  • Resolution fixed deleted

I enhance the patch to support declaration of template via tg_template key in the returned dict, and add two test cases

Thanks

David Bernard

Index: turbogears/controllers.py
===================================================================
--- turbogears/controllers.py	(révision 457)
+++ turbogears/controllers.py	(copie de travail)
@@ -27,8 +27,6 @@
     @param template: HTML template to use
     """
     if isinstance(output, dict):
-        if output.has_key("tg_template"):
-            template=output.pop("tg_template")
         if output.has_key("tg_format"):
             tg_format=output.pop("tg_format")
         if not tg_format:
@@ -257,6 +255,8 @@
 def _execute_func(self, func, tg_format,
                   html, fragment, *args, **kw):
     output = func(self, *args, **kw)
+    if isinstance(output, dict) and output.has_key("tg_template"):
+        html = output.pop("tg_template")
     if html and html.startswith("."):
         html = func.__module__[:func.__module__.rfind('.')]+html
     return _process_output(tg_format, output, html, fragment)
Index: turbogears/tests/test_controllers.py+++ turbogears/tests/test_controllers.py	(copie de travail)
@@ -40,6 +40,15 @@
             tg_template="turbogears.tests.simple")
     returnedtemplate = turbogears.expose()(returnedtemplate)
         
+    def returnedtemplate_short(self):
+        return dict(title="Foobar", mybool=False, someval="foo",
+            tg_template=".simple")
+    returnedtemplate_short = turbogears.expose()(returnedtemplate_short)
+
+    def exposetemplate_short(self):
+        return dict(title="Foobar", mybool=False, someval="foo")
+    exposetemplate_short = turbogears.expose(html=".simple")(exposetemplate_short)
+
     def istrue(self, value):
         self.value = value
         return str(value)
@@ -173,6 +182,14 @@
         print data
         assert "<HTML>" in data
         
+    def test_returnedTemplateShort(self):
+        testutil.createRequest("/returnedtemplate_short")
+        assert "Paging all foo" in cherrypy.response.body[0]
+
+    def test_exposeTemplateShort(self):
+        testutil.createRequest("/exposetemplate_short")
+        assert "Paging all foo" in cherrypy.response.body[0]
+
     def test_validation(self):
         "Data can be converted and validated"
         testutil.createRequest("/istrue?value=true")

comment:6 Changed 13 years ago by kevin

  • Status changed from reopened to closed
  • Resolution set to fixed

committed in [464]. Thanks!

Note: See TracTickets for help on using tickets.