""" import grok from zope import schema class Mammoth(grok.Model): class fields: name = schema.TextLine(title=u"Name") size = schema.TextLine(title=u"Size", default=u"Quite normal") class Edit(grok.EditForm): pass class Edit2(grok.EditForm): pass edit2 = grok.PageTemplate('<p>Test edit</p>') class Edit3(grok.EditForm): grok.template('edit2') class Display(grok.DisplayForm): pass class Display2(grok.DisplayForm): pass display2 = grok.PageTemplate('<p>Test display</p>') class Display3(grok.DisplayForm): grok.template('display2')
>>> browser = Browser() >>> browser.handleErrors = False >>> browser.open("http://localhost/manfred") >>> print browser.contents <html> <body> <h1>Hello, world!</h1> <span>Blue</span> <span>Blue</span> </body> </html> """ import grok class Mammoth(grok.Model): teeth = u"Blue" class Index(grok.View): pass index = grok.PageTemplate("""\ <html> <body> <h1>Hello, world!</h1> <span tal:content="python:context.teeth">green</span> <span tal:content="context/teeth">green</span> </body> </html> """)
""" import grok class Mammoth(grok.Model): pass class RenderWithArguments(grok.View): grok.name('render') def render(self, message, another): return "Message: %s\nAnother: %s" % (message, another) class UpdateWithArguments(grok.View): grok.name('update') grok.template('update') def update(self, message, another): self.message = message self.another = another update = grok.PageTemplate(""" Coming to us from update(): Message: <span tal:replace="view/message" /> Another: <span tal:replace="view/another" /> """)
""" Inline templates that are not associated with a view class will provoke an error: >>> grok.grok(__name__) Traceback (most recent call last): ... GrokError: Found the following unassociated template(s) when grokking 'grok.tests.view.inline_unassociated': club. Define view classes inheriting from grok.View to enable the template(s). """ import grok class Mammoth(grok.Model): pass club = grok.PageTemplate("""\ <html><body><h1>GROK CLUB MAMMOTH!</h1></body></html> """)
def __init__(self, name): self.name = name class Special(grok.Model): pass class SpecialIndex(grok.View): grok.context(Special) grok.name('index') def render(self): return "special view" grok.context(Mammoth) class Index(grok.View): pass index = grok.PageTemplate("""\ <html> <body> <h1>Hello, <span tal:replace="context/name/title" />!</h1> </body> </html> """)
>>> class WithTemplate(ExceptionView): ... grok.template('exceptionview_template') >>> grok_component('WithTemplate', WithTemplate) True >>> view = getMultiAdapter((Exception(), TestRequest()), name='index') >>> print view() <html> <body> <h1>Something went wrong!</h1> <p>Exception()</p> </body> </html> """ import grok class MockPrincipal(object): id = 'mockprincipal' exceptionview_template = grok.PageTemplate("""\ <html> <body> <h1>Something went wrong!</h1> <p tal:content="python: repr(context)"/> </body> </html> """)
>>> view = component.getMultiAdapter((manfred, request), name='meal') >>> print view() <html><body><h1>GROK EAT MAMMOTH!</h1></body></html> """ import grok class Mammoth(grok.Model): pass class Painting(grok.View): grok.template('cavepainting') cavepainting = grok.PageTemplate("""\ <html><body><h1>GROK PAINT MAMMOTH!</h1></body></html> """) class Food(grok.View): grok.template('food_template') grok.name('meal') food_template = grok.PageTemplate("""\ <html><body><h1>GROK EAT MAMMOTH!</h1></body></html> """)
from zope import schema class IceWorld(grok.Application, grok.Container): pass class Mammoth(grok.Model): class fields: name = schema.TextLine(title=u"Name") size = schema.TextLine(title=u"Size", default=u"Quite normal") class Index(grok.DisplayForm): grok.context(Mammoth) index = grok.PageTemplate(""" <p> Test display: application <tal:replace tal:replace="view/application_url" /> </p>""") class Edit(grok.EditForm): grok.context(Mammoth) edit = grok.PageTemplate(""" <p> Test edit: application <tal:replace tal:replace="view/application_url" /> </p>""")
self.sum = self.calculateSum() self.sendEmail() def sendEmail(self): """send an email here""" class PDFSum(grok.View): def update(self): pass def render(self): return pdfdata sum = grok.PageTemplate("""\ <p tal:content="view/calculateSum">...</p> <p tal:content="view/precalculatedSum">...</p> """) from zope import schema class Index(grok.Form): """a form this is the default view for the Calculator model (because it's called Index) """ class fields: operand = schema.Int(title=u'Operand') operator = schema.Choice(...)
import grok class Cave(grok.Model): pass class Index(grok.View): pass index = grok.PageTemplate("""\ <html> <body> <h1>A comfy cave</h1> </body> </html> """)
layout = grok.PageTemplate("""\ <html> <body> <!-- Most simple: Render the whole menu --> <div tal:replace="structure menu/main/@@links"/> <!-- Variation 1: Render whole menu as drop down list --> <div tal:replace="structure menu/main/@@dropdown"/> <!-- Variation 2: Render menu manually, get each item as link <ul> <li tal:repeat="item menu/main"> <a tal:replace="structure item/@@link"> </li> </ul> <!-- Variation 3: Render menu and items manually <ul tal:repeat="item menu/main"><a tal:attributes="href item/action" tal:content="item/title"></ul> </ul> <!-- Variation 4: Render option elements automatically --> <select> <option tal:repeat="item menu:main" tal:replace="item/@@option"> </select> <!-- Bind the menu to a different object explicitly --> <div tal:replace="structure python:menu['main'].bind(object1, request)"/> ' <!-- Alternative: use a grok variable in the templates: --> <div tal:replace="structure grok/menu/main/context:asdf/"/> <grok:menu </body> </html>""")
>>> from zope.testbrowser.testing import Browser >>> browser = Browser() >>> browser.handleErrors = False >>> browser.open("http://localhost/manfred/@@painting") >>> print browser.contents <html> <body> <h1>Hello, world!</h1> </body> </html> """ import grok class Mammoth(grok.Model): pass class Painting(grok.View): pass painting = grok.PageTemplate("""\ <html> <body> <h1>Hello, world!</h1> </body> </html> """)
... So developers get informed, that they called the wrong macro view. Let's clean up. >>> browser.open('http://localhost/applications') >>> ctrl = browser.getControl(name='items') >>> ctrl.getControl(value='manfred').selected = True >>> browser.getControl('Delete Selected').click() """ import grok class Mammoth(grok.Application, grok.Container): pass class ExternalView(grok.View): """A view that calls grokadminmacros 'illegally'. """ grok.context(Mammoth) externalview = grok.PageTemplate("""\ <html metal:use-macro="context/@@grokadminmacros/gaia-page"> </html> """)
""" Forms cannot define a render method. Here we show the case where the EditForm has an explicit template associate with it. >>> grok.grok(__name__) Traceback (most recent call last): ... GrokError: It is not allowed to specify a custom 'render' method for form <class 'grok.tests.form.norender.Edit'>. Forms either use the default template or a custom-supplied one. """ import grok class Mammoth(grok.Model): pass class Edit(grok.EditForm): # not allowed to have a render method def render(self): return "this cannot be" edit = grok.PageTemplate('Foo!')
""" Only one, either a template, or render() can be specified: >>> grok.grok(__name__) Traceback (most recent call last): ... GrokError: Multiple possible ways to render view <class 'grok.tests.view.eithertemplateorrender.CavePainting'>. It has both a 'render' method as well as an associated template. """ import grok class Mammoth(grok.Model): pass class CavePainting(grok.View): def render(self): pass cavepainting = grok.PageTemplate("nothing")
import grok class Mammoth(grok.Model): pass class Index(grok.View): pass index = grok.PageTemplate("""\ <html> <body> <a tal:attributes="href static/file.txt">Some text in a file</a> </body> </html>""")
We expect this grok to fail, and give >>> error_component = None >>> try: ... grok.testing.grok(__name__) ... except ConfigurationExecutionError as error: ... if sys.version_info[0] >= 3: ... error_component = error.args[1].component ... else: ... error_component = error.evalue.component >>> error_component <class 'grok.tests.error.error.CavePainting'> """ import grok import sys from zope.configuration.config import ConfigurationExecutionError class Mammoth(grok.Model): pass class CavePainting(grok.View): grok.template("a") a = grok.PageTemplate("a") cavepainting = grok.PageTemplate("b")
return "A nice large dancing hall for mammoths." class Grilled(grok.View): def update(self): self.spices = "Pepper and salt" class Painting(grok.View): pass painting = grok.PageTemplate("""\ <html metal:use-macro="context/@@layout/main"> <div metal:fill-slot="slot"> GROK SLOT! </div> </html> """) class Layout(grok.View): pass layout = grok.PageTemplate("""\ <html metal:define-macro="main"> <body> <h1>GROK MACRO!</h1> <div metal:define-slot="slot"> </div>
""" import grok class Herd(grok.Container, grok.Model): pass class Mammoth(grok.Model): pass grok.context(Mammoth) class Index(grok.View): def render(self): return self.url() class Another(grok.View): def render(self): return self.url() class YetAnother(grok.View): pass yetanother = grok.PageTemplate('<p tal:replace="view/url" />')
import grok from zope import interface grok.definelayer('my') grok.defineskin('my') # Picks up the layer 'my' if it exists grok.layer('my') # If there is only a single layer defined # in a module, it will be the default class Painting(grok.View): pass fireplace = grok.PageTemplate("""\ <html><body></body></html> """)
>>> print view() <html> <body> <h1>red</h1> <h1>red</h1> </body> </html> """ import grok class Mammoth(grok.Model): pass class CavePainting(grok.View): def update(self): self.color = "red" cavepainting = grok.PageTemplate("""\ <html> <body> <h1 tal:content="view/color"/> <h1 tal:content="python: view.color"/> </body> </html> """)
>>> request = TestRequest() >>> from zope.component import getMultiAdapter >>> view = getMultiAdapter((manfred, request), name='cavepainting') >>> print view() None >>> print request.response.getStatus() 302 >>> print request.response.getHeader('Location') somewhere-else """ import grok class Mammoth(grok.Model): pass class CavePainting(grok.View): def update(self): self.request.response.redirect('somewhere-else') cavepainting = grok.PageTemplate("""\ <html> <body> <h1 tal:content="this-is-an-error" /> </body> </html> """)
class Edit(grok.EditForm): def update(self): self.context.report = ("The form's update() was called and my name " "was %s." % self.context.name) class EditRedirect(grok.EditForm): def update(self): # redirect upon form submit so that no changes are ever saved if 'form.name' in self.request: self.redirect(self.url('index')) class EditUpdateWithArguments(grok.EditForm): def update(self, report=None): if report is not None: self.context.report = report editupdatewitharguments = grok.PageTemplate(""" <html> <body> <form action="" tal:attributes="action request/URL"> <input type="text" name="report" /> <div tal:repeat="widget view/widgets" tal:content="structure widget" /> <div tal:repeat="action view/actions" tal:content="structure action/render" /> </form> </body> </html> """)
""" It is too confusing to have a template that would be implicitly associated with a view while that view already refers to another template using grok.template. Therefore there is an error: >>> grok.grok(__name__) Traceback (most recent call last): ... GrokError: Multiple possible templates for view <class 'grok.tests.view.explicitimplicittemplate.Painting'>. It uses grok.template('cavepainting'), but there is also a template called 'painting'. """ import grok class Mammoth(grok.Model): pass class Painting(grok.View): grok.template('cavepainting') cavepainting = grok.PageTemplate("GROK CAVEPAINT MAMMOTH!") painting = grok.PageTemplate("GROK PAINT MAMMOTH!")
def __init__(self, name): self.name = name def getMammoth(self, name): return Mammoth(name) def traverse(self, name): return self.getMammoth(name) class Mammoth(grok.Model): def __init__(self, name): self.name = name grok.context(Mammoth) class Index(grok.View): pass index = grok.PageTemplate("""\ <html> <body> <h1>Hello, <span tal:replace="context/name/title" />!</h1> <p><span tal:replace="context/name/title" /> is part of <span tal:replace="context/__parent__/name" />.</p> </body> </html> """)
class Mammoth(grok.Model): pass class CavePainting(grok.View): pass cavepainting = grok.PageTemplate("""\ <html> <body> <h1 tal:content="string:Mammoth Cave Painting"/> <ul> <li tal:content="structure python:repr(request)" /> <li tal:content="structure nocall:view" /> <li tal:content="structure nocall:context" /> <li tal:content="structure nocall:modules" /> </ul> </body> </html> """) class Hunt(grok.View): grok.name('hunting') hunt = grok.PageTemplate("""\ <html><body><h1>GROK HUNT MAMMOTH!</h1></body></html> """)