jadeDom is a jQuery plugin to help render DOM elements with Jade syntax.
<div class="class_name">
<a href="#" title="Some title" class="inner_class">Some text</a>
</div>
$('#wrapper').append( [
'<div class="class_name">',
'<a href="#" title="Some title" class="inner_class">Some text</a>',
'</div>'
].join( '' ) );
$('#wrapper .class_name' ).click( function () { alert('hi'); } );
$('#wrapper').append(
$( '<div />', { 'class': 'class_name', click: function () { ... } } ).append(
$( '<a />', { 'class': 'inner_class', href: '#', title: 'Some title', text: 'Some text' } )
)
);
$('#wrapper').append( $.jade(
'div.class_name', { click: function () { ... } }, [
'a.inner_class(href=#, title="Some title") Some text'
]
) );
Arguments passed to jadeDom can be one of three types:
DOM Element: DOM Elements can come in as a string, DOM element, DOM Fragment, or jQuery DOM Object. The first argument MUST be one of these formats.
jQuery Options: This is an object that will specify which jQuery functions to call.
Child Elements: This is an array of child elements to be attached to the most recent DOM Element at this depth.
The example above demonstrates all 3 of these. '.class_name'
tells jadeDom to render a div element with a class of class_name. { click: function () { ... } }
attaches a click event to that element. The following array attaches a child element to that node.
The Options object can also accept a cache
property. This property should be a string that will be used to look up a DOM element later. This will save you the trouble of having to perform a costly jQuery lookup later.
var $dom = $.jade( 'div.something', { cache: 'something' } ); // the div was cached as 'something'
var $something = $dom.jade( 'something' ); // something was pulled from cache by calling z() on the jadeDom object
As you can see above, calling $dom.jade(str)
on a jadeDom object will allow you to access any cached elements. You can also access the full lookup table by calling $dom.jade()
without any arguments.
The tag is the first string specified in your Jade string.
$.jade( 'div' );
<div></div>
If you choose to omit the tag name, it will default to div.
$.jade( '#foo.bar' );
<div id="foo" class="bar"></div>
ID's and classes are set using CSS syntax - that is a #
represents an ID and a .
represents a class.
$.jade( 'div#foo.bar.baz' );
<div id="foo" class="bar baz"></div>
Attributes in Jade are wrapped in ()
. You can have multiple attributes by separating them with a comma. Both attribute names and values can optionally be wrapped in single or double quotes.
$.jade( 'a(href=#, title="Foo")' );
<a href="#" title="Foo">Bar</a>
Adding text to a tag can be accomplished by simply adding a space after the tag details and some text.
$.jade( 'h1#foo.bar Baz' );
<h1 id="foo" class="bar">Baz</h1>
If you want to create a text node without having it be part of another tag, that can be accomplished by starting your string with a |
.
p
| foo bar baz
| rawr rawr
| super cool
| go jade go
$.jade(
'p', [
'| foo bar baz ',
'| rawr rawr ',
'| super cool ',
'| go jade go '
]
);
<p>foo bar baz rawr rawr super cool go jade go</p>
You can also use variable replacement as done in Jade. In order to use variables, the first argument passed to jadeDom must be an object containing the lookup table.
Once you have passed in this lookup table, there are two ways to call a variable within a block of text: !{variable}
and #{variable}
.
These will both do the same thing, but #{variable}
will escape any HTML it finds while !{variable}
will be rendered as HTML.
ul
li Escaped: #{text}
li Unescaped: !{text}
$.jade( { text: '<b>Foobar</b>' },
'ul', [
'li Escaped: #{text}',
'li Unescaped: !{text}'
]
);
<ul>
<li>Escaped: <b>Foobar<b></li>
<li>Unescaped: <b>Foobar</b></li>
</ul>
You can also utilize global variables. Local variables (as in the example above) will override anything present in jadeDom's global variables.
Globals are managed with the following 3 functions:
$.jade.set_globals( object ): This is used to set the globals object, overwriting anything currently present.
$.jade.add_globals( object ): This will extend the existing globals object with any values passed in.
$.jade.clear_globals(): This will empty the globals object.
$.jade.set_globals( { foo: 'bar', bar: 'baz' } );
$.jade( 'p foo is !{foo}, while bar is !{bar}.' );
$.jade( { foo: 'not bar' }, 'p foo is !{foo}, while bar is !{bar}.' );
$.jade.update_globals( { foo: 'not bar' } );
$.jade( 'p foo is !{foo}, while bar is !{bar}.' );
$.jade.clear_globals();
$.jade( 'p foo is !{foo}, while bar is !{bar}.' );
<p>foo is bar, while bar is baz</p>
<p>foo is not bar, while bar is baz</p>
<p>foo is not bar, while bar is baz</p>
<p>foo is !{foo}, while bar is !{bar}</p>
The first line demonstrates basic globals replacement.
In the second line, the global variable 'foo' is still set, but is overridden by the local variable 'foo.'
In the third line, the globals object is updated so that foo is 'not bar.'
In the last line, no replacement occurs since the globals object has been cleared and no locals were passed in.