[views] Fix rendering of html head
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 03 Feb 2017 16:31:49 +0100
changeset 76 216ac18640f8
parent 71 a1789d380f77
child 77 f148546ee04f
[views] Fix rendering of html head Prior to this change, generated page had **2** <head>, one generated by the template and the other by cubicweb's stream.getvalue() (see cubicweb.utils.HTMLStream). It seems that the proper way to handle this is to let cubicweb handle this, since this is rather tricky we don't want to bother with this.
views/templates.py
views/templates/maintemplate.jinja2
--- a/views/templates.py	Fri Feb 03 14:48:11 2017 +0100
+++ b/views/templates.py	Fri Feb 03 16:31:49 2017 +0100
@@ -18,7 +18,7 @@
 
 from logilab.common.decorators import monkeypatch
 
-from cubicweb.utils import json_dumps, HTMLHead, UStringIO
+from cubicweb.utils import HTMLHead, UStringIO
 from cubicweb.web.views import basetemplates
 
 from . import jinja_render
@@ -42,7 +42,8 @@
 
     def call(self, view):
         self.set_request_content_type()
-        self._cw.html_headers.define_var('BASE_URL', self._cw.build_url(''))
+        self.write_doctype()
+        self.template_header(self.content_type, view=view)
         context = self.template_context(view)
         self.w(jinja_render('maintemplate', **context))
 
@@ -122,35 +123,17 @@
         return ctx
 
     def base_context(self):
-        """Return a basic context using standard cubicweb information including
-        all resources added with add_{js,css}, etc.
-        """
+        """Return a basic context using standard cubicweb information."""
         req = self._cw
-        context = {
-            'base_url': req.build_url(''),
-            'data_url': req.datadir_url,
+        return {
             'page_id': 'contentmain',
             '_': req._,
             'user': req.user.login,
+            'base_url': req.build_url(''),
+            'data_url': req.datadir_url,
             'current_url': req.relative_path(),
         }
 
-        # handle define_var() calls
-        context['js_vars'] = [(var, json_dumps(val))
-                              for var, val, override in req.html_headers.jsvars]
-        # handle add_js() calls
-        context['jsfiles'] = list(req.uiprops['JAVASCRIPTS'])
-        for jsfile in req.html_headers.jsfiles:
-            context['jsfiles'].append(jsfile)
-        # handle add_onload() calls
-        context['inline_scripts'] = req.html_headers.post_inlined_scripts
-        # handle add_css() calls
-        context['cssfiles'] = list(req.uiprops['STYLESHEETS'])
-        for cssfile, media in req.html_headers.cssfiles:
-            context['cssfiles'].append(cssfile)  # XXX media?
-
-        return context
-
 
 def registration_callback(vreg):
     vreg.register_and_replace(SherpaMainTemplate, basetemplates.TheMainTemplate)
--- a/views/templates/maintemplate.jinja2	Fri Feb 03 14:48:11 2017 +0100
+++ b/views/templates/maintemplate.jinja2	Fri Feb 03 16:31:49 2017 +0100
@@ -1,22 +1,3 @@
-<!doctype html>
-
-<html>
-  <head>
-    <meta charset="utf-8" />
-    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, width=device-width" />
-    <meta name="theme-color" content="#ffffff">
-
-    <title>{{ title }}</title>
-    <script>
-      {% for varname, value in js_vars %}
-        var {{ varname }} = {{ value }};
-      {% endfor %}
-    </script>
-    {% for css in cssfiles %}
-      <link rel="stylesheet" href="{{ css }}" type="text/css" media="all" />
-    {% endfor %}
-
-  </head>
   <body>
 
     <nav class="navbar navbar-default navbar-sherpa" role="banner">
@@ -104,15 +85,4 @@
       </div>
     </footer>
   </div>
-
-  {% for js in jsfiles %}
-    <script src="{{ js }}"></script>
-  {% endfor %}
-  {% for inlinescript in inline_scripts %}
-    <script type="text/javascript">
-      {{ inlinescript }}
-    </script>
-  {% endfor %}
-
-  </body>
-</html>
+</body>