Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Web Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « HTML5 Support for the Visual Studio 2010 Editor available for downloadnode.js making it a bit more readable »
    comments

    Introduction

    In my previous posts I installed node.js and I made the code be a bit more readable. Now I want to make the view a bit more HTML like so that it reads a bit better and to separate concerns. I selected mustache.js out of the many that are out there.

    Internal template

    first thing was to download mustache.js. I used a package manager for that. Namely npm. To install npm you must however first install curl via

    sudo apt-get install curl

    and then you can do

    sudo curl http://npmjs.org/install.sh | sudo sh

    and then I got to my Documents folder and do this.

    npm install mustache

    and it will download the files for me and put them in a folder called node_modules\mustache.

    Now I can try it out.

    1. var mustache = require('./node_modules/mustache/mustache');
    2.  
    3. function helloworld(response)
    4. {
    5.   console.log('request recieved at ' + (new Date()).getTime());
    6.   response.writeHead(200, {'Content-Type': 'text/html'});
    7.   var template = '<h1>Test</h1><p>{{helloworld}}</p>';
    8.   var model = {helloworld:'Hello World'};
    9.   response.end(mustache.to_html(template,model));
    10. }
    11.  
    12. exports.helloworld = helloworld;

    as you can see I made an object of mustache, I made a template and I made a model. And then I gave both of them to the to_html function so that it could create the html.

    Of course this is no good to us I want that template to be outside my controller.

    Like so.

    1. <html>
    2.   <head>
    3.     <title>My first template</title>
    4.   </head>
    5.   <body>
    6.     <h1>Test</h1>
    7.     <p>{{helloworld}}</p>
    8.   </body>
    9. </html>

    and this in a file called helloworld.html.

    And now we need to adapt our code to read from this file with readfile and the fs namespace.

    1. var mustache = require('./node_modules/mustache/mustache');
    2. var fs = require('fs');
    3.  
    4. function helloworld(response)
    5. {
    6.   console.log('request recieved at ' + (new Date()).getTime());
    7.   fs.readFile("./helloworld.html",function(err,template) {
    8.             response.writeHead(200, {'Content-Type': 'text/html'})
    9.             response.write(mustache.to_html(template.toString(), {helloworld:"Hello World"}))
    10.             response.end()
    11.      });
    12. }
    13.  
    14. exports.helloworld = helloworld;

    As you can see I imported fs and used readfile t read from a file and pass it to mustache, making sure I cast it to string.

    This gives the desired result.

    So in the future we should probably make a function that accepts a model and a template and gets rid of the code I just showed but this will do for now.

    Conclusion

    There are plenty of templating engines out there for you to use. I used mustache.js and got it to work after a while. Documentation is of course hard to find. I will be exploring node.js a bit more in the future it looks however that it need some recommendations and best practices. I'm having quite a bit of fun learning.

    About the Author

    User bio imageChris is awesome.
    Social SitingsTwitterHomePageLTD RSS Feed
    InstapaperVote on HN

    5 comments

    Comment from: Jerry Sievert [Visitor] · http://legitimatesounding.com/blog/
    Jerry Sievert alternately, you can use Handlebars.js:

    var template = handlebars.Handlebars.compile(html);
    response.write(template({helloworld:"Hello World"}));

    mostly mustache compatible, but fairly friendly to work with.
    06/13/11 @ 10:39
    Comment from: Christiaan Baes (chrissie1) [Member]
    Christiaan Baes (chrissie1) I will try it, didn't come across it on my travels.
    06/13/11 @ 10:41
    Comment from: Jerry Sievert [Visitor] · http://legitimatesounding.com/blog/
    Jerry Sievert http://handlebars.strobeapp.com/ - Handlebars.
    06/13/11 @ 15:25
    Comment from: julien [Visitor]
    julien One question, why are you using a VM to run nodejs?
    06/17/11 @ 14:09
    Comment from: Christiaan Baes (chrissie1) [Member]
    Christiaan Baes (chrissie1) Because I like to keep things clean. If you have one machine that does it all than some things will conflict and starting over will be more difficult.
    06/18/11 @ 00:52

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)