Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jade template engine - Each Iteration Offset

Is there a way to offset the 'each' iteration when using the Jade template engine?

for example, when passing in the object named list:

ul
   each item in list
      li #{item}

Will output

<ul>
   <li> Item 1 </li>
   <li> item 2 </li>
   <li> item 3.....
...
</ul>

But I want the first item to be displayed differently than the rest of the items, like so:

<ul>
   <li> Item 1: First Item in list! </li>
   <li> item 2 </li>
   <li> item 3.....
...
</ul>

So does anyone know a way to offset the 'each' statement in Jade so that I can render the first item separately and then render each following item starting at the 2nd index?

like image 649
Steve Mason Avatar asked Apr 04 '12 02:04

Steve Mason


People also ask

What is Jade template engine?

Jade is a template engine for node. js and the default rendering engine for the Express web framework. It is a new, simplified language that compiles into HTML and is extremely useful for web developers. Jade is designed primarily for server-side templating in node.

Why use Jade template engine?

It's used to produce XML documents like (HTML, RSS etc.), so we can't use it to create the plain text like HTML/CSS other documents. Jade Template Engine is used to make the template more beautiful and more meaningful with the help of description and their layout.

Is Jade template engines can be used with node js?

Jade is a template engine for Node. js. Jade syntax is easy to learn. It uses whitespace and indentation as a part of the syntax.

What is Jade and EJS?

The difference between ejs and jade is that ejs' purpose is to directly add javascript logic and import values to strings of html; Jade is a full templating language with its own syntax. In the end, both compile a template into a javascript function which then glues together the resulting snippets into html.


1 Answers

each item, i in list
  li= item
  if i === 1
    | : First item in list!
like image 152
Jonathan Ong Avatar answered Sep 19 '22 17:09

Jonathan Ong