npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

whiskers

v0.4.0

Published

A mustachioed templating library

Downloads

2,253

Readme

Whiskers.js

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!

Installation

For the browser, drop the minified version at dist/whiskers.min.js in your scripts directory (or source it directly from GitHub at http://gsf.github.io/whiskers.js/whiskers.min.js).

For node, npm install whiskers.

For use in Express, see examples/express.

Tests

For the browser, visit test/browser/index.html.

For node, npm test.

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:

<article>
  <header>
    {>header}
  </header>
  {if tags}
    <ul id="tags">
      {for tag in tags}
      <li>{tag}</li>
      {/for}
    </ul>
  {else}
    <p>No tags!</p>
  {/if}
  <div>{content}</div>
  {!<p>this paragraph is 
    commented out</p>!}
</article>

With the following context:

{
  header: '<h1>{title}</h1>\n<p id="by">{author}</p>',
  title: 'My life',
  author: 'Bars Thorman',
  tags: [
    'real',
    'vivid'
  ],
  content: 'I grew up into a fine willow.'
}

It would be rendered as this:

<article>
  <header>
    <h1>My life</h1>
    <p id="by">Bars Thorman</p>
  </header>
  <ul id="tags">
    <li>real</li>
    <li>vivid</li>
  </ul>
  <div>I grew up into a fine willow.</div>
</article>

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}
  <p>{variable}</p>
{/for}

An "if" tag only displays a section of the template for a truthy variable, or the inverse:

{if variable}
  <p>{variable}</p>
{/if}
{if not variable}
  <p>No variable!</p>
{/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}
  <p>{variable}</p>
{else}
  <p>No variable!</p>
{/if}

The "else" tag can also be used with "for" to display something when the array is empty:

{for variable in array}
  <p>{variable}</p>
{else}
  <p>Nothing in the array!</p>
{/for}

A partial tag begins with a greater-than sign. It renders any template assigned to that variable with the current context:

<div>{>partial}</div>

Comment tags comment out part of the template. They begin and end with exclamation points. They can include newlines, spaces, and other tags.

<p>{!these words and this {tag} 
  will not be rendered!}</p>

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: