return self.url() class YetAnother(grok.View): pass class Multiplier(grok.View): def update(self, age=0): self.age = age def render(self): return six.text_type(self.age * 2) yetanother = grok.PageTemplate('<p tal:replace="view/url" />') class URLTestingSkin(grok.IBrowserRequest): grok.skin('urltesting') class AnotherURLTestingSkin(grok.IBrowserRequest): grok.skin('anotherurltesting') class URLTestingViewOnASkin(grok.View): grok.layer(URLTestingSkin) grok.name('test') def render(self):
>>> print browser.contents <html> <body> <h1>Hello, world!</h1> <span><class 'grokcore.view.ftests.view.index.Mammoth'></span> <span><class 'grokcore.view.ftests.view.index.Mammoth'></span> </body> </html> """ import grokcore.view as grok class Mammoth(grok.Context): pass class Index(grok.View): pass index = grok.PageTemplate("""\ <html> <body> <h1>Hello, world!</h1> <span tal:content="structure python:context.__class__">green</span> <span tal:content="structure context/__class__">green</span> </body> </html> """)
>>> from zope.testbrowser.wsgi 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 grokcore.view as grok class Mammoth(grok.Context): pass class Painting(grok.View): pass painting = grok.PageTemplate("""\ <html> <body> <h1>Hello, world!</h1> </body> </html> """)
class Mammoth(grok.Context): 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> """)
grok.layer(CasualLayer) class Mammoth(grok.Context): pass class Hello(grok.View): pass hello = grok.PageTemplate("""\ <html> <body> <h1>Hi sir !</h1> </body> </html> """) class Happy(grok.View): grok.layer(PartyLayer) def render(self): return u"Hee yay !" class Sad(grok.View): grok.layer(RainyLayer)
>>> view = component.getMultiAdapter((manfred, request), name='meal') >>> print view() <html><body><h1>GROK EAT MAMMOTH!</h1></body></html> """ import grokcore.view as grok class Mammoth(grok.Context): 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> """)
>>> request = TestRequest() >>> from zope.component import getMultiAdapter >>> view = getMultiAdapter((manfred, request), name='cavepainting') >>> print view() None >>> print view.response.getStatus() 302 >>> print view.response.getHeader('Location') somewhere-else """ import grokcore.view as grok class Mammoth(grok.Context): pass class CavePainting(grok.View): def update(self): self.redirect('somewhere-else') cavepainting = grok.PageTemplate("""\ <html> <body> <h1 tal:content="this-is-an-error" /> </body> </html> """)
def __init__(self, name): self.name = name class Special(content.Model): pass class SpecialIndex(view.View): grok.context(Special) grok.name('index') def render(self): return "special view" grok.context(Mammoth) class Index(view.View): pass index = view.PageTemplate("""\ <html> <body> <h1>Hello, <span tal:replace="context/name/title" />!</h1> </body> </html> """)
""" If multiple templates can be found, one in the module and one in the template directory, there is an error: >>> grok.testing.grok(__name__) Traceback (most recent call last): ... zope.configuration.config.ConfigurationExecutionError:\ martian.error.GrokError: Conflicting\ templates found for name 'cavepainting': the inline template in\ module 'grokcore.view.tests.base.view.dirandinlinetemplate' conflicts\ with the file template in directory\ '...dirandinlinetemplate_templates' in: """ import grokcore.view as grok class Mammoth(grok.Context): pass class CavePainting(grok.View): pass cavepainting = grok.PageTemplate("nothing")
""" 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.testing.grok(__name__) Traceback (most recent call last): ... ConfigurationExecutionError: martian.error.GrokError: Multiple possible templates for view <class 'grokcore.view.tests.view.explicitimplicittemplate.Painting'>. It uses grok.template('cavepainting'), but there is also a template called 'painting'. in: """ import grokcore.view as grok class Mammoth(grok.Context): pass class Painting(grok.View): grok.template('cavepainting') cavepainting = grok.PageTemplate("GROK CAVEPAINT MAMMOTH!") painting = grok.PageTemplate("GROK PAINT MAMMOTH!")
""" Inline templates that are not associated with a view class will provoke an error: >>> from grokcore.view.testing import warn >>> import warnings >>> saved_warn = warnings.warn >>> warnings.warn = warn >>> grok.testing.grok(__name__) From grok.testing's warn(): ... UserWarning: Found the following unassociated template after configuration\ in 'grokcore.view.tests.base.view.inline_unassociated': club... >>> warnings.warn = saved_warn """ import grokcore.view as grok class Mammoth(grok.Context): pass club = grok.PageTemplate("""\ <html><body><h1>GROK CLUB MAMMOTH!</h1></body></html> """)
class CavePainting(grok.View): template = grok.PageTemplate(filename='templates/cavepainting.pt')
import grokcore.view as grok class Mammoth(grok.Context): 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>""")
>>> view = component.getMultiAdapter((manfred, request), name='templ') Traceback (most recent call last): ... zope.interface.interfaces.ComponentLookupError:\ ((<grokcore.view.tests.base.view.twoviewsusetemplate.Mammoth object at 0x...>,\ <zope.publisher.browser.TestRequest instance URL=http://127.0.0.1>),\ <InterfaceClass zope.interface.Interface>, 'templ') """ import grokcore.view as grok class Mammoth(grok.Context): pass class A(grok.View): pass a = grok.PageTemplate("View A") class B(grok.View): grok.template('a') class C(grok.View): grok.template('templ') class D(grok.View): grok.template('templ') templ = grok.PageTemplate('Template')
import grokcore.view as grok grok.templatedir("templates") class Mammoth(grok.Context): pass class CavePainting(grok.View): pass unassociated_instance = grok.PageTemplate(""" <html><body><h1>GROK PAINT MAMMOTH!</h1></body></html> """)
grok.context(Herd) def traverse(self, name): mammoth = Mammoth(name) # We pretend the mammoth is the child object of some competely # differnt Herd object. mammoth.__parent__ = Herd('The Three Stooges') return mammoth class Mammoth(content.Model): def __init__(self, name): self.name = name grok.context(Mammoth) class Index(view.View): pass index = view.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> """)
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/macros/main"> <div metal:fill-slot="slot"> GROK SLOT! </div> </html> """) class Layout(grok.View): # Layout template is in macros_templates/layout.pt for reload test # purposes. pass class Dancing(grok.View): pass
View with an associated PageTemplate that is referred to using ``grok.template``: >>> manfred = Mammoth() >>> from zope.publisher.browser import TestRequest >>> request = TestRequest() >>> from zope import component >>> view = component.getMultiAdapter((manfred, request), name='food') >>> print view() <html> <body> ME GROK EAT MAMMOTH! </body> </html> """ import grokcore.view as grok import os.path class Mammoth(grok.Context): pass class Food(grok.View): pass food = grok.PageTemplate( filename=os.path.join('templatedirectoryname', 'food.pt'))
>>> print(view()) <html> <body> <h1>red</h1> <h1>red</h1> </body> </html> """ import grokcore.view as grok class Mammoth(grok.Context): 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> """)
""" import grokcore.view as grok class Mammoth(grok.Context): 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" /> """)
class CavePainting(grok.View): template = grok.PageTemplate(filename='templates/cavepainting.pt') def render(self): return "Cool I have a render method."