• This repository has been archived on 06/Sep/2022
  • Stars
    star
    307
  • Rank 136,109 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 11 years ago
  • Updated almost 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The Jade template engine for Meteor/Blaze

Jade for Meteor

Meteor Icon

This Meteor package provides some support for the Jade template engine as a Spacebars alternative.

Spacebars and Jade packages can coexist, Spacebars will continue to compile files ending with .html and Jade will take care of those ending with .jade.

Table of Contents

Installation

Meteor-jade is installable from atmosphere, the meteor package system:

$ meteor add mquandalle:jade

Examples

Meteor comes with some examples such as leaderboard or todos. You'll find jade versions of those examples templates and even more in the examples directory.

Usage

Meteor-jade works somewhat like Jade, so if you never use Jade before you should take a look at the documentation.

There are some specifics rules relative to the Meteor way of handling templates. These rules are mostly the same as the Spacebars ones.

Templates

Every HTML tag must be in a template. You can define a template with the following syntax:

template(name="myTemplate")
  p This paragraph is inside my template

There are two particular templates that are automatically rendered inside the DOM: head and body. If you want to include a template inside another, precede its name by the + symbol:

head
  title Leaderboard

body
  +leaderboard
  //- This is equivalent to {{> leaderboard}}

Inside a text node you can use both {{spacebars}} and #{jade} expressions but the last one is recommended:

template(name="leaderboard")
  p Welcome #{player.name}

If you indent after a div or similar element, you can use | symbol in order jade not to confuse with tags:

template(name='leaderboard')
  #content
   | #{greeting}

You can also use = as a shortcut:

template(name='leaderboard')
  #content
   = greeting

If you want to insert raw HTML you can use the !{jade} syntax which is equivalent to the triple-braced {{{spacebars}}} expression.

HTML Tag attributes

In Jade you define HTML Tag attributes inside parenthesis:

input(name="myName" placeholder="name" autofocus)

If you want to conditionally include a HTML Tag attribute you can use the following syntax:

input(required = isRequired)

Where isRequired is a (potentially reactive) boolean defined in a template helper. If you want to add a list of dynamic attributes use:

input($dyn = attrs)

Spacebars equivalent:

<input {{attrs}}>

Components

As you may already know, Meteor templates are "components" as well. To use a template as a component, you simply have to provide a content block and optionally a elseContent block after the inclusion:

body
  +ifEven(value=2)
    | Hello world
  else
    | Bye world

  //-
    This is the equivalent of:
    {{#ifEven value=2}}
      Hello world
    {{else}}
      Bye world
    {{/ifEven}}
    ifEven is a component defined by the user
    See the complete example in ./examples/components.jade

Like with Spacebars, a component can receive both ordered and keywords arguments. Keywords arguments must be written after the ordered ones:

+myComponent(arg1 arg2 arg3 key1=val1 key2=val2)

Brackets are optional:

+myComponent arg1 arg2 arg3 key1=val1 key2=val2

For the four built-in components (if, unless, each and with) the + is also optional:

ul
  each players
    if isSelected
      li.selected= name
    else
      li= name

Additional features

We have some additional features over Spacebars.

else if

We provide syntaxic sugar so you can write:

if user.isAdmin
  h1 Hello admin
else if user.isConnected
  h1 Hello user
else
  h1 Hello visitor

Instead of:

if user.isAdmin
  h1 Hello admin
else
  if user.isConnected
    h1 Hello user
  else
    h1 Hello visitor

Under the hood, those two codes are compiled to the same abstract tree, so there are no runtime performance hit.

Unwrapped templates

Putting each template in its own separate file and naming the file after the template it contains is becoming a followed pattern among Meteor developers. See for instance this article from Josh Owens.

But as it stands today, this pattern doesn't respect the “don't repeat yourself” (DRY) philosophy. Indeed you have to wrap your template in a <template name="myTemplate> tag and saving it in a myTemplate.html file, effectively writing the name of the template twice. If those two names doesn't match Meteor will consider the name of the <template> tag and will ignore the file name. So if you follow this pattern you have to take care of keeping the file name and the template tag name in sync (manually).

We solve this problem using a new the .tpl.jade file extension. With it you can only define one template per file and you don't need to wrap your template in a tag. The template will be named after the file name. We handle special head.tpl.jade and body.tpl.jade templates as expected.

Anonymous helper

This feature is not yet implemented. However, once implemented it could:

if player.score > 10
  p Well done!

It'll be useful for conditions (if, else if and unless) and inside attributes.

See related issue

Unsupported Features

Currently the following Jade features are not supported by meteor-jade.

  • Code
  • Case
  • Filter

Contributing

Contributions are welcome, whether it is for a bug report, a fix or a new functionnality proposition.

Implementation

This package use the Jade lexer to define the grammar, we just add a few customs rules specifics to the Meteor components model. Then we use the Jade parser which returns a syntax tree that we transform to make it compatible with the Meteor format. We finally rely on the Spacebars compiler to generate the JavaScript code sent to the client.

Everything is executed at bundle time.

License

This code is published under the MIT license.

Tests

Use the following command to run the tests:

$ meteor test-packages packages/*

Tips

If you want to buy me a beer, I proudly accept bitcoin tips: 1Jade7Fscsx2bF13iFVVFvcSUhe7eLJgSy

Known bugs

Using Jade in a package

When using Jade in a package you need to lock the version to the latest version manually. See issue #83.

api.use([
  "templating",
  "mquandalle:[email protected]"
], "client");

More Repositories

1

meteor-bower

[DEPRECATED] Use bower packages in your Meteor app
JavaScript
158
star
2

meteor-stylus

Expressive, dynamic, robust CSS
JavaScript
58
star
3

mesaidesvelo

Découvrez les aides à l’achat d’un vélo
Svelte
57
star
4

meteor-harmony

[DEPRECATED] Javascript Harmony for Meteor
JavaScript
52
star
5

apprendre-meteor

Le cours en français pour apprendre Meteor
24
star
6

chronos

Trusted timestamp on top of ethereum
JavaScript
11
star
7

ethereum-rust

Ethereum client implemented in Rust
Rust
8
star
8

meteor-promisify-core

Replace callbacks by promises in Meteor core APIs
JavaScript
7
star
9

ethereum-cosign

Multisigs for Ethereum
CoffeeScript
7
star
10

meteor-minifiers

Remove the code that doesn't need to be send to a Meteor client.
JavaScript
6
star
11

blaze-autofocus

Support autofocus attribute in Blaze templates
JavaScript
5
star
12

tracker-mobx-bridge

JavaScript
4
star
13

meteor-collection-mutations

A solution for the allow/deny vs methods dilemma
JavaScript
4
star
14

meteor-ismobile

Add a Meteor.isMobile boolean variable
JavaScript
4
star
15

publicodes-compiled

publicodes-compiled
Svelte
3
star
16

meteor-jeet

A grid sytem for SASS or Stylus
JavaScript
3
star
17

meteor-slugify

Remove all special characters from a string.
JavaScript
2
star
18

meteor-macros

Macros for Meteor
JavaScript
2
star
19

optirem

JavaScript
2
star
20

smartBudget

CSS
1
star
21

new-sort-lib-for-libreboard

Try to replace jQuery UI by something better to handle card, list, labels sorting in LibreBoard
JavaScript
1
star
22

count-publicodes-lines

JavaScript
1
star
23

meteor-perfect-scrollbar

JavaScript
1
star
24

meteor-jquery-textcomplete

A thin wrapper around jquery-textcomplete for Meteor
JavaScript
1
star
25

meteor-jquery-ui-drag-drop-sort

A meteor wrapper for jquery-ui a custom build
JavaScript
1
star