[PATCH 4 of 5 sherpa] Cleanup code inserting header components and boxes in the main template

Sylvain Thenault sylvain.thenault at logilab.fr
Fri Feb 24 16:11:05 CET 2017


# HG changeset patch
# User Sylvain Thénault <sylvain.thenault at logilab.fr>
# Date 1487941232 -3600
#      Fri Feb 24 14:00:32 2017 +0100
# Node ID 9403b62e12aee1cc104cf4cc05af2c0aa745377d
# Parent  8ed008dd8cd45a3c9f77272dc0da4d3d78f82881
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

diff --git a/cubicweb_sherpa/views/templates.py b/cubicweb_sherpa/views/templates.py
--- a/cubicweb_sherpa/views/templates.py
+++ b/cubicweb_sherpa/views/templates.py
@@ -36,10 +36,17 @@ def add_onload(self, jscode):
     self.add_post_inline_script(u"""$(function() {
   %s
 });""" % jscode)
 
 
+def render_component(comp):
+    stream = UStringIO()
+    w = stream.write
+    comp.render(w)
+    return stream.getvalue()
+
+
 class SherpaMainTemplate(basetemplates.TheMainTemplate):
 
     def call(self, view):
         self.set_request_content_type()
         self.write_doctype()
@@ -47,29 +54,15 @@ class SherpaMainTemplate(basetemplates.T
         context = self.template_context(view)
         self.w(jinja_render('maintemplate', **context))
 
     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)
         application_message = msgcomp.render() if msgcomp else u''
         # contextual components
@@ -80,12 +73,12 @@ class SherpaMainTemplate(basetemplates.T
         ctx.update({
             'title': view.page_title(),
             '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'),
                      'label': 'profils SEDA'},
                     {'url': url('sedalib'),
diff --git a/cubicweb_sherpa/views/templates/maintemplate.jinja2.html b/cubicweb_sherpa/views/templates/maintemplate.jinja2.html
--- a/cubicweb_sherpa/views/templates/maintemplate.jinja2.html
+++ b/cubicweb_sherpa/views/templates/maintemplate.jinja2.html
@@ -12,18 +12,27 @@
           <div class="textSHERPA">
             <h1>Service Hébergé pour la Rédaction de Profils d'Archivage</h1>
           </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>
         </div>
         <div class="panel-body">


More information about the saem-devel mailing list