Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gsf/whiskers.js
Whiskers templating library for JavaScript
https://github.com/gsf/whiskers.js
Last synced: 6 days ago
JSON representation
Whiskers templating library for JavaScript
- Host: GitHub
- URL: https://github.com/gsf/whiskers.js
- Owner: gsf
- License: mit
- Created: 2011-03-21T21:59:56.000Z (almost 14 years ago)
- Default Branch: master
- Last Pushed: 2021-03-31T21:16:12.000Z (almost 4 years ago)
- Last Synced: 2024-04-14T12:59:17.594Z (9 months ago)
- Language: JavaScript
- Homepage:
- Size: 285 KB
- Stars: 108
- Watchers: 5
- Forks: 16
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Whiskers.js
===========**Note** The library is not supported for long time. It's usage is not recommended in any publicly available application due to potential security issues.
About
-----Whiskers is focused on template readability. By limiting template logic,
careful preparation of the context is encouraged, and the processing and
formatting of data is kept separate from the design of the display.At around 100 lines, Whiskers.js may be the smallest mustachioed templating
system. It also compiles and caches for quick execution. Take a look at the
well-documented [code][]![code]: https://github.com/gsf/whiskers.js/blob/master/lib/whiskers.js
Installation
------------For the browser, drop the minified version at `dist/whiskers.min.js` in your
scripts directory (or source it directly from GitHub at
).For node, `npm install whiskers`.
For use in [Express][], see [examples/express][exex].
[express]: http://expressjs.com/
[exex]: https://github.com/gsf/whiskers.js/tree/master/examples/expressTests and Dist
---------------For the browser, visit `test/browser/index.html`.
For node, `npm test`. It produces minified copy of library to dist folder
Example
-------Templates are rendered as follows, where "template" is a string and "context"
is an object:var rendered = whiskers.render(template, context);
A template might look something like this:
{>header}
{if tags}
- {tag}
{for tag in tags}
{/for}
{else}
No tags!
{/if}
{!
this paragraph is
commented out
With the following context:
{
header: '
{title}
\n{author}
',title: 'My life',
author: 'Bars Thorman',
tags: [
'real',
'vivid'
],
content: 'I grew up into a fine willow.'
}
It would be rendered as this:
My life
Bars Thorman
- real
- vivid
Usage
-----
Whiskers keeps templates readable by limiting tags to variables, statements
("for", "if", and "else"), partials, and comments.
Variable tags retrieve data from the context. They may use dot notation, and
hyphens are allowed:
{object.a-variable}
A "for" tag loops over variables in an array:
{for variable in array}
{variable}
{/for}
An "if" tag only displays a section of the template for a truthy variable, or
the inverse:
{if variable}
{variable}
{/if}
{if not variable}
No variable!
{/if}
As you can see, "for" and "if" sections are closed by a corresponding tag with
a leading slash. The previous example could also be shortened:
{if variable}
{variable}
{else}
No variable!
{/if}
The "else" tag can also be used with "for" to display something when the array
is empty:
{for variable in array}
{variable}
{else}
Nothing in the array!
{/for}
A partial tag begins with a greater-than sign. It renders any template
assigned to that variable with the current context:
Comment tags comment out part of the template. They begin and end with
exclamation points. They can include newlines, spaces, and other tags.
{!these words and this {tag}
will not be rendered!}
Any tag is escaped from rendering by prepending a backslash:
\{variable}
See the test directory for server and browser usage examples.
Forebears
---------
Whiskers was influenced by these fine projects:
*
*
*
*