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

handlebartender

v1.0.1

Published

Allows you to render Handlebar templates anywhere in your backend logic.

Downloads

10

Readme

handlebartender

Allows you to render Handlebar templates anywhere in your backend logic.

Installing

npm i handlebartender

Basic Usage

//require handlebartender
var HBT = require('handlebartender');

//tell handlebartender where the templates are to compile
var templates = HBT.compile({
  templatePath: __dirname + '/some/template/path/'
});

//you now have two ways to render your templates with data
var myData = { title: 'Hey!'}

//You should omit the extension name
templates.render('templateName', myData);

//the above is the equivalent to
templates['templateName'](myData);

Partials

By default handlebartender will look in your templatePath under the subdirectory 'partials'. For this reason avoid storing templates that are not intended to be partials in this directory. You also have the option to pass your partialsPath to the compile method if you decide to store them somewhere else.

var templates = HBT.compile({
  templatePath: __dirname + '/some/template/path/',
  partialsPath: __dirname + '/another/path/'
});

You can also register a partial the old fashioned way by accessing the handlebartenders internal Handlebars instance. If you choose to do this, make sure you do it BEFORE you compile templates, otherwise the partial will not be available for use in your templates and you will error out.

var HBT = require('handlebartender');
HBT.Handlebars.registerPartial('header', '<h2>{{title}}</h2>');

Helpers

Adding helpers to handlebartenders is also simple. You can pass a key value pair of a helper name and function as the 'helper' option to the compile method. For example:

//make a helper we can use

var jsonHelper = function(context) {
  return JSON.stringify(context);
};

var templates = HBT.compile({
  templatePath: __dirname + '/some/template/path/',
  helpers: {
    json: jsonHelper
  }
});

As with partials, you can register a helper with handlebartenders internal Handlebars instance as well. When registering a partial via the built in Handlebars instance, you need to do this before compiling templates otherwise you will get an error when using it.

HBT.Handlebars.registerHelper('link', function(text, url) {
  text = HBT.Handlebars.Utils.escapeExpression(text);
  url  = HBT.Handlebars.Utils.escapeExpression(url);

  var result = '<a href="' + url + '">' + text + '</a>';

  return new HBT.Handlebars.SafeString(result);
});

Optional Arguments

partialsPath - defaults to templatePath + '/partials'

helpers - defaults to {}

extension - defaults to .hbs

Tests

npm test

There aren't a ton of tests right. But any contributions on this front are welcome.

On pre-1.0 version?

I have created a 0.0.8x branch for anyone interested in the old usage (https://github.com/battlejj/handlebartender/tree/v0.0.8), however, I will not be supporting it.