Commits

Christian Scholz committed 3b71eaa

started to implement an example using dynamicpage

  • Participants
  • Parent commits 9c90fbb

Comments (0)

Files changed (3)

File docs/example/main.py

+import wsgiref.simple_server
+
+class Application(object):
+
+    def __call__(self, environ, start_response):
+        start_response("200 Ok", [('Content-Type', 'text/html'),])
+        return "Hello, world!",
+
+app = Application()
+wsgiref.simple_server.make_server('', 8080, app).serve_forever()

File docs/example/page.py

+from quantumcore.dynamicpage import Page, PageVariant
+from quantumcore.dynamicpage import TemplateStore
+
+document = {
+    'title' : 'Title of the document',
+    'subtitle' : 'How I learned to love Python',
+    'content' : 'This is the content of this page. And I like it',
+    'author' : 'Christian Scholz',
+}
+
+tmpls = TemplateStore()
+tmpls.add_from_package('main', __name__, 'templates/main.pt')
+tmpls.add_from_package('content', __name__, 'templates/content.pt')
+tmpls.add_from_package('editform', __name__, 'templates/editform.pt')
+
+class MainPage(PageVariant):
+    
+    
+    def byline(self):
+        """return the content for the byline"""
+        return u"Written by %s" %document['author']
+    
+    def content(self):
+        """return the main document content"""
+        return tmpls.get("content")(document)
+        
+
+class MainEditPage(MainPage):
+    """a variant of the main page which renders the edit form for the content"""
+    
+    def content(self):
+        return tmpls.get("editform")(document)
+        
+    
+class MainPageRenderer(Page):
+    """this class actually renders a page"""
+    
+    default_template = "main"
+    
+    variants = {
+        'main' : MainPage(),
+        'edit' : MainEditPage(),
+    }
+    
+    default_variant = 'main'
+    
+    
+
+
+def main():
+    
+    renderer = MainPageRenderer(context)
+    
+    # render the main page
+    print renderer()
+    
+    # render the editform
+    print renderer('edit')
+    
+    # just render the 'content' slot from the 'main' variant
+    print renderer.slot('main','content')
+    
+    
+    
+    
+    
+        
+        
+        

File docs/example/templates/main.pt

+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+
+<html>
+  <head>
+      <title>Test</title>
+      <style>
+        body { background: #f0f0f0; font-family: Helvetica, Arial, sans-serif; }
+        #container {margin: 0 auto; width: 750px;}
+        #footer {clear: both;}
+        #content {background: #fff; padding: 10px;}
+        #editbutton {display: inline-block; 
+                    padding: 3px 10px 3px 10px; 
+                    margin: 10px 0;
+                    text-decoration: none;
+                    color: #420;
+                    font-weight: bold;
+                    background: #fa0; 
+                    border: 2px solid #a20;}
+      </style>
+  </head>
+
+  <body>
+    <div id="container">
+        <div id="header">
+            <h1>Example Application</h1>
+        </div>
+        <div id="main">
+
+            <div id="content">
+                This is the document content.
+            </div>
+
+            <a href="edit" id="editbutton">Edit</a>
+        </div>
+
+        <div id="footer">
+            blablablablblablablabla
+        </div>
+    </div>
+  </body>
+</html>