Recommend this page to a friend! |
Classes of Igor Escobar | Terminal Crossword | connected-users-websocket/node_modules/jade/jade.md | Download |
|
|
DownloadJadeThe jade template engine for node.js Synopsis
Examplestranslate jade the templates dir
create {foo,bar}.html
jade over stdio
jade over s
foo, bar dirs rendering to /tmp
compile client-side templates without debugging instrumentation, making the output javascript very light-weight. This requires runtime.js in your projects.
TagsTags are simply nested via whitespace, closing tags defined for you. These indents are called "blocks".
You may have several tags in one "block":
Self-closing Tags Some tags are flagged as self-closing by default, such
as
Would yield:
AttributesTag attributes look similar to HTML, however the values are regular JavaScript, here are some examples:
As mentioned the attribute values are just JavaScript, this means ternary operations and other JavaScript expressions work just fine:
Multiple lines work too:
Multiple lines without the comma work fine:
Funky whitespace? fine:
Boolean attributesBoolean attributes are mirrored by Jade, and accept bools, aka _true_ or _false_. When no value is specified _true_ is assumed. For example:
For example if the checkbox was for an agreement, perhaps
Class attributesThe _class_ attribute accepts an array of classes, this can be handy when generated from a javascript function etc:
Class literalClasses may be defined using a ".CLASSNAME" syntax:
Or chained:
The previous defaulted to divs, however you may also specify the tag type:
Id literalMuch like the class literal there's an id literal:
Again we may specify the tag as well:
Finally all of these may be used in any combination, the following are all valid tags:
Block expansionJade supports the concept of "block expansion", in which using a trailing ":" after a tag will inject a block:
TextArbitrary text may follow tags:
yields:
Pipe textAnother form of text is "pipe" text. Pipes act as the text margin for large bodies of text.
yields:
Using pipes we can also specify regular Jade tags within the text:
Text only tagsAs an alternative to pipe text you may add a trailing "." to indicate that the block contains nothing but plain-text, no tags:
Some tags are text-only by default, for example _script_, _textarea_, and _style_ tags do not contain nested HTML so Jade implies the trailing ".":
Template script tagsSometimes it's useful to define HTML in script tags using Jade, typically for client-side templates. To do this simply give the _script_ tag an arbitrary _type_ attribute such as _text/x-template_:
InterpolationBoth plain-text and piped-text support interpolation, which comes in two forms, escapes and non-escaped. The following will output the _user.name_ in the paragraph but HTML within it will be escaped to prevent XSS attacks:
The following syntax is identical however it will _not_ escape HTML, and should only be used with strings that you trust:
Inline HTMLSometimes constructing small inline snippets of HTML in Jade can be annoying, luckily we can add plain HTML as well:
CodeTo buffer output with Jade simply use _=_ at the beginning of a line or after a tag. This method escapes any HTML present in the string.
To buffer output unescaped use the _!=_ variant, but again be careful of XSS.
The final way to mess with JavaScript code in Jade is the unbuffered _-_, which can be used for conditionals, defining variables etc:
When compiled blocks are wrapped in anonymous functions, so the following is also valid, without braces:
If you really want you could even use
Taking this further Jade provides some syntax for conditionals, iteration, switch statements etc. Let's look at those next! AssignmentJade's first-class assignment is simple, simply use the _=_ operator and Jade will _var_ it for you. The following are equivalent:
ConditionalsJade's first-class conditional syntax allows for optional parenthesis, and you may now omit the leading _-_ otherwise it's identical, still just regular javascript:
Jade provides the negated version, _unless_ as well, the following are equivalent:
IterationJavaScript's _for_ loops don't look very declarative, so Jade also provides its own _for_ loop construct, aliased as _each_:
As mentioned _each_ is identical:
If necessary the index is available as well:
Remember, it's just JavaScript:
MixinsMixins provide a way to define jade "functions" which "mix in" their contents when called. This is useful for abstracting out large fragments of Jade. The simplest possible mixin which accepts no arguments might look like this:
You use a mixin by placing
For something a little more dynamic, mixins can take arguments, the mixin itself is converted to a javascript function internally:
Yields:
Mixins may optionally take blocks, when a block is passed
its contents becomes the implicit
yields:
Mixins can even take attributes, just like a tag. When
attributes are passed they become the implicit
yields:
If you use
yields:
If you pass arguments, they must directly follow the mixin:
yields:
|