-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathchild-pages.txt
82 lines (65 loc) · 2.11 KB
/
child-pages.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
== Child Pages ==
There are various methods to construct a page hierarchy which depends on the structure of the application and the complexity.
The simplest way is using the child_ specials
{{{
#!python
class ChildPage(rend.Page):
addSlash = True
docFactory = loaders.stan(tags.html[
tags.body[
'This is a child page'
]
])
class APage(rend.Page):
addSlash = True
child_aChild = ChildPage()
docFactory = loaders.stan(tags.html[
tags.head[
tags.title['Child Example']
],
tags.body[
tags.a(href='/aChild/')['Go to child page']
]
])
}}}
This constructs a page with a child called ChildPage accessed via /aChild/.
This example can be extended further to demonstrate overriding the childFactory method
{{{
#!python
class ComplexChildren(rend.Page):
addSlash = True
docFactory = loaders.stan(tags.html[
tags.body[
'Another child'
]
])
class ChildPage(rend.Page):
addSlash = True
docFactory = loaders.stan(tags.html[
tags.body[
'A page'
]
])
def childFactory(self, ctx, childSegment):
if childSegment == 'myChild':
return ComplexChildren()
else:
rend.Page.childFactory(self, ctx, childSegment)
}}}
This provides a convenient way to locate multiple children which might require different initialisation arguments. Note that childSegment is a string containing the resource that was requested.
As an example if we wanted to allocate the same resource to a bunch of child resources, we can create a dictionary with references to the page resource and instantiate it with the child segment.
{{{
#!python
class ChildPage(rend.Page):
myChildren = {
'Dog': Animal,
'Cat': Animal,
'Zebra': Animal
}
def childFactory(self, ctx, childSegment):
if childSegment in self.myChildren:
return self.myChildren[childSegment](childSegment)
else:
rend.Page.childFactory(self, ctx, childSegment)
}}}
This is also a convenient way to provide REST style arguments.