Posts Tagged ‘learning code’

Introduction to JavaScript templating using mustache.js

Published by | Thursday, December 27th, 2012

Recently I built a small website for an event in my area. This type of project required me to manage small amounts of data—information about speakers, bios, titles, and a description of the talks. I wanted to have a speakers page, but I also wanted a rotating promo built as a component I could use on the homepage, and on other pages to highlight the event’s speakers. That meant two different views for the same data.

This was the kind of problem I used to throw a quick SQL database at, but it really wasn’t worth the pain for this project as the amount of data was so minimal.  However, I didn’t want to resort to HTML because I knew the information would change often and be a pain to update. To solve the problem, I used a library called mustache.js. It’s pretty easy to use, and solves the problem with just a few lines of code.

Why mustache.js?

Mustache is a library that allows you to read in JSON formatted data and display it using templates you design with JavaScript. There are lots of similar libraries, such as underscore.js, handlebars.js, and dust.js. So why did I choose mustache?

I picked mustache because I wanted something simple. Some of the other libraries provide more functionality, but I didn’t need complexity. I needed to spend time building the site, not learning something new. And that’s the great thing about mustache.js—if you know JSON and a bit of JavaScript, it’s a cinch to implement. Although, you do need to know how to use the JSON format, so if you’re not familiar with JSON, make sure you take a look at this article on getting started with JSON for beginners.

Installing or using CDNs

As usual, you have two options for working with libraries. You can install them manually or use CDNs (content delivery networks). Mustache has implementations available for different languages. If you want to download a copy, go to this GitHub page to download mustache.js. Because we’re going to be loading JSON files from an external source, you’ll also need to install jQuery, to download a copy of jQuery you can go to their website.

If you’re just experimenting with the libraries, another option is to load the files from a CDN, a site that specializes in hosting popular libraries like cdnjs.com. In order to keep things simple for this article, I’m going to load things from that CDN.

Our data

Here’s what the structure of my data looked like (abbreviated). It’s just a simple object with an array containing the usual information people would need to find out about the event speakers.

Of course, my JSON file has a lot more information. Here’s the original JSON document with all of the information for this project.

Creating a page

Our page is going to be super simple. I’ll create a basic HTML document, and then create a <ul> tag with an id of talktitles. The list will be empty and will get filled up with data from our JSON file. I’ll also link, in order, to the jQuery and mustache.js libraries from the CDN.

Here’s what that code looks like:

Next, we’ll need to create the template. That’s pretty easy. You create a mustache.js template inside <script> tags. Normally, your page would interpret this as JavaScript and throw an error, but if we give the script a type of text/template, the browser’s JavaScript parser will ignore it. Our template will also need an **ID** because it’s how we’ll refer to it when we use JavaScript to target it.

Crafting our template

Here’s the code for our template. It should go after our call to the jQuery library.

This should be pretty to read. We want to print the titles for each of our speakers. Our template is simply HTML with double curly braces where we want the library to generate code for us. So, to output the title, we simply use {{title}} to output that field from our JSON file. In order to loop through each of the speakers, we use the {{#speakers}} because it’s the name of the array containing all of the speakers.

The reason a lot of these libraries have names related to facial hair is because of the all of the curly braces they use. This technique of putting data from a data file into a template is known as data binding.

Calling the data

To have our data load from the template, we use jQuery’s getJSON method. I can read any JSON file. We can pass the data into a function. We’re using an anonymous function here (a function with no name).

Inside this we can then create a variable called template and then use the jQuery html() method to read the contents of our template into a variable. Then, we use that template and the data with a call to the Mustache.to_html method from mustache.js that will return the HTML with the JSON data merged with our template.

Finally, we use jQuery to place that HTML inside our unordered list with the id of talktitles. Here’s that code:

Putting it all together

Here’s a full listing of the code:

You might have a problem when trying to read the JSON document if you don’t put this code on a server. For security reasons certain browsers (Google Chrome) don’t allow you to load external files on local folders. You can try using Safari, or just load the file on a live server.

The BarCamp Deland website - completed.

My Barcamp Deland project

For the final project, I used a more complicated template to drive the display of different elements on my page. The Featured Speakers rotating promo on the homepage as well as the data in the Speakers page. I also used this on a mobile app for the event, so changing a single text file updated the information in two or three places. You can see an example of the finished Barcamp Deland website for a more complex example of the code.

Getting started as a programmer and what language to learn first

Published by | Wednesday, January 18th, 2012

It scares me to say this, but I’ve just celebrated a big anniversary: I’ve been a software developer for 25 years. My first programming language was something called PAL. It stood for Paradox Application Language, and was a part of Paradox, a popular DOS-based database application that was in many ways the Microsoft Access of its time. Over the ensuing years, I’ve learned and forgotten many languages as I moved into different areas of the software development world. But I found that each language was progressively easier to learn. Once I understood the fundamentals, I was able to apply lessons learned so far to the new challenges facing me.

No one was around in the beginning to tell me that programming was difficult. I had some work I needed to do, and with the help of sample code and tutorials, I figured out how to do it. It was only later that I learned how intimidating programming sounds to some folks. But it doesn’t have to be painful at all, and I believe that by finding and using the right learning resources, and moving forward in small incremental steps, anyone can learn to code.

We’ve been hearing from our members for the last few years that programming has become more important to you, and so in 2011 we responded by publishing a series of courses on some of the world’s most popular programming languages. In this post, I’m going to describe what you need to know to get started as a programmer and how to select a first programming language to learn.

If you’ve never done a lick of programming, you might want to start with lynda.com staff author Simon Allardice’s course Foundations of Programming: Fundamentals. As in many industries, software developers have their own lingo. Words like statement, function, method, and property have very specific meanings, and some of these meanings can differ from one language to the next. Simon explains the language of languages, and compares many of the most popular programming languages with each other. Examples are provided in JavaScript, Java, C#, and many others.

The next step is to choose your first language. Which language you choose will depend largely on what you want to accomplish. You might use one language for building web pages, another for creating applications designed for cell phones and tablets, and so on.

The most popular programming language course in the Online Training Library® is, unsurprisingly, the most recent edition of our ever-popular JavaScript Essential Training, also authored by Simon Allardice. JavaScript is so popular because it’s the programming language of the modern web. It’s the language that “glues” other web technologies together, such as HTML and CSS. It’s supported in all modern browsers, and is at the core of many popular web frameworks such as jQuery. Looking beyond the browser, JavaScript is also used in many other software development environments, including Titanium, a set of tools for building cross-platform mobile applications, Node.js, a recent entry in the world of server-side web application development, and Unity, a package that enables building games for many platforms.

If you want to build native apps for mobile devices, you might choose Objective-C to build apps for iPhone and iPad. Objective-C Essential Training will help you get started, and then you can move on to our other courses on building apps for the iOS operating system. Java Essential Training with David Gassner (yes, that’s me) teaches the language that’s used to build native apps for Android and BlackBerry devices, and C# Essential Training with Joe Marini will help you get started with building Silverlight and XNA apps for Windows Phone 7. And once you learn either Java or C#, you’ll be able to build not just mobile apps, but also applications for the web (client- and server-side) and much more.

In addition to the courses we added in 2011, the Online Training Library® offers lessons on other languages that you can use for a variety of tasks. These include tutorials on Perl and Python with Bill Weinman, PHP and Ruby with Kevin Skoglund, and one of my own personal favorites, ColdFusion Markup Language with David Gassner (me again).

So regardless of which language you want to learn, you should be able to find some valuable tools in the Online Training Library® to help you get started. I really believe that if you have something you want to accomplish that requires a bit of programming, you can learn it. That’s lynda.com’s new motto, and it absolutely applies to the world of software development.

 

Interested in more?
• All developer courses on lynda.com
• All courses from David Gassner on lynda.com

Suggested courses to watch next:
Foundations of Programming: Fundamentals
JavaScript Essential Training (2011)

Objective-C Essential Training
C# Essential Training

Java Essential Training