Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Web Development with Jade
  • Table Of Contents Toc
  • Feedback & Rating feedback
Web Development with Jade

Web Development with Jade

By : Sean Lang
4.3 (4)
close
close
Web Development with Jade

Web Development with Jade

4.3 (4)
By: Sean Lang

Overview of this book

Table of Contents (16 chapters)
close
close

Adding logic with JavaScript


As I've already mentioned, Jade compiles into JS and allows you to use JS directly in your template. So, we can use any of the logical operators that JS provides to build our markup.

If/else

The most basic logical operator is the if statement, as shown in the following code snippet:

Jade

HTML

- name = "Bob"

- if (name == "Bob") {
  h1 Hello Bob
- } else {
  h1 My name is #{name}
- }
<h1>Hello Bob</h1>

And the shorthand form (the ternary operator) also works:

Jade

HTML

- name = 'Bob'
- greeting = (name == 'Bob' ? 'Hello' : 'My name is')
h1 #{greeting} #{name}
<h1>Hello Bob</h1>

Tip

Switches don't work. Use the case statement that is explained in the next section.

For loops

Loops can be used to iterate over lists or repeat elements a certain number of times, an example is given in the following code snippet:

Jade

HTML

- list = ['one', 'two', 'three'];

ul
  - for (var i = 0; i < list.length; i++){
      li=list[i]
  - }
<ul>
  <li...
Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Web Development with Jade
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon