Cleanup code inserting header components and boxes in the main template
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 24 Feb 2017 14:00:32 +0100
changeset 180 0802510445f1
parent 179 35d98fb943a8
child 181 12721d10d5bd
Cleanup code inserting header components and boxes in the main template * use .get_component * better naming * factor out a function to render a component as string * let the template to as much thing as possible
cubicweb_sherpa/views/templates.py
cubicweb_sherpa/views/templates/maintemplate.jinja2.html
--- a/cubicweb_sherpa/views/templates.py	Fri Feb 24 13:57:09 2017 +0100
+++ b/cubicweb_sherpa/views/templates.py	Fri Feb 24 14:00:32 2017 +0100
@@ -38,6 +38,13 @@
 });""" % jscode)
 
 
+def render_component(comp):
+    stream = UStringIO()
+    w = stream.write
+    comp.render(w)
+    return stream.getvalue()
+
+
 class SherpaMainTemplate(basetemplates.TheMainTemplate):
 
     def call(self, view):
@@ -49,25 +56,11 @@
 
     def template_context(self, view):
         """Return the main-template's context."""
-        # left boxes
-        left_boxes = list(self._cw.vreg['ctxcomponents'].poss_visible_objects(
-            self._cw, rset=self.cw_rset, view=view, context='left'))
-        stream = UStringIO()
-        for box in left_boxes:
-            box.render(w=stream.write, view=view)
-        left_boxes_html = stream.getvalue()
+        # boxes
+        boxes = [render_component(box) for box in self.get_components(view, context='left')]
         # header
-        stream = UStringIO()
-        w = stream.write
-        components = self.get_components(view, context='header-right')
-        if components:
-            w(u'<ul class="nav navbar-nav navbar-right">')
-            for component in components:
-                w(u'<li>')
-                component.render(w=w)
-                w(u'</li>')
-            w(u'</ul>')
-        right_header_component = stream.getvalue()
+        header_components = [render_component(comp)
+                             for comp in self.get_components(view, context='header-right')]
         # application message
         msgcomp = self._cw.vreg['components'].select_or_none(
             'applmessages', self._cw, rset=self.cw_rset)
@@ -82,8 +75,8 @@
             'page_content': view.render(),
             'application_message': application_message,
             'contextual_components': contextual_components,
-            'right_header_component': right_header_component,
-            'left_boxes': left_boxes_html,
+            'header_components': header_components,
+            'boxes': boxes,
             'side_box': {
                 'goTo_links': [
                     {'url': url('SEDAArchiveTransfer'),
--- a/cubicweb_sherpa/views/templates/maintemplate.jinja2.html	Fri Feb 24 13:57:09 2017 +0100
+++ b/cubicweb_sherpa/views/templates/maintemplate.jinja2.html	Fri Feb 24 14:00:32 2017 +0100
@@ -14,14 +14,23 @@
           </div>
         </a>
       </div>
+      {% if header_components %}
       <div class="col-md-3 col-xs-4">
-        {{ right_header_component|safe }}
+        <ul class="nav navbar-nav navbar-right">
+        {% for comp_html in header_components %}
+        <li>{{ comp_html|safe}}</li>
+        {% endfor %}
+        </ul>
+
       </div>
+      {% endif %}
     </div>
   </nav>
   <div id="{{page_id}}" class="container-fluid" role="main">
     <aside id="aside-main-left" class="col-md-2 cwjs-aside">
-      {{ left_boxes|safe }}
+      {% for box_html in boxes %}
+      {{ box_html|safe}}
+      {% endfor %}
       <div class="panel panel-default contextFreeBox facet_filterbox" id="facet_filterbox">
         <div class="panel-heading">
           <div class="panel-title">navigation</div>