Christian Scholz  committed 11df110

removed unused code such as overriding of variants in every possible call. This might get readded in case it's actually needed.
Also changed that implementation of PageVariants in that they are now instantiated not on import time but on every call to the page.

TODO: This is now on every call to a slot which is slow, should really be done on setting the variant name.

  • Participants
  • Parent commits 5c3b6bc
  • Branches default

Comments (0)

Files changed (1)

File quantumcore/dynamicpage/

     """base class for variants which return an empty unicode string in case
     a slot is not defined"""
+    def __init__(self, page):
+        """initialize the page variant with the page it belongs to. With this one can e.g.
+        access the keyword arguments passed into the page"""
+ = page
     def __call__(self, slot, **kw):
         """render the given slot"""
         m = getattr(self, slot, None)
         if m is None:
             return u''
-        return m(**kw)
+        instance_kw = copy.copy(
+        instance_kw.update(kw)
+        return m(**instance_kw)
-class StrictPageVariant(object):
+class StrictPageVariant(PageVariant):
     """base class for variants which checks for the existance of slots
     and raises an AttributeError in case it's missing."""
     def __call__(self, slot, **kw):
         """render the given slot"""
         m = getattr(self, slot)
+        instance_kw = copy.copy(
+        instance_kw.update(kw)
         return m(**kw)
     default_variant = None
     default_template = 'main'
     active_variant = None
+    _variant_object = (None,None) # name, instance
-    def __init__(self, tmplstore, variant = None, **kw):
+    def __init__(self, tmplstore, variant_name = None, **kw):
         """initialize the Page with the template store to use"""
         self.tmplstore = tmplstore = kw
-        self.variant = variant # the variant to use
+        if variant_name is None:
+            self.variant_name = self.default_variant
+        else:
+            self.variant_name = variant_name
             self.js_resources = JSResourceManager(self.javascript, prefix_url=self.prefix+"/js")
             self.css_resources = CSSResourceManager(self.css, prefix_url=self.prefix+"/css")
+    @property
+    def variant(self):
+        """return the (cached) variant object to use"""        
+        return self.variants[self.variant_name](self)
     def serve_resources_wsgi(self,environ,start_response):
         """serve resources like CSS and JS. You need to have quantumcore.resources installed
             return self.css_resources.render_wsgi(environ, start_response)
         # TODO: raise a 404 or some other exception so that the mainloop can raise a 404
         return None
+    def set_variant(self, variant_name):
+        """change the variant to use"""
+        self.variant_name = variant_name
-    def render(self, variant = None, template=None):
+    def render(self, template=None):
         """render the selected variant. The variant is chosen in the following priorities:
         1. If a ``variant`` argument is present, 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]
-        # TODO: This is not used?!?
-        # Why do we compute things many times like active_variant?
-        vobj = self.variants[self.active_variant]
-        # we might add a hook method here, like vobj.preprocess(self)
+        tmpl = self.tmplstore[template]        
         return tmpl(page = self, **
     __call__ = render
     def render_slot(self, slot, variant = None, **kw):
         """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]
-        all_kws = copy.copy(
-        all_kws.update(kw)
-        return vobj(slot, page = self, **all_kws)
+        it's just overriding temporarily the decision from the ``render()`` method."""        
+        return self.variant(slot, page = self, **kw)
     def __getitem__(self, slotname):
         """return a rendered slot. This will directly return HTML. It will use the variant
     def render_wsgi(self, 
-                    variant = None, 
                     template = None, 
         """render the template in WSGI form with setting the appropriate headers"""
         return render_data_to_wsgi(
-            self(variant=variant, template=template).encode(encoding), 
+            self(template=template).encode(encoding),