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

tem

v0.0.2

Published

A little, flexible template engine for JavaScript

Downloads

44

Readme

tem, a small, fast, flexible template engine in JavaScript

  • Tiny (less than 1KB)
  • As fast (or nearly) as doT
  • Support for master pages
  • Support for partials
  • Pluggable (add your own commands)

Build Status

Usage

tem is mustache like. It uses the {{sym args}} where sym is the symbol or command, and args are the arguments, if any. Blocks such as for or if are closed with {{/}}

The model is referred to as it, just as in doT.

Conditionals

<h1>Hi,
{{if it.age < 20}}
  <span class="young">Youngster</span>
{{else-if it.age > 100}}
  <span class="old">Oldster</span>
{{else}}
  <span class="meh">You</span>
{{/}}
</h1>

Loops

<ul class="users">
{{for it.users u}}
  <li>{{= u.name}}</li>
{{/}}
</ul>

Or, if you want the index:

<ul class="users">
{{for it.users u, x}}
  <li>{{= u.name}} is number {{- x}}</li>
{{/}}
</ul>

Escaping

The = option escapes any HTML in the model before rendering it. The - option does not.

<h1>{{= it.name}} is escaped</h1>
<h2>{{- it.name}} is not escaped</h2>

Partials

Partials can be called like so:

dotx.add('myview', '<h1>{{= it.name}}</h1>');
dotx.add('deets', '{{tem myview}}');

dotx('deets', { name: "Greg" }); // <h1>Greg</h1>

Partials get the it context from the caller by default, but this can be overridden.

dotx.add('myview', '<h1>{{= it.name}}</h1>');
dotx.add('deets', '{{tem myview { name: "Chris" } }}');

dotx('deets', { name: "Greg" }); // <h1>Chris</h1>

Masters

It's often useful to have a master template that wraps child templates.

Master template (let's say it's named page-layout):

<main>
  <header>This is the header</header>
  <article>{{yield}}</article>
  <footer>Copyright (c) 2083</footer>
</main>

Child template:

{{master page-layout}}
  <h1>This is the child</h1>
{{/}}

That combination will produce this:

<main>
  <header>This is the header</header>
  <article><h1>This is the child</h1></article>
  <footer>Copyright (c) 2083</footer>
</main>

Adding your own commands

A command defined like this:

tem.cmd('rand', function (cmd, args, context) {
  return Math.floor(Math.random() * parseInt(args));
});

Would be called like this:

{{rand 45}}

Arguments

  • cmd is the command, in this case 'rand'
  • args is the string representation of the arguments '45'
  • context is an array
    • if your command is not self-closing, then
    • push the closing string onto the array

Commands with closing blocks

tem.cmd('wrap', function (cmd, args, context) {
  args = args.trim();
  context.push('</' + args + '>');
  return '<' + args + '>';
});

When called like this:

{{wrap div}}Hello world{{/}}

Would produce this:

<div>Hello world</div>

Installation

Just download tem.min.js, or use bower:

bower install tem

Or use npm: https://www.npmjs.com/package/tem

npm install --save tem

Contributing

Make your changes (and add tests), then run the tests:

npm test

If all is well, build your changes:

npm run min

This minifies tem, and tells you the size. It's currently less than 1KB, and I'd like to keep it that way!

License MIT

Copyright (c) 2015 Chris Davies

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.