The Standard Page
Standard is a page object within the pages module. It provides a standard and reasonably flexible page. It is originally defined with two methods body and head which return the contents of the body and head tags on the page.
To correctly return content into these segments we must use Nevows Stan. Stan is wrapped directly in Enamel's tags module.
from enamel import pages, tags class Index(pages.Standard): def body(self): return tags.div[ "Hello" ]
This creates a simple page with a div in the body containing the text "Hello". You can define attributes as follows
tags.div(id="helloDiv", _class="Helloes")["Hello"]
Note the class attribute is preceded with an underscore because it is already a reserved keyword.
To learn more about Stan go here: http://www.kieranholland.com/code/documentation/nevow-stan/
Arguments
The first thing people may want to do is retrieve GET arguments like in PHP. This is not immediately easy. Rather than using GET arguments, it is better to use a REST style of passing data (Or to use a session properly where there is authentication). The arbitraryArguments keyword allows you to accept REST data which is converted into a tuple called arguments
from enamel import pages, tags class Index(pages.Standard): arbitraryArguments = True def body(self): if self.arguments[0] == "Bye": text = "Goodbye" else: text = "Hello" return tags.div[text]
Accessing http://mysite/Bye/ will result in the text being "Goodbye". The arguments variable contains everything after the pages own root.
Child pages
Most confusion of Nevow is how to arrange a site, the general way of doing this is the childFactory method which returns children as defined by whatever logic you write. Enamel simplifies this by using a dictionary mapping defined in the page as childPages to link page children with the page objects which provide them. This can be used in conjunction with the arbitraryArguments directive, where childPages take precedence.
class notIndex(pages.Standard): def body(self): return "This is a child page of the index" class Index(pages.Standard): childPages = { 'notindex': notIndex, } def body(self): return tags.div[ tags.a(href="/notindex/")["Go away from the index"], ]
This creates two pages and connects notIndex to the Index and makes it accessible via '/notindex'
Child resources don't need to be pages (they do need to be IResource, but nevermind that). You may want to add folders for CSS, JavaScripts? and images. This can not be done with childPages, but the child_* variables can be used.
class Index(pages.Standard): childPages = { 'notindex': notIndex, } child_css = pages.static.File('./css/') def head(self): return tags.link(rel="stylesheet",type="text/css",href="/css/style.css") def body(self): return tags.div[ "Hello" ]
