Tag attributes look similar to html, however their values are just regular JavaScript.
a(href='google.com') Google
a(class='button', href='google.com') Google
<a href="google.com">Google</a><a href="google.com" class="button">Google</a>
All the normal JavaScript expressions work fine too:
- var authenticated = true
body(class=authenticated ? 'authed' : 'anon')
<body class="authed"></body>
If you have many attributes, you can also spread them across many lines:
input(
type='checkbox'
name='agreement'
checked
)
<input type="checkbox" name="agreement" checked="checked"/>
By default, all attributes are escaped (replacing special characters with escape sequences) to prevent attacks such as cross site scripting. If you need to use special characters you can use !=
instead of =
.
div(escaped="<code>")
div(unescaped!="<code>")
<div escaped="<code>"></div>
<div unescaped="<code>"></div>
Boolean attributes are mirrored by Jade, and accept bools, aka true
or false
. When no value is specified true is assumed.
input(type='checkbox', checked)
input(type='checkbox', checked=true)
input(type='checkbox', checked=false)
input(type='checkbox', checked=true.toString())
<input type="checkbox" checked="checked"/>
<input type="checkbox" checked="checked"/>
<input type="checkbox"/>
<input type="checkbox" checked="true"/>
If the doctype is html
jade knows not to mirror the attribute and uses the terse style (understood by all browsers).
doctype html
input(type='checkbox', checked)
input(type='checkbox', checked=true)
input(type='checkbox', checked=false)
input(type='checkbox', checked=true && 'checked')
<!DOCTYPE html>
<input type="checkbox" checked>
<input type="checkbox" checked>
<input type="checkbox">
<input type="checkbox" checked="checked">
The style
attribute can be a string (like any normal attribute) but it can also be an object, which is handy when parts of the style are generated by JavaScript.
a(style={color: 'red', background: 'green'})
<a style="color:red;background:green"></a>
The class
attribute can be a string (like any normal attribute) but it can also be an array of class names, which is handy when generated from JavaScript.
- var classes = ['foo', 'bar', 'baz']
a(class=classes)
//- the class attribute may also be repeated to merge arrays
a.bing(class=classes class=['bing'])
<a class="foo bar baz"></a><a class="bing foo bar baz bing"></a>
It can also be an object mapping class names to true or false values, which is useful for applying conditional classes
- var currentUrl = '/about'
a(class={active: currentUrl === '/'} href='/') Home
a(class={active: currentUrl === '/about'} href='/about'
<a href="/">Home</a><a href="/about" class="active">About</a>
Classes may be defined using a .classname
syntax:
a.button
<a class="button"></a>
Since div's are such a common choice of tag, it is the default if you omit the tag name:
.content
<div class="content"></div>
IDs may be defined using a #idname
syntax:
a#main-link
<a id="main-link"></a>
Since div's are such a common choice of tag, it is the default if you omit the tag name:
#content
<div id="content"></div>
Pronounced "and attributes", the &attributes
syntax can be used to explode an object into attributes of an element.
div#foo(data-bar="foo")&attributes({'data-foo': 'bar'})
<div id="foo" data-bar="foo" data-foo="bar"></div>
The object does not have to be an object literal. It can also just be a variable that has an object as its value (see also Mixin Attributes)
- var attributes = {'data-foo': 'bar'};
div#foo(data-bar="foo")&attributes(attributes)
<div id="foo" data-bar="foo" data-foo="bar"></div>