diff --git a/view/README.txt b/view/README.txt new file mode 100644 index 0000000..ab4b4c7 --- /dev/null +++ b/view/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. + +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.view.web.base import View + >>> view = View(None, TestRequest()) + >>> view.render() + u'< html...>............' + diff --git a/view/__init__.py b/view/__init__.py new file mode 100644 index 0000000..38314f3 --- /dev/null +++ b/view/__init__.py @@ -0,0 +1,3 @@ +""" +$Id$ +""" diff --git a/view/base.py b/view/base.py new file mode 100644 index 0000000..98e3553 --- /dev/null +++ b/view/base.py @@ -0,0 +1,36 @@ +# +# 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 zope.cachedescriptors.property import Lazy + + +class View(object): + + def __init__(self, context, request): + self.context = context + self.request = request + + def render(self, *args, **kw): + return '' + diff --git a/view/tests.py b/view/tests.py new file mode 100755 index 0000000..eca47c7 --- /dev/null +++ b/view/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.view.base 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/view/web/__init__.py b/view/web/__init__.py new file mode 100644 index 0000000..38314f3 --- /dev/null +++ b/view/web/__init__.py @@ -0,0 +1,3 @@ +""" +$Id$ +""" diff --git a/view/web/base.py b/view/web/base.py new file mode 100644 index 0000000..aaf32dc --- /dev/null +++ b/view/web/base.py @@ -0,0 +1,44 @@ +# +# 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 zope.cachedescriptors.property import Lazy +from cybertools.view import base +from cybertools.view.web.zpt.template import Template + + +class View(base.View): + + templateFactory = Template + + def __init__(self, context, request): + self.context = context + self.request = request + + @Lazy + def template(self): + return self.templateFactory(self) + + def render(self, *args, **kw): + return self.template.render(*args, **kw) + diff --git a/view/web/configure.zcml b/view/web/configure.zcml new file mode 100644 index 0000000..dd31ae9 --- /dev/null +++ b/view/web/configure.zcml @@ -0,0 +1,10 @@ + + + + + + + diff --git a/view/web/template.py b/view/web/template.py new file mode 100644 index 0000000..dfdb0a1 --- /dev/null +++ b/view/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. + +$Id$ +""" + +from zope.app.traversing.adapters import DefaultTraversable +from zope import component + + +class Template(object): + + def __init__(self, view): + self.view = view + self.context = view.context + self.request = view.request + + def render(self, *args, **kw): + return u'' + +component.provideAdapter(DefaultTraversable, (Template,)) diff --git a/view/web/zpt/__init__.py b/view/web/zpt/__init__.py new file mode 100644 index 0000000..38314f3 --- /dev/null +++ b/view/web/zpt/__init__.py @@ -0,0 +1,3 @@ +""" +$Id$ +""" diff --git a/view/web/zpt/configure.zcml b/view/web/zpt/configure.zcml new file mode 100644 index 0000000..c072812 --- /dev/null +++ b/view/web/zpt/configure.zcml @@ -0,0 +1,16 @@ + + + + + + + diff --git a/view/web/zpt/content.pt b/view/web/zpt/content.pt new file mode 100644 index 0000000..cfdc7c3 --- /dev/null +++ b/view/web/zpt/content.pt @@ -0,0 +1,15 @@ + + + + + + +
+ This is the body +
+
+ + + + diff --git a/view/web/zpt/main.pt b/view/web/zpt/main.pt new file mode 100644 index 0000000..4b56ac7 --- /dev/null +++ b/view/web/zpt/main.pt @@ -0,0 +1,54 @@ + + + + + + Powered by Zope 3 + + + + + + + + + + +
+
+ +
+
+ + + +
+ + + + Here comes the body + +
+ +
+ +
+ + + + + + +
+ diff --git a/view/web/zpt/template.py b/view/web/zpt/template.py new file mode 100644 index 0000000..74ad82a --- /dev/null +++ b/view/web/zpt/template.py @@ -0,0 +1,51 @@ +# +# 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 +# + +""" +ZPT-based template base class. + +$Id$ +""" + +from zope.app.pagetemplate import ViewPageTemplateFile +from zope.cachedescriptors.property import Lazy +from cybertools.view.web import template + + +class Template(template.Template): + + zpt = ViewPageTemplateFile('content.pt') + + macroTemplate = ViewPageTemplateFile('main.pt') + + skin = None + + @Lazy + def main_macro(self): + return self.macroTemplate.macros['page'] + + @Lazy + def resourceBase(self): + skinSetter = self.skin and ('/++skin++' + self.skin.__name__) or '' + # TODO: put '/@@' etc after path to site instead of directly after URL0 + return self.request.URL[0] + skinSetter + '/@@/' + + def render(self, *args, **kw): + kw['template'] = self + return self.zpt(*args, **kw) +