Make Dynamic Views use the width from the Template Editor

Dynamic Views is little bit different to the other Blog templates on Blogger. It renders all of the posts client-side, with a variety of views. But, there's no easy way to adjust the width of a Dynamic Views Blog, as it doesn't respond to the value selected in the GUI editor.

Blogger's template designer makes it easy to trial different widths.
Too bad this doesn't work on Dynamic Views :(

The reason that it doesn't work is that the javascript for Dynamic Views clears out all the existing <style> from the <head> element of the page when a view is loaded. So even though the <b:template-skin> element propagates the content.width variable into the css, it's wiped away before Dynamic Views even gets started.

Unless...
Actually, there is one case in which it doesn't wipe the <style> tags - when they have a class of "singleton-element". This is how we're going to propagate the content.width variable.

Step 1: Store the Variable's Value

Head to your Template tab, and hit Edit HTML.

If we take a copy of the css before Dynamic View's cleanup occurs, we can persist it in a separate css element which is marked as singleton. So to be sure we get in first, this should happen immediately after the <b:template-skin>.




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
      function getStyleRuleValue(style, selector, sheet) {
          var sheets = typeof sheet !== 'undefined' ? [sheet] : document.styleSheets;
          for (var i = 0, l = sheets.length; i &lt; l; i++) {
              var sheet = sheets[i];
              if( !sheet.cssRules ) { continue; }
              for (var j = 0, k = sheet.cssRules.length; j &lt; k; j++) {
                  var rule = sheet.cssRules[j];
                  if (rule.selectorText &amp;&amp; rule.selectorText.split(',').indexOf(selector) !== -1) {
                      return rule.style[style];
                  }
              }
          }
          return null;
      }
      window.dynamicViewsHack = {
        width: getStyleRuleValue('width', '.dynamicViewsHack')
      };

Step 2: Make a Singleton Style

Add a script somewhere near the end inside the head tag. You can tweak the classes that you want to affect with the width parameter below; the script currently affects the classic and flipcard views.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
      // Get width as number.
      var width = $('&lt;div style="width: ' + window.dynamicViewsHack.width + ';"&gt;&lt;/div&gt;').width();

      var $style = $('<style></style>');
      $style.addClass('singleton-element');
      $style.append('ol#items li.item { max-width: ' + width + 'px; }');
      $style.append(' ol#items li.item div.article { max-width: ' + width + 'px; }');
      $style.append(' div#overview div.overview-wrap { max-width: ' + (width + 250) + 'px; }');
      $style.append(' div#overview div.overview-wrap div.article { max-width: ' + width + 'px; }');
      $style.append(' div.viewitem-panel div.viewitem-content { max-width: ' + width + 'px; }');
      $style.append(' div.viewitem-panel div.viewitem-content div.article { max-width: ' + width + 'px; }');
      $('head').append($style);

Now, when you head to Template tab and hit Customise, you should be able to adjust the value of Entire Blog in the Adjust Widths tab.

Happy Blogging!

Comments

Popular posts from this blog

Remove the header image for posts in Emporio

Rotate my Blogger images

Add snippets to the Emporio theme