Skip to content

A podcast for those who design, develop and run websites.

Boagworld is the personal website of Paul (the Wurzel) Boag who lives in the heart of rural Dorset. He produces a weekly podcast along with Marcus (pop star) Lillington on all things relating to building and running websites.

Latest shows

138. Freeform
In this week's show the entire boagworld production team answer listener questions.
137. Adobe
In this week's show, Aral Balkan joins us to discuss the release of Adobe CS4 and we discuss how not to get blacklisted by google.
136. Stagnation
In this week's show we talk about overcoming stagnation and Ed Merritt shares a technique to achieve fixed footers without the use of javaScript.
135. Libraries
In this week's show we talk with John Resig on javaScript libraries and address the question what is more important when we release an app: speed or quality?
134. Chrome
In this weeks show we give you advice on choosing the right hosting company, Teifion and John send us a review of dConstruct and of course we discuss the release of Google Chrome, can it topple IE?

or view all shows

Have your say

Leave a message for the show...

Buy my book: The website owners manual. A book for all those involved in designing, developing or running websites on a daily basis.

We are pleased to announce that Friday 10th October will be Boagworld discount day at the Head Conference site. Signup to attend on that day and you will get 20% off!

HTML snippets

Published on: February 18, 2008 by Paul Boag

If you are part of a web design team or skip constantly between projects, then you might want to consider an alternative approach to writing your HTML.

At Headscape efficiency is king. If you are efficient, you increase profit margins and keep prices competitive. You can only charge as much as the market allows. If you want to increase your profits you need to complete projects faster, while avoiding lowering quality.

As part of our efficiency drive we identified 4 problems...

  • Designers have to work with each others markup. We all code in different way and this creates a learning curve when a project gets passed between designers.
  • Integration HTML markup into server side applications was time consuming. Because designers coded in a different way and change their markup for each project, it was time consuming to apply that code to web applications like our in-house content management system.
  • Designers were constantly reinventing the wheel. Each project was built from the ground up with little reuse of markup across projects.
  • It was confusing switching between multiple projects. In order to ensure the most efficient use of time, designers are expected to work on several projects simultaneously. Unfortunately, switching between project is confusing because each had different markup.

We required some way to standardise tour markup.

Templating doesn't work

At first, we produced templates for the different types of pages. For example, we had news listing, text and FAQ templates. Although this worked, they were not very flexible. As soon as the content or functionality began to deviate from the norm the templates had to be heavily customised. This undermined the benefits they provided. They also didn't allow flexibility of design. Although content and design should be separate, it rarely works that way. Sometimes content needs to be marked up up in a particular order. Other times extra divs are required. The templates didn't accommodate either scenario.

We needed a more flexible approach.

Using snippets

The solution was HTML snippets. Content such as news listings, forms, navigation and FAQs appear in a vast majority of websites we build. By coding these in a consistent way each time they appear, we solve all of the efficiency problems mentioned above.

Take for example news listings. Most news listings look the same. They have...

  • Title
  • date
  • link
  • description
  • and sometimes an image

Because of this consistency you can code in the same way each time. Content will change, each will have its own unique id and sometimes an element might be dropped (e.g. the date or image). However, fundamentally the snippet is consistent

This consistency allows...

  • A designer picking up the code to instantly understand what is happening.
  • A developer to quickly integrate it with server side code because the integration is consistent every time.
  • Pages to be built faster because you are dropping in pre-generated markup

In affect all the designer has to do is build an HTML framework. This consists of the overall containers (main content, secondary content etc.) He then drop snippets into that framework in whatever order he requires.

However, the benefits don't stop there. You can also associate CSS with each snippet of HTML.

CSS fragments

If your HTML snippets are consistent, you can also build up a library of CSS fragments to support them. Take for example our news listing. Not only does it often contain the same content it is also often laid out in the same way. The image sits to the left while title, date and description sits next to it on the right. Because we know what the markup looks like, we can create this layout as a CSS fragment that can be dropped in whenever this HTML snippet is used.

We are not limited to a single CSS fragment per HTML snippet. Over time you can build up a variety of different CSS layouts for each snippet that can be used as the design dictates.

This approach provides a huge productivity benefit as the HTML and CSS can be built up in a 'pick and mix' fashion. However, you can also take the principle one step further and apply it to Javascript.

JavaScript functions

Each HTML snippet can have associated Javascript functions. These can be dropped in just like CSS fragments. These functions carry out common behaviour associated with that HTML snippet.

Take, for example, a FAQ snippet. A common behaviour with this snippet is to only display the answer when a question is clicked. Because we have consistent code in the snippet, it is easy to build a function that works with it and can be dropped in as required. Where possible keep your functions generic and not tied to a particular HTML snippet. However, where that cannot be done, we have standard HTML that allows us to reuse functions across projects with no editing.

Conclusions

In many ways this approach is a cross between microformats and frameworks and so in itself is not groundbreaking. However, from an efficiency standpoint, the impact is overwhelming. Without a doubt it will speed up development times and allow you to turn around projects quicker.

Comments

Comments are for the discussion of this post. If you have other questions / comments then post them to the forum or send me an email

  • Post by Mark Ford on February 18, 2008 4:49 PM

    I agree this is a very sensible approach - even when you're just working on one project reusing your code is the way to go.

    .net takes care of some of this with "user controls" but very often it'd be nice to just be able to plonk a very small fragment onto a page. As yet I haven't found a good place to store these, we've talked about setting up a wiki or something for them - How do you do it?

    Also how do you version control your snippets and ensure that the CSS (x)HTML & JS place nice if one of them chanes?

  • Post by paul Boag on February 18, 2008 5:08 PM

    Good question Mark. We use Basecamp. This allows us to have discussions around the development and adaptation of the snippets. Basecamp does allow some limited versioning so we use this while developing a new snippet. However, once a snippet has been settled upon it is added to subversion which manages it going forward.

  • Post by Matt McCabe on February 18, 2008 10:05 PM

    Thanks for the tip. This is definitely a time saver, it simplifies the coding process. As it allows you to drop-in the relevant section. You just need a consistent naming convention to go with it.

    Taking it a big further you could have different branches, maybe a snippet that is better suited to sidebars rather than in the main body.

  • Post by Josh Walsh on February 18, 2008 10:18 PM

    We've been doing the very same thing for about 6 months. Rather than using Basecamp we just share a Textmate bundle that we have created with all the snippets in there.

    Keep the bundle up to date with SVN and you have a create little snippet library that's always up to date.

  • Post by Mark Ford on February 19, 2008 3:25 PM

    I just realised that I can't type, that last sentence should read "... ensure that the CSS (x)HTML & JS plays nice if one of them changes?"

  • Post by David Mead on February 19, 2008 10:01 PM

    I've been using this in Dreamweaver for sometime as it comes with a good selection and lets you create you own.

    Dropping in some CSS to highlight areas/errors or having the unordered lists I use (complete with ids/classes) for navigation is a real time saver.

    Looking forward to seeing you again @ SXSW this year.

  • Post by Rozario Chivers on February 20, 2008 11:05 AM

    I think it's great that the discussion for component based approaches and their benefit has been opened up.

    After the frustration of page based development I attempted to craft a component based approach to Web develpment for about 3 years now. The framework I use has virtually the same concepts to mitigate problems with coding approaches and also to provide benefit of standardised markup, CSS and JavaScript.

    I managed to do a small presentation at Oxford Geek Night 28th Novemeber 2007 on this subject, tentatively called 'Nanoformats', which are essentially Microformats that are agreed within an organisation.

    http://www.slideshare.net/rozario/nanoformats/

    http://oxford.geeknights.net/2007/nov-28th/talks/RozarioChivers.mp4

    Myself and a few other Web Standardistas at www.salmon.com are doing our best to refine this approach (we have many more ideas) for it's use in the wild too.

  • Post by Darren on February 20, 2008 11:33 AM

    Great article, I have been working like this for years when writing my own CMS's.

    Can you define what you mean by template? Template at design level or at CMS level?

    My latest CMS has templates that are made up of head and body and then have sections replaced with content where it find comments to replace. This works a treat.

  • Post by Jonas Flint on February 24, 2008 6:01 PM

    I listened to the podcast about 3 days ago at my part time job and was primarily interested in the snippets section. I was a little disappointed that we weren't directed to any decent snippets resources, so I did a google search for "snippets manager" software and I found something great. Of course if you already have dreamweaver (which I do, but only occasionally use) you have this type of snippets manager built in (drag and drop). But I was looking for something similar, and standalone, and found this:
    Snippets Pro

    I liked it so much I became a affiliate, so the above link is an affiliate link. If this is a no - no, please slap me on the wrist and remove it. However the website to the software is here: http://www.snipitpro.com/ .

    Hope this helps anyone out.

Leave a comment

Additional Information

Supporting boagworld

Boagworld only exists thanks to the kind support of the following people. Check them out.

Speaking and writing

Maintained by Feed Informer