Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Web Development with Jade

You're reading from   Web Development with Jade Knowing Jade makes life simpler and more productive for web developers, and this book will teach you the language concisely and thoroughly using lots of practical examples and best practices for a solid grounding.

Arrow left icon
Product type Paperback
Published in Mar 2014
Publisher
ISBN-13 9781783286355
Length 80 pages
Edition Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Sean Lang Sean Lang
Author Profile Icon Sean Lang
Sean Lang
Arrow right icon
View More author details
Toc

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...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime