Re: [Zope] Passing args to PageTemplateFile instances
2005/9/23, Anders Bruun Olsen <anders@bruun-olsen.net>:
Why the extra self-wrapping. Do it this way: def testit(self, REQUEST=None): return self.test(self, self.REQUEST, something="blah") Then you'll be able to use: <span tal:replace="options/something"/>
Thanks, I have now tried that, but it still does not work. I still get:
Error Type: KeyError Error Value: 'something'
Please press "Reply-All" and not just to me. That is the correct way of calling PageTemplateFile objects in python code. I've got it working in many places. The error must be either a new zope bug or something else being wrong. Is your setup as simple as you've described in the email? What happens when you in that TAL do something like: <br tal:replace="python:str(options)" /> Do the other namespaces work in the page template? Eg. <br tal:replace="python:len(request.form.keys())" /> or <br tal:replace="python:context.absolute_url_path()" /> -- Peter Bengtsson, work www.fry-it.com home www.peterbe.com hobby www.issuetrackerproduct.com
On Fri, Sep 23, 2005 at 12:06:00PM +0100, Peter Bengtsson wrote:
Please press "Reply-All" and not just to me.
Sorry about that.
That is the correct way of calling PageTemplateFile objects in python code. I've got it working in many places. The error must be either a new zope bug or something else being wrong. Is your setup as simple as you've described in the email?
Yes. This is the class I am using: class TestProd(SimpleItem): """A TestProd object""" meta_type = "TestProd" manage_options = ( {"label": "Edit", "action": "manage_main"}, {"label": "View", "action": "index_html"}) manage_main = PageTemplateFile("templates/mainTestProd", globals()) index_html = PageTemplateFile("templates/indexTestProd", globals()) test = PageTemplateFile("templates/test", globals()) def __init__(self, id, title): self.id = id self.title = title def testit(self, REQUEST=None): return self.test(self, self.REQUEST, something="blah") And the "test" template: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html;charset=ISO-8859-1" /> <meta name="copyright" content="© 2004 & Anders Bruun Olsen <anders(AT)bruun-olsen(DOT)net" /> <link rel="stylesheet" type="text/css" href="css" /> <title tal:content="container/title_or_id">TITLE</title> </head> <body> <div> <p>Welcome to <tal:block tal:replace="container/title_or_id"/></p> <p tal:content="python:str(options)"/> <p tal:content="python:len(request.form.keys())"/> <p tal:content="python:context.absolute_url_path()"/> </div> </body> </html> I have added it to the rootfolder in ZMI with the ID "testing".
What happens when you in that TAL do something like: <br tal:replace="python:str(options)" />
Gives: {'args': ()}
Do the other namespaces work in the page template? Eg. <br tal:replace="python:len(request.form.keys())" /> or <br
Gives: 0
tal:replace="python:context.absolute_url_path()" />
Gives: /testing -- Anders -----BEGIN GEEK CODE BLOCK----- Version: 3.12 GCS/O d--@ s:+ a-- C++ UL+++$ P++ L+++ E- W+ N(+) o K? w O-- M- V PS+ PE@ Y+ PGP+ t 5 X R+ tv+ b++ DI+++ D+ G e- h !r y? ------END GEEK CODE BLOCK------ PGPKey: http://random.sks.keyserver.penguin.de:11371/pks/lookup?op=get&search=0xD4DE...
RESULT: ------------------------------------------- Welcome to testprod {'args': (<TestProd at /test/testprod used for /test/testprod>, <HTTPRequest, URL=http://localhost:8080/test/testprod/test_testprod>), 'something': 'blah'} 0 /test/testprod -------------------------------------------- http://www.peterbe.com/test/TestProd.tgz So it works just fine in Zope 2.8.0 I suspect that the code you paste was idented wrong because the def testit() function looked like it was part of the __init__ function. 2005/9/23, Anders Bruun Olsen <anders@bruun-olsen.net>:
On Fri, Sep 23, 2005 at 12:06:00PM +0100, Peter Bengtsson wrote:
Please press "Reply-All" and not just to me.
Sorry about that.
That is the correct way of calling PageTemplateFile objects in python code. I've got it working in many places. The error must be either a new zope bug or something else being wrong. Is your setup as simple as you've described in the email?
Yes. This is the class I am using:
class TestProd(SimpleItem): """A TestProd object""" meta_type = "TestProd" manage_options = ( {"label": "Edit", "action": "manage_main"}, {"label": "View", "action": "index_html"})
manage_main = PageTemplateFile("templates/mainTestProd", globals()) index_html = PageTemplateFile("templates/indexTestProd", globals()) test = PageTemplateFile("templates/test", globals())
def __init__(self, id, title): self.id = id self.title = title
def testit(self, REQUEST=None): return self.test(self, self.REQUEST, something="blah")
And the "test" template:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html;charset=ISO-8859-1" /> <meta name="copyright" content="© 2004 & Anders Bruun Olsen <anders(AT)bruun-olsen(DOT)net" /> <link rel="stylesheet" type="text/css" href="css" /> <title tal:content="container/title_or_id">TITLE</title> </head> <body> <div> <p>Welcome to <tal:block tal:replace="container/title_or_id"/></p> <p tal:content="python:str(options)"/> <p tal:content="python:len(request.form.keys())"/> <p tal:content="python:context.absolute_url_path()"/> </div> </body> </html>
I have added it to the rootfolder in ZMI with the ID "testing".
What happens when you in that TAL do something like: <br tal:replace="python:str(options)" />
Gives: {'args': ()}
Do the other namespaces work in the page template? Eg. <br tal:replace="python:len(request.form.keys())" /> or <br
Gives: 0
tal:replace="python:context.absolute_url_path()" />
Gives: /testing
-- Anders -----BEGIN GEEK CODE BLOCK----- Version: 3.12 GCS/O d--@ s:+ a-- C++ UL+++$ P++ L+++ E- W+ N(+) o K? w O-- M- V PS+ PE@ Y+ PGP+ t 5 X R+ tv+ b++ DI+++ D+ G e- h !r y? ------END GEEK CODE BLOCK------ PGPKey: http://random.sks.keyserver.penguin.de:11371/pks/lookup?op=get&search=0xD4DE...
-- Peter Bengtsson, work www.fry-it.com home www.peterbe.com hobby www.issuetrackerproduct.com
On Fri, Sep 23, 2005 at 12:39:31PM +0100, Peter Bengtsson wrote:
RESULT: ------------------------------------------- Welcome to testprod {'args': (<TestProd at /test/testprod used for /test/testprod>, <HTTPRequest, URL=http://localhost:8080/test/testprod/test_testprod>), 'something': 'blah'} 0 /test/testprod -------------------------------------------- http://www.peterbe.com/test/TestProd.tgz So it works just fine in Zope 2.8.0 I suspect that the code you paste was idented wrong because the def testit() function looked like it was part of the __init__ function.
Yes, the paste was wrong, but there was more than that, I had my editor slightly misconfigured, which meant that the testit method was done with tab-indention and the rest of the file with space-indention. After I fixed that, it worked correctly and "something" shows up in options. I am absolutely stumped by why an indention problem can cause such a weird bug. I would have expected Zope/python to spew out an error telling me that indention was fucked up. To just silently make things not work is definitely not nice. Anyway, thank you so much for your help! -- Anders -----BEGIN GEEK CODE BLOCK----- Version: 3.12 GCS/O d--@ s:+ a-- C++ UL+++$ P++ L+++ E- W+ N(+) o K? w O-- M- V PS+ PE@ Y+ PGP+ t 5 X R+ tv+ b++ DI+++ D+ G e- h !r y? ------END GEEK CODE BLOCK------ PGPKey: http://random.sks.keyserver.penguin.de:11371/pks/lookup?op=get&search=0xD4DE...
2005/9/23, Anders Bruun Olsen <anders@bruun-olsen.net>:
On Fri, Sep 23, 2005 at 12:39:31PM +0100, Peter Bengtsson wrote:
RESULT: ------------------------------------------- Welcome to testprod {'args': (<TestProd at /test/testprod used for /test/testprod>, <HTTPRequest, URL=http://localhost:8080/test/testprod/test_testprod>), 'something': 'blah'} 0 /test/testprod -------------------------------------------- http://www.peterbe.com/test/TestProd.tgz So it works just fine in Zope 2.8.0 I suspect that the code you paste was idented wrong because the def testit() function looked like it was part of the __init__ function.
Yes, the paste was wrong, but there was more than that, I had my editor slightly misconfigured, which meant that the testit method was done with tab-indention and the rest of the file with space-indention. After I fixed that, it worked correctly and "something" shows up in options. I am absolutely stumped by why an indention problem can cause such a weird bug. I would have expected Zope/python to spew out an error telling me that indention was fucked up. To just silently make things not work is definitely not nice.
Anyway, thank you so much for your help!
I'm still not convinced. The tabbing should either have led to a SyntaxError or to testit() becoming a nested method. If it was a nested method you wouldn't be able to reach it via the web like http://localhost:8080/testprod/testit But let's drop this now :) If you're struggling with getting your editor to only use spaces correctly you can ask the list for advise since there are many different python progammers here with the same experience. -- Peter Bengtsson, work www.fry-it.com home www.peterbe.com hobby www.issuetrackerproduct.com
participants (2)
-
Anders Bruun Olsen -
Peter Bengtsson