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

bracket-templates

v0.1.0

Published

Minimal javascript templating using square brackets.

Downloads

316

Readme

Bracket-Templates

A mimimal JavaScript template engine inspired by mustache using square brackets, supporting default strings, sub-key notation, block iterators and truthy conditional logic.

###Instlation

$ npm install bracket-templates

###Options:

var bracket = require('bracket-templates');
bracket.options = {
  prefix: '',   			// (default = none), an optional placeholder prefix string.
  debug: false  	// (default = false), enable debug messages on the console.
  strictKeys: false	// (default = false), when true, only mutate found keys - partial rendering.
};

###Render Method:

bracket.render(template, dataObj[, options, callback])
  • template - Can be either a String or a Buffer (string-in, string-out & buffer-in, buffer-out).
  • dataObj - Any javascript Object or Array.
  • options - Local options obejct.
  • callback - A callback function.

###Template Tag Syntax: [ {propertyName} [: {defaultValue}] ] Template placeholders are enclosed by square brackets (obviously), and represent data object property names and/or any nested key identifiers (via dot, hyphen, or undersocre notation). Optionally, a default value can be included by adding a colon followed by the default string. Tempalte placeholders support both truthy conditional blocks and list/dict iterator blocks. Additionally, tempalte placeholders may contain an optional prefix.

###Block Constructs: [ [#, ~, ^, !]{propertyName} ] Blocks begin with an opening tag and end with a closing tag, and may span multiple lines. Opening block tags must contain one of the following prefixes preceeding the {propertyName}.

  • # (iterator block)

    • If the propertyName represents an object or array, repeat the block with the value of each element. Otherwise, repeat the block with the full object.
  • ~ (truthy block)

    • Render the block if the propertyName exists and is truthy.
  • ^ (falsy block)

    • Render the block if the propertyName does not exists or is falsy.
  • ! (falsy bock)

    • Render the block if the propertyName does not exists or is falsy.

###Special placeholder names:

  • KEY/INDEX - the current property name or index in a iterator. In a logical block this is the logical key name.
  • VALUE - The current property or item value in an iterator. In a logical block this is the boolean value.

###Rendering a template:

var bracket = require('bracket-templates');
var data = {
  thing: "World"
};

console.log( bracket.render('Hello [thing]. I like [ color : green ].', data) );

This code would return the rendered string "Hello World. I like green." Since there is no property "color" in the data object, the default value is used.

Whitespace inside tag brackets is generally ignored, but preserved inside the context of the {defaultValue}. Both tags [name:default] and [ name : default ] are equivalent.

###Examples:


A basic template with some default text.

[ name : Joe Somebody ]
[ address ]
[ phone ]

Code:

var bracket = require('bracket-templates');
var template = fs.readFileSync('./template.txt');
var data = {
  name: false,
  addres: '123 Any Street, Anytowne, OH',
  phone: 5551212
};
console.log( bracket.render( String(template), data ) );

A templalte using the optional prefix 'object'.

Hello [ object-name ]

Code:

var bracket = require('bracket-templates');
var template = fs.readFileSync('./template.txt');
var data = {
  name: 'Joe User'
};
console.log( bracket.render( String(template), data, {prefix: 'object'} ) );

A template using dot based sub-key notation (alternately, hyphens and underscores are supported)

Hello [ company.employees.0.name : mindless worker ].

Code:

var bracket = require('bracket-templates');
var template = fs.readFileSync('./template.txt');
var data = {
  company: {
    employees: [{
      name: 'Joe User'
    }]
  }
};
console.log( bracket.render( String(template), data ) );

An iterator block using sub-key notation.

Employees:
[ #company.employees ]
  name: [ name ]
[ /company.employees ]

Code:

var bracket = require('bracket-templates');
var template = fs.readFileSync('./template.txt');
var data = {
  company: {
    employees: [{
      name: 'Joe Worker'
    },{
      name: 'Jane Person'
    }]
  }
};
console.log( bracket.render( String(template), data ) );

An object iterator using specials.

[ #properties ]
  [ KEY ]: [ VALUE ]
[ /properties ]

Code:

var bracket = require('bracket-templates');
var template = fs.readFileSync('./template.txt');
var data = {
  properties: {
    name: 'Joe User',
    age: 37
  }
};
console.log( bracket.render( String(template), data ) );

A truthy conditional block.

[ ~taxable ]
  Please cut off your leg.
[ /taxable ]

Code:

var bracket = require('bracket-templates');
var template = fs.readFileSync('./template.txt');
var data = {
  taxable: 'yes'
};
console.log( bracket.render( String(template), data ) );

A falsy conditional block.

[ ^taxable ]
  Aren't you lucky!
[ /taxable ]

or

You are [ !taxable ] not [ /taxable ] taxable.

Code:

var bracket = require('bracket-templates');
var template = fs.readFileSync('./template.txt');
var data = {};
console.log( bracket.render( String(template), data ) );

A truthy conditional iterator block.

[ #taxable ]
  You owe [ taxRate : 100% ] of your income.
[ /taxable ]

Code:

var bracket = require('bracket-templates');
var template = fs.readFileSync('./template.txt');
var data = {
  taxable: {
    taxRate: '99%'
  }
};
console.log( bracket.render( String(template), data ) );

Escaping some literal bracket text.

Example: \[ someName ] would render [ someName ]

Allternately \[ someName \] will render [ someName ]

Code:

var bracket = require('bracket-templates');
var template = fs.readFileSync('./template.txt');
var data = {
  someName: 'Test'
};
console.log( bracket.render( String(template), data ) );

Please report any issues