-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
src/grokcore/traverser/ftests/traversal/traverser_with_layer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
""" | ||
Apart from using the ``traverse`` method on a model, you can | ||
also create a separate traverser component: | ||
>>> getRootFolder()["herd"] = Herd('The Big Mammoth Herd') | ||
>>> from zope.app.wsgi.testlayer import Browser | ||
>>> browser = Browser() | ||
>>> browser.handleErrors = False | ||
>>> browser.open("http://localhost/herd/manfred") | ||
>>> print browser.contents | ||
<html> | ||
<body> | ||
<h1>Hello, Manfred!</h1> | ||
<p>Manfred is part of The Big Mammoth Herd.</p> | ||
</body> | ||
</html> | ||
>>> browser.open("http://localhost/herd/ellie") | ||
>>> print browser.contents | ||
<html> | ||
<body> | ||
<h1>Hello, Ellie!</h1> | ||
<p>Ellie is part of The Big Mammoth Herd.</p> | ||
</body> | ||
</html> | ||
Now let's call the same URI on a different Layer. | ||
>>> browser.open("http://localhost/++skin++elephant/herd/ellie") | ||
>>> print browser.contents | ||
<html> | ||
<body> | ||
<h1>Hello, Ellie Elephant!</h1> | ||
<p>Ellie Elephant is part of The Big Mammoth Herd.</p> | ||
</body> | ||
</html> | ||
""" | ||
import grokcore.component as grok | ||
import grokcore.content as content | ||
import grokcore.traverser | ||
import grokcore.view as view | ||
|
||
|
||
class ElephantLayer(view.IDefaultBrowserLayer): | ||
pass | ||
|
||
class ElephantSkin(ElephantLayer): | ||
view.skin('elephant') | ||
|
||
|
||
class Herd(content.Model): | ||
|
||
def __init__(self, name): | ||
self.name = name | ||
|
||
|
||
class HerdTraverser(grokcore.traverser.Traverser): | ||
grok.context(Herd) | ||
|
||
def traverse(self, name): | ||
return Mammoth(name) | ||
|
||
|
||
class ElephantTraverser(grokcore.traverser.Traverser): | ||
grok.context(Herd) | ||
view.layer(ElephantLayer) | ||
|
||
def traverse(self, name): | ||
return Mammoth(name + ' Elephant') | ||
|
||
|
||
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> | ||
""") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters