diff --git a/web/README.txt b/web/README.txt new file mode 100644 index 0000000..2c964ec --- /dev/null +++ b/web/README.txt @@ -0,0 +1,27 @@ +=============================== +All about Views, Templates, ... +=============================== + +$Id$ + + +Generic Views +============= + +OK, there aren't really generic views. Already the first implementation we +want to look at is a specic one: It is based on Zope Page Templates and +uses the classic CMF/Zope 3 approach: The template belonging to a view +calls a `main` macro and fills a slot there. But at least the template +implementation is decoupled from the view, so we are able to put a lot of +generic functionality into the view. + +But in order to make a ZPT work we need a Zope-compatible request, so we use +the standard Zope 3 TestRequest. + + >>> from zope.publisher.browser import TestRequest + + >>> from cybertools.web.view import View + >>> view = View(None, TestRequest()) + >>> view.render() + u'.........' + diff --git a/web/__init__.py b/web/__init__.py new file mode 100644 index 0000000..38314f3 --- /dev/null +++ b/web/__init__.py @@ -0,0 +1,3 @@ +""" +$Id$ +""" diff --git a/web/content.pt b/web/content.pt new file mode 100644 index 0000000..ae87358 --- /dev/null +++ b/web/content.pt @@ -0,0 +1,15 @@ + + + + + + +
+ This is the body +
+
+ + + + diff --git a/web/template.py b/web/template.py new file mode 100644 index 0000000..05cce5c --- /dev/null +++ b/web/template.py @@ -0,0 +1,39 @@ +# +# Copyright (c) 2006 Helmut Merz helmutm@cy55.de +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +""" +Generic template base class (if fact it is the ZPT-based type...). + +$Id$ +""" + +from zope.app.pagetemplate import ViewPageTemplateFile + + +class Template(object): + + zpt = ViewPageTemplateFile('content.pt') + + def __init__(self, view): + self.view = view + self.context = view.context + self.request = view.request + + def render(self, *args, **kw): + return self.zpt(*args, **kw) + diff --git a/web/tests.py b/web/tests.py new file mode 100755 index 0000000..9b79252 --- /dev/null +++ b/web/tests.py @@ -0,0 +1,30 @@ +#! /usr/bin/python + +""" +Tests for the 'cybertools.index' package. + +$Id$ +""" + +import unittest, doctest +from zope.testing.doctestunit import DocFileSuite + +from cybertools.web import view + + +class Test(unittest.TestCase): + "Basic tests for the index package." + + def testBasicStuff(self): + pass + + +def test_suite(): + flags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS + return unittest.TestSuite(( + DocFileSuite('README.txt', optionflags=flags), + unittest.makeSuite(Test), + )) + +if __name__ == '__main__': + unittest.main(defaultTest='test_suite') diff --git a/web/view.py b/web/view.py new file mode 100644 index 0000000..06e7cae --- /dev/null +++ b/web/view.py @@ -0,0 +1,46 @@ +# +# Copyright (c) 2006 Helmut Merz helmutm@cy55.de +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +""" +Generic view base class. + +$Id$ +""" + +from cybertools.web.template import Template + + +class View(object): + + templateFactory = Template + + _template = None + + def __init__(self, context, request): + self.context = context + self.request = request + + @property + def template(self): + if self._template is None and self.templateFactory: + self._template = self.templateFactory(self) + return self._template + + def render(self, *args, **kw): + return self.template.render(*args, **kw) +