Sunday, December 30, 2012

TypeScript Node.js Development Part 3 - Twitter Bootstrap

This is going to be a quick post since I only want to cover how to get a jade layout working with Twitter Bootstrap. Bootstrap is a front-end framework that includes a nice standard UI, an easy way to make a neat layout that updates depending on the size of the window, and a bunch of neat Javascript features like popups, dropdowns, and alerts. It's a great way to start off a site, especially for people like me who are horrible at graphic design.

To get this started, first I'm going to get rid of the original CSS code, but leave the file there in case I want to add anything else later. Getting the site "bootstrap enabled" is a simple matter of referencing the bootstrap CSS and JS files, and I'm also going to include jQuery since the Bootstrap Javascript code requires it, and also because it's something that is pretty useful on its own. With these changes, the layout.jade file now looks like:
    title Page Title
    link(rel='stylesheet', href='//')
    link(rel='stylesheet', href='/css/style.css')


    #container!= body

The basic way to layout a Bootstrap page is to use row divs to separate vertical parts of the page and column spans with optional offsets to separate horizontal parts of rows. The Bootstrap docs explain this in more detail, but for my simple index page, I'm going to enclose all my content in one row which is 4 columns wide and offsetted 2 columns from the left, and in jade that looks like this:
    [content goes here]

My content is going to be the title and test array from part 2, then some code which tests that jQuery and Bootstrap are both hooked up correctly and working. The jQuery test is a simple accessor that modifies some HTML. The Bootstrap test is a bit more involved - it's a button that when clicked will open up a modal dialog. Translating some jQuery code and sample Bootstrap code to Jade, we get this:
    h1= title
    p Welcome to #{title}

    - each test in testArray
        a(href= "/user/"+test)
          b= test


      $("div#jquery-test").html("jQuery works!")

      | Bootstrap Modal

        button.close(type="button", data-dismiss="modal") x
        h3 Modal Test

      $('#bootstrap-test').click(function() { $('#myModal').modal() });

Load that up, and we see the reassuring "jQuery works!" and a button which, as expected, opens up a modal dialog.

The final step I wanted to include was to add some Google Analytics tracking code, since that is something that I want in all of my sites. This gets added to the layout file, and in Jade it looks like:
      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'ANALYTICS-ID']);
      (function () {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);

And that's all I wanted to cover in this post. As with part 2, I've made a Visual Studio template which includes these changes.

Previously: TypeScript Node.js Development Part 2 - Standard Packages and Azure Deployment, next: TypeScript Node.js Development Part 4 - More Packages.

No comments:

Post a Comment