Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Scalable Stylesheets - Theming with Modular CSS

Scalable Stylesheets - Theming with Modular CSS

Drupal excels at making big sites. It's flexible. It scales well. However CSS, written haphazardly, does not scale well. It breaks down and becomes increasingly painful to maintain as sites grow and change with time. Fortunately, if you approach CSS development methodically — in a organized and consistent fashion — your stylesheets can adapt as sites and teams evolve. But it turns out, implementing CSS in such a way is non-trivial in Drupal. In this presentation, we'll look at methods that will empower you take control of your styleheets, and hopefully lead to Drupal themes that are more flexible and withstand the test of time.

pixelwhip

July 14, 2012
Tweet

Other Decks in Programming

Transcript

  1. Contextual Styling CSS Pseudo Logic by exploiting the cascade .not-­‐front

     #sidebar-­‐first  .block  h2  {    ... }
  2. Naming Conventions What you have .item-­‐list, .views-­‐row  {    ...

    } .menu,   .leaf, .leaf  a  {    ... } .resource-­‐list, .resource-­‐item  {    ... } .nav, .nav-­‐item, .nav-­‐link  {    ... } What you want
  3. Applying Classes Through the UI • Views • Semantic Fields

    • Display Suite • Block Class • Skinr • Fusion Accelerator
  4. Applying Classes Through the UI Directly to template files block.tpl.php

    block-­‐-­‐footer.tpl.php block-­‐-­‐header.tpl.php block-­‐-­‐search.tpl.php block-­‐-­‐navigation-­‐-­‐menu.tpl.php block-­‐-­‐navigation-­‐-­‐arghh.tpl.php block-­‐-­‐this-­‐-­‐sucks.tpl.php block-­‐-­‐wtf.tpl.php block-­‐-­‐i-­‐need-­‐a-­‐drink.tpl.php
  5. BLOCKS block.tpl.php <div  id="<?php  print  $block_html_id;  ?>"      class="<?php

     print  $classes;  ?>"<?php  print  $attributes;  ?>>    <?php  print  render($title_prefix);  ?>    <?php  if  ($block-­‐>subject):  ?>        <h2<?php  print  $title_attributes;  ?>><?php  print  $block-­‐>subject  ?></h2>    <?php  endif;?>    <?php  print  render($title_suffix);  ?>    <div  class="content"<?php  print  $content_attributes;  ?>>        <?php  print  $content  ?>    </div> </div>
  6. BLOCKS block.tpl.php <div  id="<?php  print  $block_html_id;  ?>"      class="<?php

     print  $classes;  ?>"<?php  print  $attributes;  ?>>    <?php  print  render($title_prefix);  ?>    <?php  if  ($block-­‐>subject):  ?>        <h2<?php  print  $title_attributes;  ?>><?php  print  $block-­‐>subject  ?></h2>    <?php  endif;?>    <?php  print  render($title_suffix);  ?>    <div  <?php  print  $content_attributes;  ?>>        <?php  print  $content  ?>    </div> </div>
  7. Attributes Array          ‑ <?php  print  drupal_attributes($vars['#attributes']);

     ?>                         ‑ class="input-­‐search  input-­‐inline"  placeholder="Enter  a  search  term…"   title="Enter  the  terms  you  wish  to  search  for." $vars['#attributes']
  8. Opportunities to Alter Block Markup hook_block_view_alter() ‑ hook_preprocess_block() ‑ hook_process_block()

    ⬋                ⬊ block.tpl.php        block-­‐-­‐module.tpl.php
  9. BLOCKS hook_preprocess_block() function  [theme]_preprocess_block(&$vars)  {    //  Set  shortcut  variables.

       ...    //  Set  global  classes  for  all  blocks.    ...    //  Add  specific  classes  and/or    //  theme_hook_suggestions  to  targeted  blocks.    ... }
  10. FIELDS MARKUP <div  class="classes">    <div  class="TITLE-­‐classes">...</div>    <div  class="CONTENT-­‐classes">

           <div  class="ITEM-­‐classes">            ...        </div>    </div> </div>
  11. FIELDS theme_field() function  theme_field($variables)  {    $output  =  ''; ...

       //  Render  the  label,  if  it's  not  hidden.    if  (!$variables['label_hidden'])  {        $output  .=  '<div  class="field-­‐label"'  .  $variables['title_attributes']  .                  '>'  .  $variables['label']  .  ':&nbsp;</div>';    }        ...    return  $output; }
  12. FIELDS theme_field() function  theme_field($variables)  {    $output  =  ''; ...

       //  Render  the  label,  if  it's  not  hidden.    if  (!$variables['label_hidden'])  {        $output  .=  '<div'  .  $variables['title_attributes']  .              '>'  .  $variables['label']  .  ':&nbsp;</div>';    }        ...    return  $output; }
  13. MENUS MARKUP <ul  class="MENU-­‐TREE-­‐classes">    <li  class="ITEM-­‐classes">      

     <a  class="LINK-­‐classes">...</a>    </li>    <li  class="ITEM-­‐classes">...</li>    <li  class="ITEM-­‐classes">...</li>  </ul> theme_menu_tree() theme_menu_link()
  14. MENUS theme_menu_link() CORE function  modularstyle_menu_link(array  $variables)  {    $element  =

     $variables['element'];    $sub_menu  =  '';    if  ($element['#below'])  {        $sub_menu  =  drupal_render($element['#below']);    }    $output  =  l($element['#title'],                              $element['#href'],                            $element['#localized_options']);        return  '<li'  .  drupal_attributes($element['#attributes'])  .  '>'                          .  $output  .  $sub_menu  .  "</li>\n"; }