Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Web Development with Jade
Web Development with Jade

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.

eBook
$10.99 $16.99
Paperback
$26.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Web Development with Jade

Chapter 2. Basic Syntax

Now that you know what Jade actually is, let's enter that part of the book where you start to learn how to write Jade.

Significance of whitespace


Rather than using opening/closing tags to delimit the start/end of blocks, Jade uses indentation. This can be a little strange if you're used to languages where whitespace doesn't matter (such as JS, CSS, and of course HTML). However, it does have the benefit of forcing you to have good indentation, preventing horrible formatting as illustrated in the following code (which is a perfectly valid HTML):

<!DOCTYPE html>
<html><head><title>An Example</title></head>
<body><h1>Horrible Formatting</h1>
<p>Never write HTML like this, it is <i>really</i> hard to read</p>
</body></html>

Also, whitespace significance prevents stupid errors like the following:

<i>notice the order of the <b>closing tags</i></b>

Now onto how it actually works:

Each level of indentation (the rectangles outlined with dashed lines) creates a new block (the pastel-colored sections) out of...

Tags


Since Jade is indentation-based, there are no end tags, and there are no angle brackets (<, >) to surround tags, because those are lame and ugly. The name of the tag is all that you need, and it is the first text on the line. Consider the following example:

Jade

HTML

p
<p></p>

If we add another tag within the <p> block (as explained earlier), we can create a nested tag as follows:

Jade

HTML

p
  span
<p>
  <span></span>
</p>

Alternatively, without putting it in the <p> block, we can just create it in a way that it acts as a sibling, as follows:

Jade

HTML

p
span
<p></p>
<span></span>

Text and blocks of text

Tags are pretty boring if they don't have any content, so Jade gives us three ways of putting text in tags.

Text on the same line

You can put the text directly after the tag name (separated by a space) as follows:

Jade

HTML

p Hello Word!
<p>Hello Word!</p>

Text blocks

For large bodies of text, putting...

Inline HTML


It's also perfectly fine to put inline HTML in any of those text blocks, as shown in the following example:

Jade

HTML

p.

  This is a <i>
  demonstration</i>
  of Jade's <b>text
  blocks</b>
<p>
  This is a <i>
  demonstration</i>
  of Jade's <b>text
  blocks</b>
</p>

Attributes


Attributes are also pretty important, so here's how to write those:

Jade

HTML

p(id="hello") Hello Word!
<p id="hello">Hello Word!</p>

That's right! They're pretty similar to the way you write attributes in HTML, except they're surrounded by a pair of parentheses. Also, if you have multiple attributes, they're delimited by commas, rather than just spaces. An example of this is as follows:

Jade

HTML

p(id="hello", class="world")
<p id="hello" class="world">
</p>

Tip

Jade 0.35.0 (released on August 21, 2013) added support for space-separated attributes. Soon, this will be supported by syntax highlighters, syntax checkers, and related tools like html2jade; but until then, you may wish to stick with the comma-delimited syntax. For this reason, the rest of this book will use comma-delimited attributes.

Passing objects as attributes

In Jade, you can easily pass strings as attributes, but if you pass objects, they will be turned into the most useful representation for...

Shorthands


IDs and classes

IDs and classes are both pretty common attributes, so Jade gives us a shorthand method for writing them. This is similar to the way CSS selectors are written. An example of this is as follows:

Jade

HTML

p#hello Hello Word!
<p id="hello">Hello Word!</p>
p#hello.world
<p id="hello" class="world"></p>

Pretty familiar, eh? IDs are just prefixed with a # (pound symbol) and classes are prefixed with . (a period). These may be put in any order after the tag name with any number of classes.

Automatic div

Because the div tags are used so frequently, Jade offers a shorthand way for writing them; by omitting the tag, Jade assumes you want to use a div tag; therefore, the following code:

Jade

HTML

div#hello Hello Word!
<div id="hello">
Hello Word!
</div>

It can also be rewritten as:

Jade

HTML

#hello Hello Word!
<div id="hello">
Hello Word!
</div>

However, this is possible only as long as there is an ID and/or class where the tag name...

Comments


Single line

Normal HTML comments are pretty verbose, so Jade offers us a much shorter way to write them that looks similar to JavaScript comments.

Jade

HTML

//a single line comment
<!-- a single line comment-->

Also, if you don't want your comments to show up in the compiled HTML, you can use silent comments by adding a - (hyphen) after //.

//- a silent single line comment

Block comments

But of course, we need to be able to comment out multiple lines too; for that, we use block comments. If you indent a block after a comment, that block will be added to the comment too. An example of this is as follows:

Jade

HTML

// a block comment
  h1 Now I'm Commented Out.
  p And me too.
<!-- a block comment
h1 Now I'm Commented Out.
p And me too.-->

As you can see, the first line of the comment becomes a text block, and the indented block is not parsed. However, the first line is entirely optional and is generally just used to note what was commented out. We can omit it if we want to...

Block expansion


When each tag only has one tag nested under it, it can be a little annoying to have a new line for each one of them:

ul
  li.first
    a(href='#') foo
  li
    a(href='#') bar
  li.last
    a(href='#') baz

So, Jade has a feature called block expansion that lets us put those tags on the same line, meaning we can rewrite the preceding example as the following:

ul
  li.first: a(href='#') foo
  li: a(href='#') bar
  li.last: a(href='#') baz

The : (colon) after the tag name and attributes indicates that there is another tag following that one. We can even make really long chains of tags:

Jade

HTML

ul: li.first: b: a(href='#') foo
<ul>
  <li class="first">
    <b>
      <a href="#">foo</a>
    </b>
  </li>
</ul>

But that is really hard to read, so please, never do that unless you have a very good reason.

Doctypes


Doctypes can be really long, so naturally, Jade gives us a much shorter way to write them, as shown:

doctype
<!DOCTYPE html>
doctype default
<!DOCTYPE html>
doctype html
<!DOCTYPE html>
doctype xml
<?xml version="1.0" encoding="utf-8" ?>
doctype transitional
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
doctype strict
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
doctype frameset
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
doctype 1.1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
doctype basic
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">
doctype mobile
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN...

Summary


In this chapter, we dived into the language itself, covering the very basics of the syntax. This included how indentation-based syntaxes work and how to write tags, text, attributes, comments, and some nifty shorthands for classes, IDs, and doctypes.

Left arrow icon Right arrow icon

What you will learn

  • Write cleaner, indentationbased markup
  • Use logical statements to format data for display on the Web
  • Avoid repetition by eliminating redundant operations
  • Divide your templates into logical sections with blocks
  • Avoid common organizational pitfalls when designing Jadebased projects
  • Apply shorthand for brevity
  • Utilize Jade for clientside templates
  • Employ techniques like filters to quickly mockup web pages in higher level languages like stylus or coffeescript

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Mar 26, 2014
Length: 80 pages
Edition :
Language : English
ISBN-13 : 9781783286355
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Mar 26, 2014
Length: 80 pages
Edition :
Language : English
ISBN-13 : 9781783286355
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 120.97
Getting Started with Grunt: The JavaScript Task Runner
$38.99
Web Development with Jade
$26.99
Node Cookbook: Second Edition
$54.99
Total $ 120.97 Stars icon

Table of Contents

8 Chapters
What is Jade? Chevron down icon Chevron up icon
Basic Syntax Chevron down icon Chevron up icon
Feeding Data into Templates Chevron down icon Chevron up icon
Logic in Templates Chevron down icon Chevron up icon
Filters Chevron down icon Chevron up icon
Mixins Chevron down icon Chevron up icon
Template Inheritance Chevron down icon Chevron up icon
Organizing Jade Projects Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.3
(4 Ratings)
5 star 50%
4 star 25%
3 star 25%
2 star 0%
1 star 0%
Dustin Marx Apr 30, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Web Development with Jade is a relatively short book of approximately 60 pages of substantive content. The book consists of eight short chapters that briefly introduce the benefits of Jade, explain why Jade may be considered superior to some of its alternatives, introduce Jade syntax, and describe packaging of Jade source. The short chapters make for quick reads and each chapter is easily understood because of the straightforward explanations. Jade is not the most complex language (even template language) that one is going to run into and Web Development with Jade reflects this simplicity nicely and does not add unnecessary complexity in its explanations.The examples in Web Development with Jade are clear and easy to understand. Lots of white space makes these examples stand out nicely and text is relatively concise while still managing to communicate sufficient detail for learning Jade.I have read the PDF version of the book and appreciate the color coded syntax of its code listings. The overall presentation of Web Development with Jade makes it easy to read and learn from.Basic knowledge of HTML, JavaScript, and CSS would benefit of a reader of Web Development with Jade, but this does not need to be deep knowledge or lengthy experience with these technologies. Minimal awareness of what HTML, JavaScript, and CSS are and the ability to recognize basic constructs of each and how they relate to each other should be sufficient for most readers to be able to learn about Java from reading Web Development with Jade.Web Development with Jade has a few quotations expressing strong opinions of the author. Although not all of these are backed up, there are only a few of them that really stand out and, for the most part, they don't impact the transfer of information about applying Jade in web development.There are online resources regarding Jade, but I'm not aware of any single resource online that presents everything covered in Web Development with Jade or that presents an overview of Jade with this level of detail in such a presentable form. Packt Publishing provided me a copy of this book in electronic (PDF) form and it is that edition that is reviewed here.
Amazon Verified review Amazon
Ionut-Cristian Florescu Apr 29, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Over the last two years I've been building commercial and open-source Node.js projects and I've been using the Jade template engine almost exclusively ever since.That is why I've got so used with it that I now find writing plain old "vanilla" HTML code rather cumbersome and unproductive.That's also why, even since the early days of the project, I've introduced support for compiling Jade templates in ASPAX, arguably the simplest Node.js asset packager you can use (aspax.github.io).Until recently I've been under the impression that there's not much more I can learn about it - until I found Sean's excellent book.While the "official" Jade website is pretty good, this book will also give you, in Chapter 1, a brief overview of the current markup preprocessor engines landscape and a nice explanation of how Jade really works, in both pictures and code. It will also show you why Jade is so cool compared to other alternatives out there, due to its concision and clarity.Chapters 2 through 7 will teach you everything about writing Jade code, starting with the basic syntax, working with data, flow control logic and even touching advanced topics like filters, mixins and template inheritance.Chapter 8, undoubtedly one of the most valuable pieces of information in the book, will teach you how to structure the presentation-related code in your application.So, if you're looking to become a professional JavaScript/Node.js developer or you already are one but you're looking for a well-written Jade reference to consolidate your skills, you should definitely have a look at "Web Development with Jade".
Amazon Verified review Amazon
W Boudville May 01, 2014
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Some readers will be frustrated by the perhaps awkward syntax of HTML. But what can you do? Just put up with all those wretched tags? Well, Jade presents itself as a more compact alternative. It lets you write without the tags, in a style akin almost to free form poetry, as alluded to early in the text. This is input in Jade, which spits out correctly written HTML.You do not have to know the innards of Jade. The book talks briefly about how it works. You can probably skip that if you wish. And you have undoubtedly noticed the brevity of the narrative. A good sign that you can rapidly learn to use it. Less than a day to experienced coders.There is even provision for simple logic in Jade templates. Ok a programmer using to procedural languages like C or Fortran would laugh at the functionality here. But all this sits on top of HTML, which is a declarative language. So the amount of logic Java furnishes should not be deprecated.
Amazon Verified review Amazon
Wouter Blancquaert May 30, 2014
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
Web Development with Jade, a book provided by Packt Publishing is one of those small books for web developers willing to learn more about Jade, but are no longer interested in the basic details. It is short, meaning it goes fast. Don’t expect an introduction to HTML5, CSS, JavaScript or Node.js. This book is for those already having a good background on these topics and are willing to learn Jade fast.The book itself covers more or less the syntactic ground of the Jade templating engine, and that’s about it. One small introductory chapter giving a little hint of existing templating engines, and a final chapter about how a Jade project structure should look like. This doesn’t mean this book isn’t worth buying, but know what to expect.I would advise this book to web developers with at least some experience, but certainly not for designers new to the web development landscape.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.