<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">

  <title>The Software Simpleton</title>
  
  <link href="http://thesoftwaresimpleton.com//" />
  <updated>2011-11-30T21:22:01+00:00</updated>
  <id>http://thesoftwaresimpleton.com//</id>
  <author>
    <name>Paul Cowan</name>
    
      <email>dagda1@scotalt.net</email>
    
  </author>

  
  <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/TheSoftwareSimpleton" /><feedburner:info uri="thesoftwaresimpleton" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:browserFriendly></feedburner:browserFriendly><entry>
    <title>Backbone.js - Lessons Learned</title>
    <link href="http://thesoftwaresimpleton.com//blog/2011/11/13/backbone-js---lessons-learned/" />
    <updated>2011-11-13T13:14:00+00:00</updated>
    <id>http://thesoftwaresimpleton.com//blog/2011/11/13/backbone-js---lessons-learned</id>
    <content type="html">&lt;p&gt;I have been using &lt;a href="http://documentcloud.github.com/backbone/" target="_blank"&gt;Backbone.js&lt;/a&gt; for a few months now and I have found the learning curve to be a steep one.  As is the case with any flexible framework, there are a number of ways of doing anything in Backbone.js and I think the framework could do with some tightening up to stop users having to roll their own custom implementations for common scenarios such as disposing of views.&lt;/p&gt;

&lt;p&gt;For those that have been sleeping under a rock recently, &lt;a href="http://documentcloud.github.com/backbone/" target="_blank"&gt;Backbone.js&lt;/a&gt; is a client side MVC framework that should not require any introduction.  The client side MVC framework seems to be a bit of a du jour thing at the moment but I think it is important to have some sort of organisational structure around JavaScript or the looseness of the language can quickly turn any javascript code into the now infamous ball of mud.  Backbone.js is one way of achieving such a conformity.&lt;/p&gt;

&lt;p&gt;The point of this post is to solidify where my current thinking is with Backbone.js and to welcome any comments from my readership of 10 or so readers that might point me to a better and brighter land.  I must also warn you that I will be using coffeescript for the code examples instead of pure javascript and if this offends, please read no further.&lt;/p&gt;

&lt;h2&gt;Scenario&lt;/h2&gt;

&lt;p&gt;The scenario I want to illustrate is a simple one, I want to show how I transition from the state outlined below were a user selects from a list of what are called in the application context, business units:
&lt;img class='' src='http://thesoftwaresimpleton.com//images/backbone/bu.png' width='' height='' alt='' title=''&gt;
To a more detailed report illustrated in the image below:
&lt;img class='' src='http://thesoftwaresimpleton.com//images/backbone/mi.png' width='' height='' alt='' title=''&gt;
The user can go back and forward between these views and drill down into further views of the information but I want to concentrate on this simple scenario outlined above in the two screenshots.&lt;/p&gt;

&lt;h3&gt;Gotcha 1 - Don't Have Views That Reference Each Other a.k.a. Coupling&lt;/h3&gt;

&lt;p&gt;In the scenario I have listed, I need some way to gather the user's checkbox selection and pass these values to another view that renders the table and chart of results.  My first few attempts at this in other applications was to let one of the views hold a reference to another and pass the information that way.   An example of this type of naive implementation might look something like this:&lt;/p&gt;

&lt;div&gt;&lt;script src='https://gist.github.com/1362192.js?file='&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;code&gt;class BusinessUnitsView extends MIBaseView
  events:
    'click #generate': &amp;quot;generate&amp;quot;

  generate: (e) =&amp;gt;    
    e.preventDefault()

    identifiers = _.map( $('input:checked'), (check) -&amp;gt; 
                    {Uid: $(check).val(), Title: $(check).parents(&amp;quot;tr:eq(0)&amp;quot;).find(&amp;quot;label&amp;quot;).html()}
                  )

    @managementReportView = new ManagementReportView({collect:  new BusinessUnits(identifiers)})

    @managementReportView.render()&lt;/code&gt;&lt;/pre&gt;&lt;/noscript&gt;&lt;/div&gt;


&lt;p&gt;You can see on lines 12 and 14 that I am creating a new view and calling render on it directly as a reference.  This felt very wrong as there is now a coupling between the views. Thankfully somebody on twitter kindly pointed me in the direction of this &lt;a href="http://lostechies.com/derickbailey/2011/07/19/references-routing-and-the-event-aggregator-coordinating-views-in-backbone-js/" target="_blank"&gt;post&lt;/a&gt; from &lt;a href="https://twitter.com/#!/derickbailey" target="_blank"&gt;Derek Bailey&lt;/a&gt; who is a wealth of information on Backbone.js.&lt;/p&gt;

&lt;p&gt;The post outlines an alternative to creating and coupling one view in another like in the example above with the ManagementReportView.  We instead create a central object that manages the raising of events and the subscribers for those events.  Martin Fowler calls this the &lt;a href="http://martinfowler.com/eaaDev/EventAggregator.html"&gt;Event Aggregator&lt;/a&gt; pattern.  This serves the purpose of decoupling the views from each other.  The entire listing of the event aggregator is on line 3 of the following gist:&lt;/p&gt;

&lt;div&gt;&lt;script src='https://gist.github.com/1362205.js?file='&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;code&gt;class App
  init: -&amp;gt;
    vent = _.extend({}, Backbone.Events)
    businessUnitsView = new BusinessUnitsView({vent: vent, el: $('#businessUnits')})   
    managementView = new ManagementReportView({vent: vent, el: $('#managementReport')}) &lt;/code&gt;&lt;/pre&gt;&lt;/noscript&gt;&lt;/div&gt;


&lt;p&gt;We are taking advantage of the way Backbone already handles events.  The Backbone &lt;strong&gt;Events&lt;/strong&gt; object is a module that can be mixed in with any object, giving the object the ability to bind and trigger custom named events.  On lines 4 and 5, we are making sure that any of the views that want to publish or subscribe to events have a reference to the &lt;strong&gt;vent&lt;/strong&gt; object.  With this in place, I can simply raise an event from the BusinessUnitView and pass the newly created collection as an event argument.  Below is the BusinessUnitView refactored to take advantage of the event aggregator:&lt;/p&gt;

&lt;div&gt;&lt;script src='https://gist.github.com/1362495.js?file='&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;code&gt;class BusinessUnitsView extends MIBaseView
  initialize: (options) -&amp;gt;
    @vent = options.vent
    options.vent.bind('showBusinessUnits', @showBusinessUnits)

  events:
    'click #generate': &amp;quot;generate&amp;quot;

  generate: (e) =&amp;gt;    
    e.preventDefault()

    identifiers = _.map( $('input:checked'), (check) -&amp;gt; 
                    {Uid: $(check).val(), Title: $(check).parents(&amp;quot;tr:eq(0)&amp;quot;).find(&amp;quot;label&amp;quot;).html()}
                  )

    if identifiers.length == 0
      showErrorMessage('You have not selected any businss units.')
      return    

    @vent.trigger('reportbusinessunitview:load', new BusinessUnits(identifiers))&lt;/code&gt;&lt;/pre&gt;&lt;/noscript&gt;&lt;/div&gt;


&lt;p&gt;The first addition is on line 3 where the view obtains a reference to our extended Backbone.js &lt;strong&gt;Events&lt;/strong&gt; object and on line 20 where the custom &lt;strong&gt;Events&lt;/strong&gt; object is used to &lt;strong&gt;trigger&lt;/strong&gt; a custom event that is uniquely identified by a string.  We are using the convention of a colon separated string (&lt;strong&gt;reportingbusinessunitview:load&lt;/strong&gt;) to namespace the events and avoid collisions.  Below is the code listing for the ManagementReportView that subscribes to the event listed above:&lt;/p&gt;

&lt;div&gt;&lt;script src='https://gist.github.com/1365243.js?file='&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;code&gt;class ManagementReportView extends MIBaseView
  initialize: (options) -&amp;gt;    
    options.vent.bind(&amp;quot;reportbusinessunitview:unload&amp;quot;, @onClose)
    options.vent.bind(&amp;quot;reportbusinessunitview:load&amp;quot;, @load)
    @vent = options.vent    

  load: (businessUnits) =&amp;gt;
    @collection = businessUnits
    @collection.bind 'reset', @render
    @collection.fetch()

  onClose: =&amp;gt;    
    @collection.unbind('reset') if @collection
    @disposeViews()

  render: =&amp;gt;
    self = @

    @collection.each (item, counter) -&amp;gt;
      self.renderView(self.el, 'append', new ReportBusinessUnitView(model: item, vent: self.vent))&lt;/code&gt;&lt;/pre&gt;&lt;/noscript&gt;&lt;/div&gt;


&lt;p&gt;On line 3 of the above gist, we are using the &lt;strong&gt;bind&lt;/strong&gt; method of the Events object to specify a callback that will be triggered when the event is raised and any optional arguments can be passed to the callback which in this instance is the newly created businessunits backbone collection that we created from the user input in the previous view. On line 7 we define the callback event handler.  The &lt;strong&gt;load&lt;/strong&gt; callback on line 7 is declared using the fat arrow &lt;strong&gt;=&gt;&lt;/strong&gt; syntax.  Coffeescript will take care of binding methods to &lt;em&gt;this&lt;/em&gt; if you declare a function using the fat arrow &lt;strong&gt;=&gt;&lt;/strong&gt; instead of the skinny one &lt;strong&gt;-&gt;&lt;/strong&gt;.  This means you don't need to use the underscore &lt;strong&gt;_.bindAll&lt;/strong&gt; method as I would in javascript which can be quite tedious and relies on your diligence.  One of the many reasons why you should be using coffeescript.  On line 8 of the load callback we are setting the view's collections reference to the argument raised from the triggered event and line 9 brings us to our next gotcha:&lt;/p&gt;

&lt;h3&gt;Gotcha 2 - Handle Backbone Model Events and not DOM Events Wherever Possible&lt;/h3&gt;

&lt;p&gt;Part of my reason for warming to backbone is that it brings conformity and organisation to my JavaScript/Coffeescript.  It is all too easy for traditional browser based javascript to descend into a sea of DOM event handlers.  Wherever possible, I have my views respond to backbone model or collection events.  On line 9 of the above gist, I am adding an event handler to the &lt;strong&gt;reset&lt;/strong&gt; event which is fired whenever a backbone collection is replaced with a bulk insert.  On line 10 we are calling the built in &lt;strong&gt;fetch&lt;/strong&gt; method of the backbone collection to retrieve the data from the remote server.  When the data returns successfully from the remote server, the &lt;em&gt;reset&lt;/em&gt; event is fired and we have bound the render event as a callback to this event on line 16.  You can subscribe to events that are fired whenever elements are added or removed from a collection or when an individual model's attributes change and you should get accustomed to all of these behaviours.  Retrieving the data from the remote server brings me to gotcha 3 and in effect the render method:&lt;/p&gt;

&lt;h3&gt;Gotcha 3 - SRP - Keep Your Views Skinny&lt;/h3&gt;

&lt;p&gt;It is all to easy to end up with bloated views that do way to much and my first few attempts at backbone suffered from this.  I've seen code that make calls to the remote server in the view and this is wrong. Let the collection/model do the remote call and subscribe to an appropriate backbone collection/model event.  Below is the listing of the BusinessUnits object:&lt;/p&gt;

&lt;div&gt;&lt;script src='https://gist.github.com/1365331.js?file='&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;code&gt;class BusinessUnit extends Backbone.Model

class BusinessUnits extends Backbone.Collection
  model: BusinessUnit

  url: -&amp;gt;
    &amp;quot;#{_getBusinessUnitScores}#{@getUids()}&amp;quot;

  getUids: -&amp;gt;
    @pluck(&amp;quot;Uid&amp;quot;).join(',')&lt;/code&gt;&lt;/pre&gt;&lt;/noscript&gt;&lt;/div&gt;


&lt;p&gt;As this is javascript (coffeescript really) we can override any of the built in backbone collection methods like I am doing with the url method on line 6 but you can if required override &lt;strong&gt;fetch&lt;/strong&gt; or &lt;strong&gt;parse&lt;/strong&gt; methods for tighter control.&lt;/p&gt;

&lt;p&gt;It is worth noting that the render method creates further subviews to attach to the parent view's element. When refactoring backbone code, you tend to split your views up into finer detail or subviews to further inforce SRP.&lt;/p&gt;

&lt;p&gt;The inner workings of the render method brings me to my last and most troublesome gotcha.&lt;/p&gt;

&lt;h3&gt;Gotcha 4 - Kill All Your Zombies.....Dead&lt;/h3&gt;

&lt;p&gt;I cannot take the credit for the excellent zombie analogy or in fact any of the revelations in this post, this again goes to Derek Bailey and a combination of this &lt;a href="http://lostechies.com/derickbailey/2011/09/15/zombies-run-managing-page-transitions-in-backbone-apps/" target="_blank"&gt;post&lt;/a&gt; and this &lt;a href="http://stackoverflow.com/questions/7567404/backbone-js-repopulate-or-recreate-the-view/7607853#7607853"&gt;Stackoverflow&lt;/a&gt; question and answer session.  I have combined the contents of the above 2 links into how I now handle this common scenario.&lt;/p&gt;

&lt;p&gt;If we refer back to the screenshot I showed at the start:
&lt;img class='' src='http://thesoftwaresimpleton.com//images/backbone/mi.png' width='' height='' alt='' title=''&gt;
I want to render a subview for every row in the above screenshot that has a cross or a tick image in the table columns.  The gotcha outlined here is that I need to dispose of these views whenever the &lt;strong&gt;Back&lt;/strong&gt; button on the top right is clicked.  If I don't dispose of the subviews, new and additional subviews will be added every time the user transitions to this view with a new collection selection.  Multiple event handlers and general round chaos will occur and I have been there and it can be distressing to the uninitiated.  There will be a number of views that are not attached to any element floating about in memory which gives them their zombie status.&lt;/p&gt;

&lt;p&gt;Let us take a look again at the render method:&lt;/p&gt;

&lt;div&gt;&lt;figure role=code&gt;&lt;pre&gt;&lt;code&gt;render: =&gt;
  self = @

  @collection.each (item, counter) -&gt;
    self.renderView(self.el, 'append', new ReportBusinessUnitView(model: item, vent: self.vent))&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;


&lt;p&gt;Here we are simply iterating over our collection and appending onto this view's element a new subview for each element of the collection.  We are actually calling a method named &lt;strong&gt;renderView&lt;/strong&gt; of our own &lt;strong&gt;BaseView&lt;/strong&gt; class that extends the Backbone.View class.  More on that later but for completeness, below is the ReportBusinessUnitView class:&lt;/p&gt;

&lt;div&gt;&lt;script src='https://gist.github.com/1365460.js?file='&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;code&gt;class ReportBusinessUnitView extends MIBaseView
  initialize: (options) -&amp;gt;
    @vent = options.vent
    @template = _.template($('#businessunit_template').html())

  events:
    &amp;quot;click .individualBu&amp;quot;: &amp;quot;showBusinessUnitDetail&amp;quot;

  showBusinessUnitDetail: (e) =&amp;gt;
    e.preventDefault()    
    self = @   

    @vent.trigger('management:showbusinessunitdeail', @model)

  render: =&amp;gt;
    $(@el).html(@template(@model.toJSON()))
    @&lt;/code&gt;&lt;/pre&gt;&lt;/noscript&gt;&lt;/div&gt;


&lt;p&gt;I've also found underscore's templating to be much faster than JQuery's templating engine which I initially started out using. Below is the underscore template:&lt;/p&gt;

&lt;div&gt;&lt;script src='https://gist.github.com/1365477.js?file='&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;code&gt;&amp;lt;script type=&amp;quot;text/template&amp;quot; id=&amp;quot;businessunit_template&amp;quot;&amp;gt;
  &amp;lt;tr data-uid=&amp;quot;{{Uid}}&amp;quot;&amp;gt;
    &amp;lt;td class=&amp;quot;first&amp;quot;&amp;gt;&amp;lt;span&amp;gt;{{Name}}&amp;lt;/span&amp;gt;&amp;lt;/td&amp;gt;
    &amp;lt;td class=&amp;quot;{{StatusClass}} tac&amp;quot;&amp;gt;{{OverallScore}}%&amp;lt;/td&amp;gt;
    &amp;lt;td&amp;gt;
        &amp;lt;a class=&amp;quot;impactanalysis individualBu&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt; &amp;lt;/a&amp;gt;
    &amp;lt;/td&amp;gt;
  &amp;lt;/tr&amp;gt;
&amp;lt;/script&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;/noscript&gt;&lt;/div&gt;


&lt;p&gt;The code will take care of iterating over our collection and attaching the subviews to our parent view.  But if the user pressed back and then chose another selection and transitioned to this view again, we would add further subviews to this parent view.  We need some way of keeping track of and disposing of our subviews.&lt;/p&gt;

&lt;p&gt;To get round this problem, we have defined our own base class that uses underscore's extend method to &lt;em&gt;mixin&lt;/em&gt; additional functionality with that of the Backbone.Collection class by adding custom methods onto the Backbone.Collections prototype.&lt;/p&gt;

&lt;div&gt;&lt;script src='https://gist.github.com/1368362.js?file='&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;code&gt;class BaseView
  constructor: (options) -&amp;gt;
    Backbone.View.apply(@, [options])

  _.extend(BaseView.prototype, Backbone.View.prototype, {

    renderView: (el, func, view) -&amp;gt;
      $.fn[func].call(el, view.render().el)
      @views ||= []
      @views.push view&lt;/code&gt;&lt;/pre&gt;&lt;/noscript&gt;&lt;/div&gt;


&lt;p&gt;On line 7, we are defining a method named &lt;strong&gt;renderView&lt;/strong&gt; which takes a jquery wrapped DOM element, a string denoting a function of the JQuery object to call and a backbone view.  On line 8, we use javascript's ability to reference and call a method on an object like accessing a hashtable. This string will generally be something like 'append' or a means of attaching the subview to the DOM.  On line 9 we intialise an array to hold the subviews if it has not already been initailised using coffeescript's ruby like &lt;strong&gt;||=&lt;/strong&gt; operator.  Finally we push the newly created view onto the array.&lt;/p&gt;

&lt;p&gt;We call the method like this:&lt;/p&gt;

&lt;div&gt;&lt;figure role=code&gt;&lt;pre&gt;&lt;code&gt;@collection.each (item, counter) -&gt;
    self.renderView(self.el, 'append', new ReportBusinessUnitView(model: item, vent: self.vent))&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;


&lt;p&gt;We pass the view's element, the string &lt;strong&gt;'append'&lt;/strong&gt; which will be called on the jquery object in the renderView method like this &lt;strong&gt;$.fn[func].call&lt;/strong&gt; and the subview we want attached.  This is very neat and I can take absolutely no credit for writing it.  We now have a store of all the views that are added.&lt;/p&gt;

&lt;p&gt;All that remains is a means of disposing of these views and below are the two methods that take care of this on our custom BaseView class:&lt;/p&gt;

&lt;div&gt;&lt;script src='https://gist.github.com/1368467.js?file='&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;code&gt;    unbindFromAll: -&amp;gt;
      _.each(@bindings, (binding) -&amp;gt;
        binding.model.unbind(binding.ev, binding.callback)
      )

      @bindings = []    

    dispose: () -&amp;gt;
      @disposeViews()
      @unbindFromAll()
      @unbind()
      @remove()

    disposeViews: -&amp;gt;     
      if @views
        _(@views).each (view) -&amp;gt;
          view.dispose()
      @views = []
  })&lt;/code&gt;&lt;/pre&gt;&lt;/noscript&gt;&lt;/div&gt;


&lt;p&gt;We can either call &lt;strong&gt;dispose&lt;/strong&gt; on line 8   that will remove all subviews and the view itself or &lt;strong&gt;disposeViews&lt;/strong&gt; on line 14 that will only remove the subviews.  Dispose takes care of not only unbinding any event handlers but also of removing the element from the DOM. We can simply call one of these methods in an appropriate place on the view like this:&lt;/p&gt;

&lt;div&gt;&lt;figure role=code&gt;&lt;pre&gt;&lt;code&gt;@disposeViews()&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;


&lt;p&gt;This has made backbone much more manageable although I cannot claim credit for much of the code.&lt;/p&gt;

&lt;p&gt;That is it for this post and I will be amazed if anyone has made it this far.  Please feel free to leave any comments.  Below is the full listing of the BaseView class:&lt;/p&gt;

&lt;div&gt;&lt;script src='https://gist.github.com/1367986.js?file='&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;code&gt;class BaseView
  constructor: (options) -&amp;gt;
    @bindings = []
    Backbone.View.apply(@, [options])

  _.extend(BaseView.prototype, Backbone.View.prototype, {
    bindTo: (model, ev, callback) -&amp;gt;
      model.bind(ev, callback, @)
      @bindings.push({model: model, ev: ev, callback: callback})

    unbindFromAll: -&amp;gt;
      _.each(@bindings, (binding) -&amp;gt;
        binding.model.unbind(binding.ev, binding.callback)
      )

      @bindings = []    

    dispose: () -&amp;gt;
      @disposeViews()
      @unbindFromAll()
      @unbind()
      @remove()

    renderView: (el, func, view) -&amp;gt;
      $.fn[func].call(el, view.render().el)
      @views ||= []
      @views.push view

    disposeViews: -&amp;gt;     
      if @views
        _(@views).each (view) -&amp;gt;
          view.dispose()
      @views = []
  })

@.BaseView = BaseView

@.BaseView.extend = Backbone.View.extend&lt;/code&gt;&lt;/pre&gt;&lt;/noscript&gt;&lt;/div&gt;

</content>
  </entry>
  
  <entry>
    <title>Simple CSS3 Login Form Using SASS</title>
    <link href="http://thesoftwaresimpleton.com//blog/2011/09/26/simple-css3-login-form-using-sass/" />
    <updated>2011-09-26T08:34:00+01:00</updated>
    <id>http://thesoftwaresimpleton.com//blog/2011/09/26/simple-css3-login-form-using-sass</id>
    <content type="html">&lt;h3&gt;Previous Posts&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://thesoftwaresimpleton.com//blog/2011/09/07/site-refresh-with-html5-and-sass---part-1/"&gt;Part 1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://thesoftwaresimpleton.com//blog/2011/09/08/site-refresh-with-html5-and-sass---part-2/"&gt;Part 2&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://thesoftwaresimpleton.com//blog/2011/09/18/creating-gradients-with-compass-and-sass/"&gt;Part 3: Creating Gradients With Compass&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;I have been using these posts to cement the knowledge I am gleaning while transforming the markup for a product I am building.  I find that trying to transmit the knowledge via blogging is the only way to really learn something or else it is easily forgotten.  In the last three posts I have gone from defining my  project file structure to creating the markup and CSS3 (with the extreme help of sass) for the header element in the image below.  In this post, I want to outline the steps I took to create the following login page:&lt;/p&gt;

&lt;p&gt;&lt;img class='' src='http://thesoftwaresimpleton.com//images/simple-form/login.png' width='' height='' alt='' title=''&gt;&lt;/p&gt;

&lt;p&gt;In the past, I hate to admit that I would have used a table to align the inputs with their labels but this time round I want to use css to control the layout.  With that said, below is the haml that generates the markup for the above login form:&lt;/p&gt;

&lt;div&gt;&lt;script src='https://gist.github.com/1242152.js?file='&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;code&gt;=form_for @user_session, :id =&amp;gt; 'login', :url =&amp;gt; user_session_path do |f|
  =f.error_messages
  %h2 
    Log In
  %fieldset#inputs
    =f.label :email
    =f.text_field(:email, {:placeholder =&amp;gt; &amp;quot;email&amp;quot;, :autofocus =&amp;gt; &amp;quot;autofocus&amp;quot;, :required =&amp;gt; &amp;quot;required&amp;quot;, :autocomplete =&amp;gt; &amp;quot;off&amp;quot;})
    =f.label :password
    =f.password_field(:password, {:placeholder =&amp;gt; &amp;quot;password&amp;quot;})
  %fieldset#actions
    =f.submit(&amp;quot;Log In&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&lt;/noscript&gt;&lt;/div&gt;


&lt;p&gt;The above haml generates the following markup:&lt;/p&gt;

&lt;div&gt;&lt;script src='https://gist.github.com/1242169.js?file='&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;code&gt;&amp;lt;form accept-charset=&amp;quot;UTF-8&amp;quot; action=&amp;quot;/user_session&amp;quot; class=&amp;quot;new_user_session&amp;quot; id=&amp;quot;new_user_session&amp;quot; method=&amp;quot;post&amp;quot;&amp;gt;
    &amp;lt;div style=&amp;quot;margin:0;padding:0;display:inline&amp;quot;&amp;gt;&amp;lt;input name=&amp;quot;utf8&amp;quot; type=&amp;quot;hidden&amp;quot; value=&amp;quot;&amp;amp;#x2713;&amp;quot; /&amp;gt;
         &amp;lt;input name=&amp;quot;authenticity_token&amp;quot; type=&amp;quot;hidden&amp;quot; value=&amp;quot;5Q0RXjMIdCbnW8vGYTPYpa+UCg1F8TPQ+EOSDTcQ0Ck=&amp;quot; /&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;h2&amp;gt;Log In&amp;lt;/h2&amp;gt;
    &amp;lt;fieldset id='inputs'&amp;gt;
      &amp;lt;label for=&amp;quot;user_session_email&amp;quot;&amp;gt;Email&amp;lt;/label&amp;gt;
      &amp;lt;input autocomplete=&amp;quot;off&amp;quot; autofocus=&amp;quot;autofocus&amp;quot; id=&amp;quot;user_session_email&amp;quot; name=&amp;quot;user_session[email]&amp;quot; placeholder=&amp;quot;email&amp;quot; required=&amp;quot;required&amp;quot; size=&amp;quot;30&amp;quot; type=&amp;quot;text&amp;quot; /&amp;gt;
      &amp;lt;label for=&amp;quot;user_session_password&amp;quot;&amp;gt;Password&amp;lt;/label&amp;gt;
      &amp;lt;input id=&amp;quot;user_session_password&amp;quot; name=&amp;quot;user_session[password]&amp;quot; placeholder=&amp;quot;password&amp;quot; size=&amp;quot;30&amp;quot; type=&amp;quot;password&amp;quot; /&amp;gt;
    &amp;lt;/fieldset&amp;gt;
    &amp;lt;fieldset id='actions'&amp;gt;
       &amp;lt;input name=&amp;quot;commit&amp;quot; type=&amp;quot;submit&amp;quot; value=&amp;quot;Log In&amp;quot; /&amp;gt;
     &amp;lt;/fieldset&amp;gt;
&amp;lt;/form&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;/noscript&gt;&lt;/div&gt;


&lt;p&gt;The only points of interest in the above markup are the new HTML5 attributes that are attached to the input elements on lines 8 and 10 of the above gist.  These attributes are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;a href="http://dev.w3.org/html5/spec-author-view/common-input-element-attributes.html#the-required-attribute" target="_blank"&gt;required&lt;/a&gt; attribute which as you might have correctly deduced is used to specify that the element requires user input.  Some browsers will alter the default appearance of required elements with a red border for example and most should stop you submitting the form if the mandatory field is left blank.  It is worth remembering that this is HTML5, things can and will change until ratification.&lt;/li&gt;
&lt;li&gt;The &lt;a href="http://dev.w3.org/html5/spec-author-view/common-input-element-attributes.html#the-placeholder-attribute" target="_blank"&gt;placeholder&lt;/a&gt; attribute which if set will place default text in an input field if the input element is empty or not in focus.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;With the markup out of the way, we can now concentrate on some of the new CSS3 bells and whistles that I have used via sass and compass to create some of the nice effects in the form.&lt;/p&gt;

&lt;p&gt;First of all, I want to concentrate on the login form itself:
&lt;img class='' src='http://thesoftwaresimpleton.com//images/simple-form/login-form.png' width='' height='' alt='' title=''&gt;
Below is the sass that is used to create the sunken effect for the text of the header and label elements that are outlined in the above image.&lt;/p&gt;

&lt;div&gt;&lt;script src='https://gist.github.com/1242249.js?file='&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;code&gt;@mixin form-labels
  color: $label-color
  text-shadow: 0 1px 0px $label-box-shadow

form label
  @include form-labels
  float: left
  text-transform: uppercase

form h2
  font-size: 35px
  @include form-labels&lt;/code&gt;&lt;/pre&gt;&lt;/noscript&gt;&lt;/div&gt;


&lt;p&gt;On line 1, is the definition of a mixin that I have used to avoid duplication in the two css selectors that assign styles to the &amp;lt;h2&amp;gt; and &amp;lt;label&amp;gt; elements.  The mixin is used on lines 6 and 12 via the @include directive.&lt;br/&gt;
On line 3 of the above gist, I am using the new css3 text-shadow property that does what it says on the tin and adds a shadow effect to the text of a block element to make it stand out more.&lt;/p&gt;

&lt;p&gt;It is worth noting that I keep all my colour variable definitions in a separate sass partial file named &lt;strong&gt;_colors.sass&lt;/strong&gt;.  This allows me to change the colour scheme in one file and not have to jump around the project files looking for the definitions.  Below is an excerpt from &lt;strong&gt;colors.sass&lt;/strong&gt; that shows the variable declarations used in the above gist:&lt;/p&gt;

&lt;div&gt;&lt;figure role=code&gt;&lt;pre&gt;&lt;code&gt;$label-color: #445668
$label-box-shadow: #f2f2f2&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;


&lt;p&gt;The form element of the log in page uses a few of the new css3 techniques to add the new and now infamous css3 border-radius property that gives the form the nice rounded corners effect.  It is often jokingly stated that the main purpose of CSS3 is to facilitate the adding of rounded corners to block elements.&lt;/p&gt;

&lt;p&gt;But first of all, I want to show how I got this rippled effect on the bottom of the form:
&lt;img class='' src='http://thesoftwaresimpleton.com//images/simple-form/form-bottom.png' width='' height='' alt='' title=''&gt;
I have used the new css3 box-shadow property to achieve this effect. As the name suggests, the box-shadow property is used to apply an inset or drop shadow to a block element.  It is also possible to add multiple box-shadows to an element as in this example.  As this is a css3 experimental property there are the usual vendor specific prefixes for this property....that is unless you use compass and you can use the &lt;a href="http://compass-style.org/reference/compass/css3/box_shadow/" target="_blank"&gt;box-shadow mixin&lt;/a&gt; like I am below:&lt;/p&gt;

&lt;div&gt;&lt;figure role=code&gt;&lt;pre&gt;&lt;code&gt;#new_user_session
  @include box-shadow($login-box-shadow-color 0 0 2px, $login-box-shadow-color 0 1px 1px, #fff 0 3px 0, $login-box-shadow-color 0 4px 0, #fff 0 6px 0, $login-box-shadow-color 0 7px 0)&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;


&lt;p&gt;I am passing in alternating box-shadow colour definitions to the mixin that are delimited by commas to give the appearance of a ripple.  The above sass generates the following css:&lt;/p&gt;

&lt;div&gt;&lt;figure role=code&gt;&lt;pre&gt;&lt;code&gt;#new_user_session {
  -moz-box-shadow: rgba(0, 0, 0, 0.2) 0 0 2px, rgba(0, 0, 0, 0.2) 0 1px 1px, white 0 3px 0, rgba(0, 0, 0, 0.2) 0 4px 0, white 0 6px 0, rgba(0, 0, 0, 0.2) 0 7px 0;
  -webkit-box-shadow: rgba(0, 0, 0, 0.2) 0 0 2px, rgba(0, 0, 0, 0.2) 0 1px 1px, white 0 3px 0, rgba(0, 0, 0, 0.2) 0 4px 0, white 0 6px 0, rgba(0, 0, 0, 0.2) 0 7px 0;
  -o-box-shadow: rgba(0, 0, 0, 0.2) 0 0 2px, rgba(0, 0, 0, 0.2) 0 1px 1px, white 0 3px 0, rgba(0, 0, 0, 0.2) 0 4px 0, white 0 6px 0, rgba(0, 0, 0, 0.2) 0 7px 0;
  box-shadow: rgba(0, 0, 0, 0.2) 0 0 2px, rgba(0, 0, 0, 0.2) 0 1px 1px, white 0 3px 0, rgba(0, 0, 0, 0.2) 0 4px 0, white 0 6px 0, rgba(0, 0, 0, 0.2) 0 7px 0;
}&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;


&lt;p&gt;The above css creates the rippled effect.&lt;/p&gt;

&lt;p&gt;Now let us get back to those nice rounded corners that are this season's black in the fashionable world of css3:
&lt;img class='' src='http://thesoftwaresimpleton.com//images/simple-form/box-radius.png' width='' height='' alt='' title=''&gt;
The css3 border-radius property is used to give a block element rounded corners and as we cannot be bothered to type all these vendor specific prefixed properties, we are going to use the compass &lt;a href="http://compass-style.org/reference/compass/css3/border_radius/"&gt;border-radius mixin&lt;/a&gt;.
Below is the border-radius mixin applied to the login form element:&lt;/p&gt;

&lt;div&gt;&lt;figure role=code&gt;&lt;pre&gt;&lt;code&gt;#new_user_session
  @include border-radius(10px)&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;


&lt;p&gt;Which generates the following css:&lt;/p&gt;

&lt;div&gt;&lt;figure role=code&gt;&lt;pre&gt;&lt;code&gt;#new_user_session{
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
-o-border-radius: 10px;
-ms-border-radius: 10px;
-khtml-border-radius: 10px;
border-radius: 10px;
}&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;


&lt;p&gt;As you can see, compass takes care of the vendor specific pain.&lt;/p&gt;

&lt;p&gt;Below is the sass in its entirety that is employed to create the css styling effects of the form minus the inputs.&lt;/p&gt;

&lt;div&gt;&lt;script src='https://gist.github.com/1242453.js?file='&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;code&gt;#new_user_session
  @include box-shadow($login-box-shadow-color 0 0 2px, $login-box-shadow-color 0 1px 1px, #fff 0 3px 0, $login-box-shadow-color 0 4px 0, #fff 0 6px 0, $login-box-shadow-color 0 7px 0)
  padding: 30px 40px 20px 40px
  margin: 0px 1px 5px 1px
  @include border-radius(10px)
  @include background(linear-gradient(darken($login-background, 10), #FFF))
  @include center-horizontally(320px)
  
#new_user_session 
  label
    width: 85px
    margin-right: 12px
    text-align: right
  h2
    text-align: center
    text-transform: uppercase
  input[type=submit]
    float: right
    margin-top: 5px&lt;/code&gt;&lt;/pre&gt;&lt;/noscript&gt;&lt;/div&gt;


&lt;p&gt;The only point that I have not covered is the gradient effect that is created using the compass border mixin on line 6 of the above gist.  I described linear gradients in the previous &lt;a href="http://thesoftwaresimpleton.com//blog/2011/09/18/creating-gradients-with-compass-and-sass/"&gt;post&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Input Styling&lt;/h2&gt;

&lt;p&gt;I have used a combination of all the techniques mentioned so far to add some definition to the input elements:
&lt;img class='' src='http://thesoftwaresimpleton.com//images/simple-form/inputs.png' width='' height='' alt='' title=''&gt;
Below is the sass that is used to create the box-shadow, linear-gradient and border radius effects:&lt;/p&gt;

&lt;div&gt;&lt;script src='https://gist.github.com/1242480.js?file='&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;code&gt;input, textarea
  padding: 9px
  border: 1px solid $text-box-border
  width: 200px
  background: $text-box-bg

input
  @include border-radius(5px)

input, text    
  @include background-image(image-url('noise.png'), linear-gradient($text-box-from-gradient, $text-box-to-gradient))&lt;/code&gt;&lt;/pre&gt;&lt;/noscript&gt;&lt;/div&gt;


&lt;h3&gt;CSS3 Transitions&lt;/h3&gt;

&lt;p&gt;With CSS3 you can now achieve some effects that would have in the past required javascript to achieve.  One of these techniques is known as a &lt;a href="http://www.w3schools.com/css3/css3_transitions.asp" target="_blank"&gt;css3 transition&lt;/a&gt;.  This allows the author to specify a css change or transition from one css style to another.  It is also possible to put a time delay on the transition.&lt;/p&gt;

&lt;p&gt;This is impossible to show without a demo and as my site is not on a public facing server yet, so I am going to have to point to these &lt;a href="http://24ways.org/2009/going-nuts-with-css-transitions" target="_blank"&gt;examples&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In my log in page, I am specifying that I want the text input's border and background to change when the cursor is hovered over the input control:
&lt;img class='' src='http://thesoftwaresimpleton.com//images/simple-form/input-hover.png' width='' height='' alt='' title=''&gt;
We can also put a delay on the transition which equates to a tasteful delay of a specified time before the transition occurs.&lt;/p&gt;

&lt;p&gt;Below is the sass I have used to create this nice time delayed transition:&lt;/p&gt;

&lt;div&gt;&lt;script src='https://gist.github.com/1242522.js?file='&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;code&gt;input, textarea
  &amp;amp;:hover
    background: darken($text-box-from-gradient, 10)
    border-color: $text-box-hover

input, textarea
  @include transition-property(background)
  @include transition-duration(0.5s)&lt;/code&gt;&lt;/pre&gt;&lt;/noscript&gt;&lt;/div&gt;


&lt;p&gt;I am using the compass transition-property and transition-duration mixins to produce this effect.  The compass documentation for the transition mixins can be found &lt;a href="http://compass-style.org/examples/compass/css3/transition/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Button Effect&lt;/h2&gt;

&lt;p&gt;Lastly I want to describe how the styling was created for the button:
&lt;img class='' src='http://thesoftwaresimpleton.com//images/simple-form/button.png' width='' height='' alt='' title=''&gt;
I have used the &lt;a href="http://brandonmathis.com/projects/fancy-buttons/"&gt;fancy-buttons&lt;/a&gt; library to easily create this effect.&lt;/p&gt;

&lt;p&gt;With the library imported, I can simply use the fancy-button mixin to generate the css3 styles like this:&lt;/p&gt;

&lt;div&gt;&lt;figure role=code&gt;&lt;pre&gt;&lt;code&gt;input[type=submit]
  @include fancy-button(#617798, 14px, 1em, 4px)
  width: auto
  text-transform: uppercase&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;


&lt;p&gt;I will leave things here for this article, if you made it to the end of the article then pat yourself on the back!&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <title>Creating Gradients With Compass and SASS</title>
    <link href="http://thesoftwaresimpleton.com//blog/2011/09/18/creating-gradients-with-compass-and-sass/" />
    <updated>2011-09-18T12:08:00+01:00</updated>
    <id>http://thesoftwaresimpleton.com//blog/2011/09/18/creating-gradients-with-compass-and-sass</id>
    <content type="html">&lt;h3&gt;Previous Posts&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://thesoftwaresimpleton.com//blog/2011/09/07/site-refresh-with-html5-and-sass---part-1/"&gt;Part 1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://thesoftwaresimpleton.com//blog/2011/09/08/site-refresh-with-html5-and-sass---part-2/"&gt;Part 2&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;In the previous two posts, I have started to outline the transition of my yet to be completed product from old school xhtml, css and javascript into the new world of HTML5 and CSS3.  As the product is not even on the market yet, I am taking the bold step of tackling the design myself.  I am a css and design heretic so this is quite a difficult step for me.  On the plus side, I will learn a lot on the way no matter what the outcome is. In this article, I want to touch on creating gradients with sass and compass.&lt;/p&gt;

&lt;p&gt;A gradient in the context of css is a gradual transition between two colours.  This transition can be transformed through a vertical axis or a horizontal axis.  Below is how my application looks after I have applied the styles that I will create in this article.&lt;br/&gt;
&lt;img class='' src='http://thesoftwaresimpleton.com//images/post3/gradients.png' width='' height='' alt='' title=''&gt;
I have applied gradients to both the header section and the navigation bar of the html document.  CSS3 comes with a gradient property and most of the modern browsers have their own prefixed slants on this property.  As we are using compass and sass, we can forget about the need to create these vendor specific duplicated linear-gradient properties.  Yet another reason for using compass is that compass will output the vendor specific cross browser rules.  The latest version of compass has the excellent &lt;a href="http://compass-style.org/reference/compass/css3/images/" target="_blank"&gt;images module&lt;/a&gt; and I am going to take advantage of the &lt;strong&gt;background-image&lt;/strong&gt; mixin to create the gradient effect.  Below is the rule that will be applied to the new html5 &amp;lt;header&amp;gt; element of our document:&lt;/p&gt;

&lt;div&gt;&lt;figure role=code&gt;&lt;pre&gt;&lt;code&gt;body &gt; header
  background-color: $header-bg
  @include background-image(image-url('noise.png'), linear-gradient(darken($header-bg, 20), $header-bg, lighten($header-bg, 11)))&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;


&lt;p&gt;I am using the &lt;strong&gt;background-image&lt;/strong&gt; mixin to generate the linear-gradient as recommended in the latest &lt;a href="http://compass-style.org/CHANGELOG/" target="_blank"&gt;compass docs changelog&lt;/a&gt; which states:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;The linear-gradient and radial-gradient mixins have been deprecated. Instead use the background-image mixin and pass it a gradient function. The deprecation warning will print out the correct call for you to use.&lt;/p&gt;&lt;/blockquote&gt;


&lt;p&gt;We are also passing in a fallback image if the browser does not support linear-gradient.&lt;/p&gt;

&lt;p&gt;For IE6 and IE7 there is the &lt;a href="http://compass-style.org/reference/compass/css3/images/#mixin-filter-gradient" target="_blank"&gt;filter-gradient&lt;/a&gt; mixin.  I will probably create an &lt;strong&gt;IE &amp;lt; 8&lt;/strong&gt; conditional stylesheet and try this out at a later stage.&lt;/p&gt;

&lt;p&gt;It is also worth noting that we are taking advantage of the sass functions lighten and darken to avoid having multiple colour hex values to change.  The above &lt;strong&gt;background-image&lt;/strong&gt; mixin will generate the following css:&lt;/p&gt;

&lt;div&gt;&lt;figure role=code&gt;&lt;pre&gt;&lt;code&gt;/* line 1, /Users/paulcowan/projects/leadcapturer/app/assets/stylesheets/partials/_header.sass */
body &gt; header {
  background-color: #6a85af;
  background-image: url(/assets/noise.png), -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #acbbd3), color-stop(50%, #6a85af), color-stop(100%, #4f6992));
  background-image: url(/assets/noise.png), -webkit-linear-gradient(#acbbd3, #6a85af, #4f6992);
  background-image: url(/assets/noise.png), -moz-linear-gradient(#acbbd3, #6a85af, #4f6992);
  background-image: url(/assets/noise.png), -o-linear-gradient(#acbbd3, #6a85af, #4f6992);
  background-image: url(/assets/noise.png), -ms-linear-gradient(#acbbd3, #6a85af, #4f6992);
  background-image: url(/assets/noise.png), linear-gradient(#acbbd3, #6a85af, #4f6992);
}&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;


&lt;p&gt;I hope you can see the projection of one line of vendor independent compass/sass code to the many lines of browser/vendor specific css code.&lt;/p&gt;

&lt;h2&gt;Navigation Bar&lt;/h2&gt;

&lt;p&gt;We will use the same technique to apply a gradient to the navigation bar:
&lt;img class='' src='http://thesoftwaresimpleton.com//images/post3/nav.png' width='' height='' alt='' title=''&gt;
We are going to use the new semantic &amp;lt;nav&amp;gt; element of html5 to mark up the navigation and then apply css rules to it.  Below is the haml for the new navigation bar:&lt;/p&gt;

&lt;div&gt;&lt;script src='https://gist.github.com/1225066.js?file='&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;code&gt;  %nav{:role =&amp;gt; &amp;quot;navigation&amp;quot;}
    %ul{:role =&amp;gt; &amp;quot;user&amp;quot;}
      %li
        =link_to &amp;quot;log Out&amp;quot;, logout_path
    %ul{:role =&amp;gt; &amp;quot;main-navigation&amp;quot;}
      %li 
        =link_to &amp;quot;Capture&amp;quot;, {:action =&amp;gt; &amp;quot;Index&amp;quot;, :controller =&amp;gt; &amp;quot;Home&amp;quot;}, :method =&amp;gt; :get
      %li
        =link_to &amp;quot;Archives&amp;quot;, {:action =&amp;gt; &amp;quot;Index&amp;quot;, :controller =&amp;gt; &amp;quot;Archive&amp;quot;}, :method =&amp;gt; :get&lt;/code&gt;&lt;/pre&gt;&lt;/noscript&gt;&lt;/div&gt;


&lt;p&gt;And here is the rendered output of the above haml&lt;/p&gt;

&lt;div&gt;&lt;figure role=code&gt;&lt;pre&gt;&lt;code&gt;&amp;lt;nav role="navigation"&gt;
    &amp;lt;ul role="user"&gt;
      &amp;lt;li&gt;
        &amp;lt;a href="http://thesoftwaresimpleton.com//logout"&gt;log Out&amp;lt;/a&gt;
      &amp;lt;/li&gt;
    &amp;lt;/ul&gt;
    &amp;lt;ul role="main-navigation"&gt;
      &amp;lt;li&gt;
        &amp;lt;a href="http://thesoftwaresimpleton.com//Home/Index" data-method="get"&gt;Capture&amp;lt;/a&gt;
      &amp;lt;/li&gt;
      &amp;lt;li&gt;
        &amp;lt;a href="http://thesoftwaresimpleton.com//Archive/Index" data-method="get"&gt;Archives&amp;lt;/a&gt;
      &amp;lt;/li&gt;
    &amp;lt;/ul&gt;
&amp;lt;/nav&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;


&lt;p&gt;I am also using the new html5 semantic &lt;a href="http://www.w3.org/wiki/PF/XTech/HTML5/RoleAttribute" target="_blank"&gt;role attribute&lt;/a&gt;. HTML5 has many of these new semantic markers that help devices such as screen readers pick out the relevant sections.&lt;/p&gt;

&lt;p&gt;I want to take this opportunity to show the power of both sass variables and the extremely useful sass functions desaturate, darken and lighten to &lt;strong&gt;DRY&lt;/strong&gt; up your css.  Below are  the variable declarations we will be using for the navigation bar:&lt;/p&gt;

&lt;div&gt;&lt;script src='https://gist.github.com/1225069.js?file='&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;code&gt;$nav-bg: desaturate(lighten(#9CC889, 18),5)
$nav-border: darken($nav-bg, 10)
$nav-border-top: lighten($nav-bg, 15)
$nav-border-bottom: darken($nav-bg, 25)
$nav-border-left: darken($nav-bg, 11)
$nav-border-right: lighten($nav-bg, 7)
$nav-color: darken($nav-bg, 38)
$nav-color-hover: darken($nav-color, 25)&lt;/code&gt;&lt;/pre&gt;&lt;/noscript&gt;&lt;/div&gt;


&lt;p&gt;Only on line 1 do we actually specify any hex value for a variable, in the subsequent variable declarations, we pass in relevant percentage values to the lighten and darken functions to keep everything in ratio. A change to the &lt;strong&gt;$nav-bg&lt;/strong&gt; variable will cause the other variables to adjust accordingly.&lt;/p&gt;

&lt;p&gt;A full list of similar sass colour functions can be found &lt;a href="http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html" target-"_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Below is the listing for the &lt;strong&gt;_navigation.sass&lt;/strong&gt; partial file that contains the rules for the &amp;lt;nav&amp;gt; tag outlined above:&lt;/p&gt;

&lt;div&gt;&lt;script src='https://gist.github.com/1225073.js?file='&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;code&gt;body &amp;gt; nav
  position: relative
  background-color: $nav-bg
  @include background-image(image-url('noise.png'), linear-gradient(lighten($nav-bg, 8), $nav-bg, darken($nav-bg, 11)))
  border
    top: 1px solid $nav-border-top
    bottom: 1px solid $nav-border-bottom
  padding-bottom: .35em
  padding-top: .35em
  ul
    @include horizontal-list(0)
    float: left
  ul[role=user]
    float: right
    li:last-child a
      padding-right: 0
    li
      margin: 0
  a
    @include link-colors($nav-color, $nav-color-hover, $visited: $nav-color)
    @extend .sans
    text-shadow: lighten($nav-bg, 12) 0 1px
    float: left
    text-decoration: none
    padding: .1em 0
  li + li
    border-left: $nav-border-left
    margin-left: .8em
    a
      padding-left: .8em
      border-left: 1px solid $nav-border-right&lt;/code&gt;&lt;/pre&gt;&lt;/noscript&gt;&lt;/div&gt;


&lt;p&gt;Besides the &lt;strong&gt;background-image&lt;/strong&gt; mixin on line 4 that I described previously, we are taking advantage of a couple of other useful sass mixins:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://compass-style.org/reference/compass/utilities/lists/horizontal_list/" target="_blank"&gt;Horizontal List&lt;/a&gt; (line 11): which transforms a &amp;lt;ul&amp;gt; list horizontally.&lt;/li&gt;
&lt;li&gt;&lt;a href="http://compass-style.org/reference/compass/utilities/links/link_colors/" target="_blank"&gt;link-colors&lt;/a&gt; (line 20): Which sets the colors for a link in one mixin.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;I will leave things here for this post but in the next post I will describe the layout I am going to use for the main section of the page.  I had previously used the compass blueprint library which is a css grid layout framework but the world has moved on and I am going to see what else is available.  If anyone can recommend a more modern approach please leave a comment below.&lt;/p&gt;

&lt;p&gt;Please leave any comments below.  Feedback is always appreciated.&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <title>Site Refresh With HTML5 and SASS - Part 2</title>
    <link href="http://thesoftwaresimpleton.com//blog/2011/09/08/site-refresh-with-html5-and-sass---part-2/" />
    <updated>2011-09-08T07:58:00+01:00</updated>
    <id>http://thesoftwaresimpleton.com//blog/2011/09/08/site-refresh-with-html5-and-sass---part-2</id>
    <content type="html">&lt;h3&gt;Previous Posts&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://thesoftwaresimpleton.com//blog/2011/09/07/site-refresh-with-html5-and-sass---part-1/"&gt;Part 1&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;In the last &lt;a href="" title="http://www.thesoftwaresimpleton.com/blog/2011/09/07/site-refresh-with-html5-and-sass---part-1/"&gt;post&lt;/a&gt; I took a walk through the basic set up of my sass files that I will be using for the update of my site to the new landscape of html5.&lt;/p&gt;

&lt;p&gt;Below is the file layout for my sass files:
&lt;img class='' src='http://thesoftwaresimpleton.com//images/site-refresh-part1/sass-structure.png' width='' height='' alt='' title=''&gt;
I went on to mention that the &lt;strong&gt;application.css.sass&lt;/strong&gt; file above is used as a manifest and I just use it to point to my application point of entry &lt;strong&gt;screen.sass&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;application.css.sass looks like this:&lt;/p&gt;

&lt;div&gt;&lt;figure role=code&gt;&lt;pre&gt;&lt;code&gt;@import "screen"&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;


&lt;p&gt;screen.sass currently looks like this:&lt;/p&gt;

&lt;div&gt;&lt;script src='https://gist.github.com/1201022.js?file='&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;code&gt;@import 'compass'
@include global-reset
@include reset-html5

@import 'partials/utilities'
@import 'partials/colors'
@import 'partials/typography'
@import 'partials/layout'
@import 'partials/header'
@import 'partials/navigation'
@import 'partials/footer'&lt;/code&gt;&lt;/pre&gt;&lt;/noscript&gt;&lt;/div&gt;


&lt;p&gt;In the last post, I explained how the reset mixins in lines 2 and 3 provide a facility for removing browser inconsistencies by a mechanism known as css reset rules.  I now want to explain the contents of the other files that are imported via the sass &lt;strong&gt;@import&lt;/strong&gt; directive.&lt;/p&gt;

&lt;h2&gt;SASS Partials&lt;/h2&gt;

&lt;p&gt;All the files that are imported on lines 5 to 12 of screen.sass are sass partials.  Any sass file that is imported as part of a parent or main css file and does not need its own individual css file is what is known as a &lt;em&gt;partial&lt;/em&gt;.  A sass partial file does not make sense on its own.  In this example, screen.sass will generate one css file that is made up of seven partials.  This is very analgous to rails partials or any partial in any mvc framework.  The convention for a sass partial is to begin the filename with a &lt;strong&gt;_&lt;/strong&gt; character. If you refer to the opening image, each file in the partials directory begins with an underscore.  This tells sass not to generate an individual css file for the partial and only use it for imports.  To confuse matters, you drop both the &lt;em&gt;_&lt;/em&gt; and the file extension in the @import statement.&lt;/p&gt;

&lt;p&gt;The first import statement on line 5 imports a currently empty file named &lt;em&gt;_utilities.sass&lt;/em&gt;.  This file will be used for housing any reusable mixins or sass functions that we create on our journey to market.  Next up we have the partial &lt;em&gt;_colors.sass&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;SASS Variables&lt;/h2&gt;

&lt;p&gt;_colors.sass currently looks like this:&lt;/p&gt;

&lt;div&gt;&lt;script src='https://gist.github.com/1205627.js?file='&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;code&gt;$header-bg: #6A85AF !default
$title-color: #f2f2f2 !default
$subtitle-color: lighten($header-bg, 58)

$type-border: #ddd !default

$text-color: #222 !default
$text-color-light: #aaa !default&lt;/code&gt;&lt;/pre&gt;&lt;/noscript&gt;&lt;/div&gt;


&lt;p&gt;The point of &lt;em&gt;_colors.sass&lt;/em&gt; is to have a central location were we can adjust the colour scheme of the website.  The explicit colour values that will be referenced in other sass files for things like background colours etc. are stored in what are known as sass variables.&lt;/p&gt;

&lt;p&gt;One of the glaring misses in CSS is the ability to store reusable values in variables.  This can lead you to having to use potentially faulty search and replace techniques in your text editor to swap hex code values and manage colour palette changes in your stylesheets.  With sass you can assign values to variables, and manage colours, border sizes etc. in a single location.&lt;/p&gt;

&lt;p&gt;SASS variables start with the &lt;em&gt;$&lt;/em&gt; character and can contain any character that is valid in a CSS class name.  In line 1 of &lt;strong&gt;_colors.sass&lt;/strong&gt;, I am storing the hex value of the colour that I will use for the header section of my page.  As colors.sass is imported in screen.sass, the variables that are declared in _colors.sass will be available in any partials that are imported in screen.sass.  Below is an example of how the sass variable can be used:&lt;/p&gt;

&lt;div&gt;&lt;figure role=code&gt;&lt;pre&gt;&lt;code&gt;body &gt; header
  background: $header-bg&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;


&lt;p&gt;Line 3 of _colors.sass contains the following variable declaration:&lt;/p&gt;

&lt;div&gt;&lt;figure role=code&gt;&lt;pre&gt;&lt;code&gt;$subtitle-color: lighten($header-bg, 58)&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;


&lt;p&gt;The return value of the lighten function that is outlined above is used to assign the value of the $subtitle-color variable and is one of the many utility functions that are included in SASS.  The lighten function unsurprisingly lightens the colour by a percentage value.  In the above example, we can tie the $subtitle-color variable to the $header-bg variable so they will always be in ratio.  A quick look at the &lt;a href="http://sass-lang.com/" target="_bland"&gt;sass docs&lt;/a&gt; will broaden your knowledge of what sass functions are available.&lt;/p&gt;

&lt;p&gt;Before carrying on, below is the rendered HTML from our main application.html.haml file which we will be applying css rules to:&lt;/p&gt;

&lt;div&gt;&lt;script src='https://gist.github.com/1202722.js?file='&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;head&amp;gt;
  &amp;lt;meta charset='utf-8'&amp;gt;
  &amp;lt;meta author='Paul Cowan'&amp;gt;
  &amp;lt;title&amp;gt;Lead Generator&amp;lt;/title&amp;gt;
  &amp;lt;link href=&amp;quot;/assets/application.css&amp;quot; media=&amp;quot;screen&amp;quot; rel=&amp;quot;stylesheet&amp;quot; type=&amp;quot;text/css&amp;quot; /&amp;gt;
  &amp;lt;script src=&amp;quot;/assets/application.js&amp;quot; type=&amp;quot;text/javascript&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;meta content=&amp;quot;authenticity_token&amp;quot; name=&amp;quot;csrf-param&amp;quot; /&amp;gt;
  &amp;lt;meta content=&amp;quot;PNhdEuNsWQC2xIrHP8Ae4SspAEtSCXiSr9exmzHI2po=&amp;quot; name=&amp;quot;csrf-token&amp;quot; /&amp;gt;
  &amp;lt;link href='http://fonts.googleapis.com/css?family=Orbitron:regular,italic,bold,bolditalic' rel='stylesheet' type='text/css'&amp;gt;
  &amp;lt;link href='http://fonts.googleapis.com/css?family=PT+Serif:regular,italic,bold,bolditalic' rel='stylesheet' type='text/css'&amp;gt;
  &amp;lt;link href='http://fonts.googleapis.com/css?family=PT+Sans:regular,italic,bold,bolditalic' rel='stylesheet' type='text/css'&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
  &amp;lt;header&amp;gt;
    &amp;lt;hgroup&amp;gt;
      &amp;lt;h1&amp;gt;
        &amp;lt;a href=&amp;quot;/Home/Index&amp;quot; data-method=&amp;quot;get&amp;quot;&amp;gt;Lead Capturer&amp;lt;/a&amp;gt;
        &amp;lt;h2&amp;gt;Superior quality Lead Generation&amp;lt;/h2&amp;gt;
      &amp;lt;/h1&amp;gt;
    &amp;lt;/hgroup&amp;gt;
  &amp;lt;/header&amp;gt;
&amp;lt;/body&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;/noscript&gt;&lt;/div&gt;


&lt;h2&gt;Typography&lt;/h2&gt;

&lt;p&gt;The next sass partial file that we are importing from the parent screen.sass is the &lt;strong&gt;_typography.sass&lt;/strong&gt; file, which currently looks like this:&lt;/p&gt;

&lt;div&gt;&lt;script src='https://gist.github.com/1218685.js?file='&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;code&gt;.heading
  font-family: &amp;quot;Orbitron&amp;quot;, &amp;quot;Georgia&amp;quot;, &amp;quot;Helvetica Neue&amp;quot;, Arial, San-Serif 
  
.sans
  font-family: &amp;quot;PT Sans&amp;quot;, &amp;quot;Helvetica Neue&amp;quot;, Arial, sans-serif
  
.serif
  font-family: &amp;quot;PT Serif&amp;quot;, Georgia, Times, &amp;quot;Times New Roman&amp;quot;, serif

body &amp;gt; header h1
  font-size: 2.2em
  @extend .heading
  line-height: 1.2em
  text-transform: uppercase
  font-weight: normal
  margin-bottom: 0.6667em
  
body
  line-height: 1.5em
  color: $text-color
  @extend .serif
  
#{headings()}
  @extend .heading
  text-rendering: optimizelegibility
  margin-bottom: 1em&lt;/code&gt;&lt;/pre&gt;&lt;/noscript&gt;&lt;/div&gt;


&lt;p&gt;This file as the file name suggests is where all the type face instructions will be centrally held.  On line 1 of _typography.sass, we are declaring a css class with a name of heading and then declaring the font-family we want to use and the fall back options.  It is unlikely you will have heard of the Orbitron font-family and that is because we are importing it from the &lt;a href="http://code.google.com/apis/webfonts/" target="_blank"&gt;Google Web Fonts API&lt;/a&gt;.  I mentioned this in the previous &lt;a href="http://www.thesoftwaresimpleton.com/blog/2011/09/07/site-refresh-with-html5-and-sass---part-1/" target="_blank"&gt;post&lt;/a&gt;.  We can import fonts from the google web fonts api by declaring a separate &amp;lt;link&amp;gt; element for each font we want to import. The declaration for importing the Orbitron font is below:&lt;/p&gt;

&lt;div&gt;&lt;figure role=code&gt;&lt;pre&gt;&lt;code&gt;%link{:rel =&gt; "stylesheet", :type =&gt; "text/css", :href =&gt; "http://fonts.googleapis.com/css?family=Orbitron:regular,italic,bold,bolditalic"}&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;


&lt;p&gt;On line 4 of the typography.sass file, we are using the css selector &lt;strong&gt;body &gt; header h1&lt;/strong&gt; to select the first &lt;strong&gt;h1&lt;/strong&gt; element of the new html5 &lt;strong&gt;header&lt;/strong&gt; element. On line 7 of &lt;strong&gt;_typography.sass&lt;/strong&gt;, &lt;strong&gt;@extend&lt;/strong&gt; is used to tell sass that we want the &lt;strong&gt;body &gt; header h1&lt;/strong&gt; selector to inherit all the styles defined in another selector.  This is worth the sass price of admission alone.  The ability to &lt;strong&gt;DRY&lt;/strong&gt; up your css by inheriting other selector rules reduces repeating yourself tenfold. This is one of the many reasons why sass will (not should) become the new css.&lt;/p&gt;

&lt;p&gt;In line 11 of _typography.sass we come across the first use of what is arguably sass's most important feature which is known as a &lt;em&gt;sass mixin&lt;/em&gt;.  A &lt;em&gt;mixin&lt;/em&gt; as the name suggests, mixes in rules into other rules.  We extract the rules using the &lt;strong&gt;@mixin&lt;/strong&gt; directive.  For example we could create a &lt;em&gt;mixin&lt;/em&gt; defined like below:&lt;/p&gt;

&lt;div&gt;&lt;figure role=code&gt;&lt;pre&gt;&lt;code&gt;@mixin BigText
  font-size: 20px
  font-weight: bold
  text-transform: uppercase&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;


&lt;p&gt;We can &lt;em&gt;mixin&lt;/em&gt; these rules into other rules using the &lt;strong&gt;@include&lt;/strong&gt; directive.&lt;/p&gt;

&lt;div&gt;&lt;figure role=code&gt;&lt;pre&gt;&lt;code&gt;body &gt; header h1
  @include BigText&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;


&lt;p&gt;It is also possible to pass arguments to mixins just as is outlined in line 11 of typography where we are mixing in the rules of the compass &lt;a href="http://compass-style.org/examples/compass/css3/text_shadow/" target="_blank"&gt;Text-Shadow mixin&lt;/a&gt;.  This mixin will create the rules for the new css3 text shadow property.  The CSS3 text-shadow property has been around for some time now and is commonly used to recreate Photoshop’s Drop Shadow type shading to add subtle shadows which help add depth, dimension and to lift an element from the page.&lt;/p&gt;

&lt;div&gt;&lt;figure role=code&gt;&lt;pre&gt;&lt;code&gt;@include text-shadow(rgba(#000, 0.8) 0 0 8px)&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;


&lt;p&gt;Below is a screenshot of what the some total of our css styling looks so far:
&lt;img class='' src='http://thesoftwaresimpleton.com//images/site-refresh-part2/h1-part1.png' width='' height='' alt='' title=''&gt;
I think you will agree that this looks pretty ugly but as we continue, I will progressively make it look better:
&lt;img class='' src='http://thesoftwaresimpleton.com//images/site-refresh-part2/h1-part2.png' width='' height='' alt='' title=''&gt;
As you can see, I am not a designer but this looks reasonable enough to me but probably less so to others.  Ok, back to sass...&lt;/p&gt;

&lt;p&gt;On line 18 of _typography.sass, we come across the strange syntax of   &lt;strong&gt;#{headings()}&lt;/strong&gt;.  This helper &lt;a href="http://compass-style.org/reference/compass/helpers/selectors/#headings" target="_blank"&gt;function&lt;/a&gt; is part of the compass library.  This helper function will emit all the h[n] headings for you.  Below is the css that is rendered from this function:&lt;/p&gt;

&lt;div&gt;&lt;figure role=code&gt;&lt;pre&gt;&lt;code&gt;.heading, body &gt; header h1, h1, h2, h3, h4, h5, h6 {
  font-family: "Orbitron", "Georgia", "Helvetica Neue", Arial, San-Serif;
}&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;


&lt;p&gt;I am going to leave things here for now but in the next post I will outline some other cool sass techniques such as gradients.&lt;/p&gt;

&lt;p&gt;Please feel free to leave comments below.&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <title>Site Refresh With HTML5 and SASS - Part 1</title>
    <link href="http://thesoftwaresimpleton.com//blog/2011/09/07/site-refresh-with-html5-and-sass---part-1/" />
    <updated>2011-09-07T10:51:00+01:00</updated>
    <id>http://thesoftwaresimpleton.com//blog/2011/09/07/site-refresh-with-html5-and-sass---part-1</id>
    <content type="html">&lt;p&gt;I have been working on &lt;a href="http://www.leadcapturer.com/" target="_blank"&gt;this&lt;/a&gt; as a side project for some time now and I have decided to take a deep dive into what is available in HTML5 and CSS3 and update the front end code. This yet to be birthed product is written in JRuby and is a rails application.  I have recently been through the pain of upgrading to Rails 3.1 in order to take advantage of the dazzlingly new, shinny and controversial Rails 3.1 &lt;a href="http://guides.rubyonrails.org/asset_pipeline.html" target="_blank"&gt;asset pipeline&lt;/a&gt;.  I have been using sass and compass for some time and I had previously used the compass blueprint library for layout and utilities.  This feels a bit dated now so it is high time to refresh.  I have previously blogged about sass, compass and blueprint &lt;a href="http://thesoftwaresimpleton.blogspot.com/2010/03/rails-day-6-blueprint-compass-sass-and.html" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I am trying to avoid dissolving into deep depressive rants about things and I could go off on one about the Rails 3.1 abstraction levels reaching their natural meltdown limits with this Rails release.  It is not quite .NET webforms but it is certainly heading in that direction.&lt;/p&gt;

&lt;h2&gt;Markup Refresh&lt;/h2&gt;

&lt;p&gt;I am going to take this site refresh one blog post at a time.  I want to be sure I remember my transitions.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;head&lt;/strong&gt; is the only sensible place to start in HTML so here is my new header code which is written in your favourite mark up language and mine &lt;a href="http://haml-lang.com/" target="_blank"&gt;HAML&lt;/a&gt;:&lt;/p&gt;

&lt;div&gt;&lt;script src='https://gist.github.com/1200025.js?file='&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;code&gt;%head
  %meta{:charset =&amp;gt; &amp;quot;utf-8&amp;quot;}
  %meta{:author =&amp;gt; &amp;quot;Paul Cowan&amp;quot;}
  %title Lead Generator
  =stylesheet_link_tag    &amp;quot;application&amp;quot; 
  =javascript_include_tag &amp;quot;application&amp;quot; 
  =csrf_meta_tags
  %link{:rel =&amp;gt; &amp;quot;stylesheet&amp;quot;, :type =&amp;gt; &amp;quot;text/css&amp;quot;, :href =&amp;gt; &amp;quot;http://fonts.googleapis.com/css?family=Orbitron:regular,italic,bold,bolditalic&amp;quot;}
  %link{:rel =&amp;gt; &amp;quot;stylesheet&amp;quot;, :type =&amp;gt; &amp;quot;text/css&amp;quot;, :href =&amp;gt; &amp;quot;http://fonts.googleapis.com/css?family=PT+Serif:regular,italic,bold,bolditalic&amp;quot;}
  %link{:rel =&amp;gt; &amp;quot;stylesheet&amp;quot;, :type =&amp;gt; &amp;quot;text/css&amp;quot;, :href =&amp;gt; &amp;quot;http://fonts.googleapis.com/css?family=PT+Sans:regular,italic,bold,bolditalic&amp;quot;}
%body
  %header
    %hgroup
      %h1 
        =link_to &amp;quot;Lead Capturer&amp;quot;, {:action =&amp;gt; &amp;quot;Index&amp;quot;, :controller =&amp;gt; &amp;quot;Home&amp;quot;}, :method =&amp;gt; :get
        %h2 Superior quality Lead Generation&lt;/code&gt;&lt;/pre&gt;&lt;/noscript&gt;&lt;/div&gt;


&lt;p&gt;The above is my main rails layout file.  The following are the main points of interest:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In HAML the HTML declaration is a two pronged approach.  You first of all place the doctype marker &lt;strong&gt;!!!&lt;/strong&gt; in the .html.haml file:&lt;/li&gt;
&lt;/ul&gt;


&lt;div&gt;&lt;figure role=code&gt;&lt;pre&gt;&lt;code&gt;!!!
 %head&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;


&lt;p&gt;  Secondly, you need to tell HAML the output format on application start up which is specified in the main rails &lt;strong&gt;environment.rb&lt;/strong&gt; file.&lt;/p&gt;

&lt;div&gt;&lt;figure role=code&gt;&lt;pre&gt;&lt;code&gt;Haml::Template.options[:format] = :html5&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;


&lt;p&gt;  With these options set, we can pass the rendering of the correct &lt;strong&gt;!DOCTYPE&lt;/strong&gt; instruction to the HAML runtime.
  This generates the following HTML declaration:&lt;/p&gt;

&lt;div&gt;&lt;figure role=code&gt;&lt;pre&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;&lt;p&gt;On lines 3 and 5 of the gist, we are declaring the single application javascript and css declarations that are generated from the Rails 3.1 &lt;a href="http://guides.rubyonrails.org/asset_pipeline.html" target="_blank"&gt;asset pipeline&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;On lines 8 - 11 we are going to take advantage of the &lt;a href="http://code.google.com/apis/webfonts/" target="_blank"&gt;Google Web Fonts API&lt;/a&gt; and CSS3 to embed some nice looking fonts in the application.  There is a separate declaration and import for each web font that the application will make use of.  More on this in later posts.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;On line 13 we encounter our first new HTML5 tag, the &lt;strong&gt;header&lt;/strong&gt; element.  The semantic &lt;strong&gt;header&lt;/strong&gt; tag specifies an introduction, or a group of navigation elements for the document.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;On line 14, the &lt;strong&gt;hgroup&lt;/strong&gt; tag is used to group the document's title and associated subtitles.  The &lt;strong&gt;hgroup&lt;/strong&gt; tag can only contain a group of &lt;em&gt;h1&lt;/em&gt; to &lt;em&gt;h6&lt;/em&gt; elements.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Below is the beautifully indented output of the HAML markup:&lt;/p&gt;

&lt;div&gt;&lt;script src='https://gist.github.com/1202722.js?file='&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;head&amp;gt;
  &amp;lt;meta charset='utf-8'&amp;gt;
  &amp;lt;meta author='Paul Cowan'&amp;gt;
  &amp;lt;title&amp;gt;Lead Generator&amp;lt;/title&amp;gt;
  &amp;lt;link href=&amp;quot;/assets/application.css&amp;quot; media=&amp;quot;screen&amp;quot; rel=&amp;quot;stylesheet&amp;quot; type=&amp;quot;text/css&amp;quot; /&amp;gt;
  &amp;lt;script src=&amp;quot;/assets/application.js&amp;quot; type=&amp;quot;text/javascript&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;meta content=&amp;quot;authenticity_token&amp;quot; name=&amp;quot;csrf-param&amp;quot; /&amp;gt;
  &amp;lt;meta content=&amp;quot;PNhdEuNsWQC2xIrHP8Ae4SspAEtSCXiSr9exmzHI2po=&amp;quot; name=&amp;quot;csrf-token&amp;quot; /&amp;gt;
  &amp;lt;link href='http://fonts.googleapis.com/css?family=Orbitron:regular,italic,bold,bolditalic' rel='stylesheet' type='text/css'&amp;gt;
  &amp;lt;link href='http://fonts.googleapis.com/css?family=PT+Serif:regular,italic,bold,bolditalic' rel='stylesheet' type='text/css'&amp;gt;
  &amp;lt;link href='http://fonts.googleapis.com/css?family=PT+Sans:regular,italic,bold,bolditalic' rel='stylesheet' type='text/css'&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
  &amp;lt;header&amp;gt;
    &amp;lt;hgroup&amp;gt;
      &amp;lt;h1&amp;gt;
        &amp;lt;a href=&amp;quot;/Home/Index&amp;quot; data-method=&amp;quot;get&amp;quot;&amp;gt;Lead Capturer&amp;lt;/a&amp;gt;
        &amp;lt;h2&amp;gt;Superior quality Lead Generation&amp;lt;/h2&amp;gt;
      &amp;lt;/h1&amp;gt;
    &amp;lt;/hgroup&amp;gt;
  &amp;lt;/header&amp;gt;
&amp;lt;/body&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;/noscript&gt;&lt;/div&gt;


&lt;h2&gt;SASS Folder Structure&lt;/h2&gt;

&lt;p&gt;When I first started using SASS there was nowhere near the level of docs and blog posts available to the end user.  As part of this site refresh, I took a look on &lt;a href="http://github.com/" target="_blank"&gt;github&lt;/a&gt; at some of the open source projects using SASS to find guidance on how some of the SASS officionados, use SASS.&lt;/p&gt;

&lt;p&gt;It is also worth mentioning before proceeding that I use the possibly now old school and pythonesque sass syntax and not the more css like scss syntax.  When I started with sass there was no scss syntax but to be honest, I think .scss was introduced to make sass more designer friendly.  As a developer, I feel more at home with whitespace delimiting my blocks rather than the now horribly out of fashion curly braces.&lt;/p&gt;

&lt;p&gt;The screenshot below outlines a popular file organisation pattern for structuring your SASS files:&lt;/p&gt;

&lt;p&gt;&lt;img class='' src='http://thesoftwaresimpleton.com//images/site-refresh-part1/sass-structure.png' width='' height='' alt='' title=''&gt;&lt;/p&gt;

&lt;p&gt;The first point of entry is the &lt;strong&gt;application.css.sass&lt;/strong&gt; file.  Back in what are probably now classified as the old days, a separate process running the &lt;strong&gt;compass.exe&lt;/strong&gt; would watch the sass directory for changes to your sass files.  Any changes were compiled into your ~/public folder.  With rails 3.1, we're getting css and javascript served up through application.css and application.js.  These main application files are really just manifest files that bundle everything in the &lt;em&gt;stylesheets&lt;/em&gt; or &lt;em&gt;javascripts&lt;/em&gt; folder into one file.&lt;/p&gt;

&lt;h3&gt;First Rails 3.1 SASS Gotcha&lt;/h3&gt;

&lt;p&gt;Out of the box, the application.css.sass file looks something like this:&lt;/p&gt;

&lt;div&gt;&lt;figure role=code&gt;&lt;pre&gt;&lt;code&gt;/*
 * This is a manifest file that'll automatically include all the stylesheets available in this directory
 * and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
 * the top of the compiled file, but it's generally better to create a new file per style scope.
 *= require_self
 *= require_tree . 
*/&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;


&lt;p&gt;The &lt;em&gt;requite_tree&lt;/em&gt; statement above tells the system to bundle everything in the stylesheets folder into a single file via the magic of sprockets.&lt;/p&gt;

&lt;p&gt;When I first started using this approach in Rails 3.1, I quickly found out that sass variables or indeed sass scopes were not shared across files.  This is because application.css.sass is really for combining plain css files into one via the magic of &lt;strong&gt;sprockets&lt;/strong&gt;.  With sass files, you need to use the &lt;strong&gt;@import&lt;/strong&gt; directive in the &lt;strong&gt;application.css.sass&lt;/strong&gt; file to import additional sass files.  If you are using application.css.scss with &lt;em&gt;require_tree&lt;/em&gt;, the runtime will still compile your sass and combine the resultant css into one file but there will be no shared scopes for things like variables, mixins etc.  It took me a while to figure that one out.  Remember I mentioned the raised abstraction levels in Rails 3.1?&lt;/p&gt;

&lt;p&gt;With that in mind, my application.css.sass file looks like this:&lt;/p&gt;

&lt;div&gt;&lt;figure role=code&gt;&lt;pre&gt;&lt;code&gt;@import "screen"&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;


&lt;p&gt;The &lt;strong&gt;application.css.sass&lt;/strong&gt; file is now just a pointer to the main application logic file, &lt;strong&gt;screen.sass&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;At this time of writing, my &lt;strong&gt;screen.sass&lt;/strong&gt; file currently looks like this:&lt;/p&gt;

&lt;div&gt;&lt;script src='https://gist.github.com/1201022.js?file='&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;code&gt;@import 'compass'
@include global-reset
@include reset-html5

@import 'partials/utilities'
@import 'partials/colors'
@import 'partials/typography'
@import 'partials/layout'
@import 'partials/header'
@import 'partials/navigation'
@import 'partials/footer'&lt;/code&gt;&lt;/pre&gt;&lt;/noscript&gt;&lt;/div&gt;


&lt;p&gt;On line 1 we are using the &lt;strong&gt;@import&lt;/strong&gt; directive to import the &lt;a href="http://compass-style.org/reference/compass/" target="_blank"&gt;compass&lt;/a&gt; library into our code.  Using sass without compass is a complete waste of an incredible resource, compass has a wealth of useful features that are hard to ignore.  If you are not using compass with sass, you might as well be using the &lt;strong&gt;less&lt;/strong&gt; framework.  Compass has a multitude of mixins, functions and other utilities that make css all of a sudden fun to a css heretic like me.&lt;/p&gt;

&lt;p&gt;Lines 2 and 3 of &lt;em&gt;screen.sass&lt;/em&gt; bring the output or generated styles of two of these compass mixins into the rendered &lt;strong&gt;application.css&lt;/strong&gt; output via the sass &lt;strong&gt;@include&lt;/strong&gt; directive.  The &lt;strong&gt;@include&lt;/strong&gt; directive takes the name of a mixin and optionally arguments to pass to it, and includes the styles defined by the mixin into the current rule.  The following &lt;a href="http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#including_a_mixin" target="_blank"&gt;link&lt;/a&gt; has some examples that illustrate how they can be used.&lt;/p&gt;

&lt;p&gt;Both these mixins generate what are known as css reset rules.  Reset stylesheets try to reduce browser inconsistencies in things like default line heights, paddings, margins and font sizes.  The goal of the reset stylesheet is to give you a completely style free starting point.  For example some browsers indent unordered lists, others do not.  The reset rules level the playing field and gives you a neutral cross browser starting point to add your own.&lt;/p&gt;

&lt;p&gt;The first mixin, the &lt;strong&gt;global-reset&lt;/strong&gt; mixin is based on &lt;a href="http://meyerweb.com/eric/tools/css/reset/index.html" target="_blank"&gt;Eric Myer's 2.0&lt;/a&gt; global reset rules.&lt;/p&gt;

&lt;p&gt;The second mixin, the &lt;strong&gt;reset-html5&lt;/strong&gt; mixin, provides a basic set of HTML5 elements so they are rendered correctly in browsers that do not recognise them and reset in browsers that have default styles for them.&lt;/p&gt;

&lt;p&gt;Below is the css that is produced from these mixins:&lt;/p&gt;

&lt;div&gt;&lt;script src='https://gist.github.com/1201540.js?file='&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;code&gt;/* line 17, /Users/paulcowan/.rvm/gems/jruby-1.6.4/bundler/gems/compass-f23bf58e8db8/frameworks/compass/stylesheets/compass/reset/_utilities.scss */
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
  margin: 0;
  padding: 0;
  border: 0;
  font-size: 100%;
  font: inherit;
  vertical-align: baseline;
}&lt;/code&gt;&lt;/pre&gt;&lt;/noscript&gt;&lt;/div&gt;


&lt;p&gt;I think I prefer the 2 lines of sass code to the output above.  There are a number of compass reset mixins that can be utilised in your code.&lt;/p&gt;

&lt;p&gt;I think I will leave things here, in the next post I will reveal what is inside the other files in the above &lt;strong&gt;screen.sass&lt;/strong&gt; file.&lt;/p&gt;

&lt;p&gt;If you are a web developer and you are not using sass/compass, I have to ask the question....why?&lt;/p&gt;
</content>
  </entry>
  
</feed>

