Start learning with our library of video tutorials taught by experts. Get started

Node.js Essential Training
Illustration by Richard Downs

Generating HTML views in Jade


From:

Node.js Essential Training

with Joseph LeBlanc

Video: Generating HTML views in Jade

Most frameworks and content management systems use the concept of theming or templating HTML documents. Express is no exception. By default, Express offers Jade as a templating system. In this video, we'll create some templates in Jade and use them to display data from our application. Let's go to the exercise files and set up our server. Go to chapter five and video six, then copy the start folder to your desktop. From here, we'll install the modules. Type in cd and then I'm just going to drag that folder into terminal to get that path.
Expand all | Collapse all
  1. 3m 9s
    1. Welcome
      42s
    2. What you should know before watching this course
      57s
    3. Using the exercise files
      47s
    4. Using the challenges
      43s
  2. 18m 50s
    1. Why use Node.js?
      2m 1s
    2. Choosing a Node.js installation process
      4m 56s
    3. Installing Node.js on the Mac
      2m 5s
    4. Installing Node.js on Windows
      1m 43s
    5. Installing Node.js via Node Version Manager on Ubuntu Linux
      4m 45s
    6. Installing Node.js via Node Version Manager on a Mac
      3m 20s
  3. 13m 54s
    1. Exploring language additions to the V8 JavaScript engine
      3m 38s
    2. Understanding require() and modules
      6m 39s
    3. Understanding callback execution in the event loop
      3m 37s
  4. 12m 23s
    1. Initializing Node.js projects
      2m 37s
    2. Finding an NPM
      3m 30s
    3. Maintaining projects using the npm command
      6m 16s
  5. 21m 4s
    1. Creating modules with getters and setters
      6m 33s
    2. Understanding module caching and scopes
      6m 15s
    3. Implementing JavaScript creational patterns
      5m 8s
    4. Challenge: Leveraging module caching
      55s
    5. Solution: Leveraging module caching
      2m 13s
  6. 32m 27s
    1. Starting Express applications
      3m 42s
    2. Handling GET requests returning JSON
      3m 26s
    3. Navigating large datasets using request variables in routes
      5m 4s
    4. Modifying data through PUT requests
      4m 32s
    5. Supplying middleware to modify HTTP response headers
      4m 37s
    6. Generating HTML views in Jade
      8m 9s
    7. Challenge: List all records as JSON
      37s
    8. Solution: List all records as JSON
      2m 20s
  7. 25m 37s
    1. Evaluating unit testing methodologies
      3m 8s
    2. Isolating existing code for testing
      6m 47s
    3. Supplying helpers for test data
      2m 35s
    4. Writing unit tests with Mocha
      6m 5s
    5. Simulating HTTP requests with SuperTest
      7m 2s
  8. 28m 45s
    1. Connecting to MongoDB via Mongoose
      4m 16s
    2. Defining schemas for collections in Mongoose
      3m 47s
    3. Querying MongoDB collections
      5m 32s
    4. Persisting Express sessions through MongoDB and mongo-connect
      7m 6s
    5. Logging in to Express applications using Passport
      8m 4s
  9. 21m 47s
    1. Controlling the built-in REPL
      4m 18s
    2. Constructing a custom REPL
      5m 40s
    3. Receiving command-line arguments with Optimist
      6m 14s
    4. Building command-line tools
      3m 35s
    5. Challenge: Launch a custom REPL from a command
      41s
    6. Solution: Launch a custom REPL from a command
      1m 19s
  10. 15m 46s
    1. Emitting events and attaching listeners
      5m 32s
    2. Streaming chunked data through readable streams
      2m 33s
    3. Controlling readable streams
      1m 52s
    4. Piping readable data into writable streams
      2m 10s
    5. Handling duplex streams over TCP
      3m 39s
  11. 4m 6s
    1. Deploying projects to cloud hosting services
      2m 36s
    2. Finding Node.js resources
      1m 30s

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
Please wait...
Node.js Essential Training
3h 17m Intermediate Oct 16, 2013

Viewers: in countries Watching now:

Node.js is a powerful tool for controlling web servers, building applications, and creating event-driven programming. And it brings JavaScript—a language familiar to all web developers—into an environment independent of web browsers. Learn all about Node.js and start creating JavaScript applications in this course with Joseph LeBlanc. He shows how to install Node.js on Mac, Windows, and Linux and dives deep into its modules and Express framework for app development. Learn how to unit test your code, start sessions on web servers, stream data, and create simple command-line tools.

Topics include:
  • Why use Node.js?
  • Installing Node.js
  • Understanding the event loop
  • Initializing Node.js projects
  • Creating modules with getters and setters
  • Starting Express applications
  • Testing your code
  • Working with sessions and databases
  • Building command-line tools
  • Emitting events and attaching listeners
  • Controlling readable streams
Subjects:
Developer Servers Cloud Computing Programming Languages
Software:
JavaScript Node.js
Author:
Joseph LeBlanc

Generating HTML views in Jade

Most frameworks and content management systems use the concept of theming or templating HTML documents. Express is no exception. By default, Express offers Jade as a templating system. In this video, we'll create some templates in Jade and use them to display data from our application. Let's go to the exercise files and set up our server. Go to chapter five and video six, then copy the start folder to your desktop. From here, we'll install the modules. Type in cd and then I'm just going to drag that folder into terminal to get that path.

Now I'm going to type npm install. Now let's open the start folder in Sublime Text. At the moment, the public and the views folders are entirely empty. We're going to be adding some files here in this video. The first file I'm going to add is Bootstrap. To help with the styles, I'll be using the Bootstrap CSS framework, and it's available from getbootstrap.com. You can use whatever CSS files or frameworks you'd like for your project, but I'm personally using this one. So I don't have to spend a lot of time doing browser resets and layouts.

So I'm just going to click Download Bootstrap, then I'm going to decompress the ZIP file. And inside the folder, there's another folder called the dist and another folder called css. The file I want to use is bootstrap.min.css. And so this file is going to go into our public folder. So open up the public folder and just drag bootstrap.min.css there. And now it appear in our public folder. So now let's handle the views. I'm going to be using Jade.

Jade is designed so you can write HTML in a simplified way with fewer key strokes. The indentation in Jade files determine where tags are open and closed. You don't have to worry about closing tags, because the format of the Jade file enforces closed tags automatically. The Jade parser will also throw an error if the formatting of the file is incorrect. This drastically reduces the possibility of accidentally sending broken HTML to a web browser.

Jade is also designed so markup can be split into reusable blocks. You can extend Jade views to use other Jade views so you don't have to repeat markup over and over again. Finally, Jade lets you define place holders for variables. Jade will automatically handle the HTML escaping of variables so you don't send broken markup to browsers. Let's start writing some Jade now. I'm going to create a new file in the Views folder. And I'm going to name it layout.jade.

The first thing I'm going to enter in is the doctype, and now I'm going to open with an HTML tag. You'll notice that I'm not using any angle brackets and I'm not closing any tags. I'm just typing the names of the tags. And now to nest tags, I'm just pressing Tab. So, now I'm going to open the head tag, and inside of head, we're going to have a title tag, and then a link tag so we can bring in that CSS file. The title tag is going to have a variable place holder. So I'm going to type an equal sign after the title tag, and then I'm going to type title.

We'll be supplying the title variable later. For the link tag, I want to specify that this is a style sheet and I also want to tell the browser where to find the style sheet. I start with an open and close parenthesis and then I type rel=stylesheet. To add another attribute, add a comma. And now I'm going to type href=bootstrap.min.css. Now I'm going to go back up a level and start the body. The first thing I'm going to add is a div.

I want to add a class to this div, so I'm going to press dot, and then type container. This is going to give us a div with a class of container. Finally, I'm going to add what's called a block. A block is going to allow us to define a placeholder where more content can appear. I'm going to name this block, content. In a moment we're going to define another Jade file, and it's going to use this block. So, now let's create another Jade file. This first jade file is the layout and we're just going to use this for every screen that we define.

The next view we're defining is the list view. So, let's create that now. I'm gong to save this as list.jade. So, the first thing I'm going to type in the top is extends layout. Now, what this keyword is going to do is it's going to bring in the layout file, and it's going to use it here in list. The next thing I'm going to do is define that content block. So, over here on line eight in layout.jade, we defined block content as a place holder. Now here in list, I'm going to define what's going to go into that placeholder.

So the first thing that's going to go there is an h1 tag. And this is going to be another variable place holder. Again I'm going to use that title variable. Then at the same level, I'm going to define an unordered list. Inside that unordered list, I'm going to start a loop. What's interesting about Jade is that it allows us to use a limited subset of JavaScript directly in our Jade views. So this is allowing us to do a loop. The index becomes the object property name, while flight becomes the object property value.

Then on the next line I'm indenting and then I'm adding a hyphen. This hyphen is going to let me write some raw JavaScript right here in the Jade file. What I want to do is set that flight variable to the actual information from the flight object. Now, the flight objects that we've defined from earlier come from that module. And to get that information, we need to use the get information method. So, I'm going to set flight to flight.getinformation. Finally, on line eight, I'm going to define a list item.

From here, I can output the flight number, the flight origin, and the destination. Notice that I'm concatenating a string here. So, now, we've added two views in Jade. We need to do one more thing before we can actually use all these views and that the define a route that calls them. Let's go to apps.js. Let's add a new route called List and then let's define a new function that'll handle this list. I'm going to add this function to the routes module. So, I'm going to type routes.list.

Now, let's go to routes and index.js and define the function. I'm going to go to the bottom of the file, and then type exports.list. I'm going to define that as a new function and it's going to accept a request and a response object. To render this list, I'm going to call the render method of the response object. The first argument is the name of the Jade view that we want to load. In this case I want to use the List view. The second argument is an object with all the data that we want to pass to the view.

Both in layout and in list we have the title variable. Let's set this title variable to all flights. Then in the List view, we had the flights variable. We have the flights defined here on line six so, let's add them to this object. So now with this handler function in place, let's start up the server and go to the web browser. Type in node, space app and then press Return. Go to localhost at Port 3000 and go to slash list.

Now all the flights in the system are listed in HTML. Jade is helpful templating language for writing clean and correct HTML quickly. Jade views can include other Jade views and placeholders for variables can be added. You can even write some limited forms of JavaScript directly in Jade. In the next chapter, we'll explore ways of maintaining code quality through unit testing. In the meantime, I have a challenge in the next video to help you get more hands-on experience with Express.

There are currently no FAQs about Node.js Essential Training.

Share a link to this course
Please wait... Please wait...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

join now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Node.js Essential Training.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Welcome to the redesigned course page.

We’ve moved some things around, and now you can



Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked