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

tbt

v0.1.0

Published

mostly handlebars compatible template engine with logic

Downloads

8

Readme

/*   __ ___.    __    *
 * _/  |\_ |___/  |_  *
 * \   __\ __ \   __\ *
 *  |  | | \_\ \  |   *
 *  |__| |___  /__|   *
 * (c)toyboy \/ 2016  */

Tiny and mostly ~~harmless~~ handlebars compatible template engine

Features?

  • Common.js, amd and global module
  • Looping objects and arrays with support to get key, index, first and last of each iteration
    • {{#each products}}{{>product}}{{/each}}
  • Variable resolver that supports paths like ..products.0.name and "strings"
  • Conditionals if/else if/else with all kinds of crazy !<>=%/ operators that lets you do stuff like
    • class="{{#if price > 9000}}too-expensive{{else if price > 1000}}maybe{{else}}can-afford{{/if}}"
  • Ternary operator for simple stuff like
    • class="{{= price > 1000 ? 'apple-prices' : 'normal-prices'}}"
  • Allows you to assign variables, can also be used to do math like {{..total += price}}
  • Partials support, with inline generation {{<myPartial}}code goes here{{/myPartial}} and parameters!
  • Custom helpers! Not as fancy way of doing them as handlebars but good-enough
  • Includes a cool command line compiler (like all the big boys do)
  • Will break your white spaces all day and not get tired! Or try to allow you to somewhat control them like in handlebars {{~this~}}
  • Includes template fetch via fs.* when in node.js and XMLHttpRequest when running in browserlike enviroments
  • Doesn't require a runtime as exported templates include everything needed to run them
  • Even compiled version is able to load other pre-compiled templates/partials/helpers

Tiny!

The whole thing (compiler and all) comes in at 19kb (under 8kb minified), the "runtime" part (included in compiled template/partial/helper bundle) weights around 860 bytes minified and around 430 bytes if no looping support is needed!

As a comparison: handlebars (with compiler and all) is 75kb minified and even the runtime is 16kb when minified, you know you could fit the full tbt twice in that space, just saying.. still a bit fat compared to doT.js though..

How to use?

npm intall tbt -g
var tbt = require('tbt');

// sets the default search path for templates, we assume templates have the extension .tbt
tbt.path = '/var/www/templates/'; 
// tbt will look for /var/www/templates/myTemplate.tbt compile it and run it with data given
tbt('myTemplate', {here: {is: ['my','data'] } }, function(err, text) {
    if(!err) {
        console.log(text);
    }
});

var fs = require('fs');
// Export returns the "runtime" version of all templates/partials/helpers in cache (loaded in with the same tbt in the same session)
fs.writeFileSync('myCompiledTemplates.js', tbt.export()); 
...
<script src="myCompiledTemplates.js"></script>
<script>
tbt('myTemplate', {here: {is: ['some', 'new', 'data'] } }, function(err, text) {
    if(!err) $('#myPlaceholder').html(text);
});
</script>
...

Few more examples can be found in examples/ folder and in the few tests included.

Logic

Supports:

  • if / else if / else
  • each
  • < (save partial)
  • > (load partial)
  • = variable ? true : false (ternary, adds result to output)
  • variable = 1 + 1 (assigning variables inline)

Why?

Handlebars is too big (and lacks alot of logic) and doT is a bit too simple (+ it doesn't even try to be handlebars compatible'ish). And a lot of these template engines that advertise "light weight" "small compiled templates" etc. actually are quite big when you add the runtime needed to actually get any output from the template.

And because why not? How hard can it be? :)

License

tbt is licensed under the MIT license (see LICENSE)

Inspired by:

doT.js by Laura Doktorova

Handlebars.js by Yehuda Katz

And by this article by Krasimir Tsonev