Commits

Christian Scholz committed c9a349c

Started to implement template store, Page objects and Page variants

  • Participants
  • Parent commits 4e980db

Comments (0)

Files changed (3)

quantumcore/dynamicpage/__init__.py

+from page import Page, PageVariant
+from templates import TemplateStore

quantumcore/dynamicpage/page.py

+
+class PageVariant(object):
+    """base class for variants"""
+    
+    def __call__(self, slot, **kw):
+        """render the given slot"""
+        m = getattr(self, slot)
+        return m(**kw)
+
+class Page(object):
+    """a Page represents a web page on a client's web browser"""
+    
+    variants = {}
+    default_variant = None
+    default_template = 'main'
+    active_variant = None
+    
+    def __init__(self, tmplstore, variant = None, **kw):
+        """initialize the Page with the template store to use"""
+        self.tmplstore = tmplstore
+        self.kw = kw
+        self.variant = variant # the variant to use
+    
+    def render(self, variant = None, template=None):
+        """render the selected variant. The variant is chosen in the following priorities:
+        
+        1. If a ``variant`` argument is present, use this
+        2. If the ``Page`` object was instantiated with a variant name, use this
+        3. Use the default variant inside the ``Page`` class
+        
+        The selected variant is stored inside ``active_variant`` inside the instance.
+        """
+        if variant is not None:
+            self.active_variant= variant
+        elif self.variant is not None:
+            self.active_variant = self.variant
+        else:
+            self.active_variant = self.default_variant
+            
+        if template is None:
+            template = self.default_template
+            
+        # get the necessary objects
+        tmpl = self.tmplstore[template]
+        vobj = self.variants[self.active_variant]
+        
+        return tmpl(page = self, **self.kw)
+        
+    __call__ = render
+        
+    def render_slot(self, slot, variant = None):
+        """render just a slot. We choose the variant to use like above but
+        we do not store the found variant as the active one. This means that 
+        it's just overriding temporarily the decision from the ``render()`` method."""
+        if variant is not None:
+            variant_to_use= variant
+        elif self.variant is not None:
+            variant_to_use = self.variant
+        else:
+            variant_to_use = self.default_variant
+        
+        vobj = self.variants[variant]
+        return vobj('variant', **kw)
+        
+    def __getitem__(self, slotname):
+        """return a rendered slot. This will directly return HTML. It will use the variant
+        store in ``active_variant`` which usually was set by the ``render()``method."""
+        return self.render_slot(slotname, self.active_variant)
+        
+        
+        
+        
+            
+        
+            
+        

quantumcore/dynamicpage/templates.py

+import pkg_resources
+
+from chameleon.zpt.template import PageTemplate
+
+
+class TemplateStore(object):
+    """a class for storing and caching templates"""
+
+    _cache = {}
+
+    def add_from_package(self, name, package, filename):
+        pt = pkg_resources.resource_string(package, filename)
+        obj = self.cache[name] = PageTemplate(pt)
+        return obj
+
+    def get(self, name):
+        """return a template by name"""
+        return self._cache[name]
+        
+    __getitem__ = get