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

picolate

v2.2.0

Published

A minimalistic and flexible templating engine, inspired by Handlebars.

Downloads

9

Readme

Picolate

A minimalistic and flexible templating engine, inspired by Handlebars.

Install

npm install --save picolate

Language

The following composable building blocks are supported. Anything else that is not part of any of these building blocks is simply left untouched in the template.

Custom delimiters can be configured too, to fit the language to your needs. By default they are {{ and }}.

Comment

A comment does not render anything, it gets stripped out from the template.

{{!-- Some arbitrary comment --}}

Expression

An expression is eval-ed, its return value coerced into a string, and that gets interpolated in the template. Any valid JS expression can be used.

<p>{{name}}<p>
<p>{{person.name}}<p>

Each

An each block can be used to iterate over an array, rendering the content of the block once for each value.

The as name part is required, you always need to provide a name to use for each iterated value.

An else branch is supported too, it will be rendered when the array is empty.

{{#each people as person}}
  <p>{{person.name}} {{person.surname}}</p>
{{else}}
  <p>No people...</p>
{{/each}}

If

An if block is only rendered if its expression is truthy. Any valid JS expression can be used.

An else branch is supported too, it will be rendered when the condition is falsy.

{{#if isVisible}}
  <p>Something is visible...<p>
{{else}}
  <p>Something is not visible...<p>
{{/if}}

With

A with block is conceptually analogous to a with statement, it adds properties of the provided object to the current scope.

An else branch is supported too, it will be rendered when the value is falsy.

{{#with person}}
  <p>{{name}} {{surname}}</p>
{{else}}
  <p>No person...</p>
{{/with}}

Usage

Once you have written a template there are few things that you can do with it:

import picolate from 'picolate';

// Render a template, without pre-compiling it, which is slower if you need to render it multiple times

const html = picolate.render ( '<p>{{name}}</p>', { name: 'John' } );

// Compile a template, parsing it once, which is faster if you need to render it multiple times

const template = picolate.compile ( '<p>{{name}}</p>' );
const html = template ({ name: 'John' });

// Validate that a template is valid before doing anything else with it

const isValid = picolate.validate ( '{{#if person}}<p>{{name}}</p>' ); // => false

// Parse a template into an AST, this is low-level function you might never need

const ast = picolate.parse ( '{{#if person}}{{person.name}}{{/if}}' );
// {
//   type: 'root',
//   children: [
//     {
//       type: 'if',
//       value: 'person',
//       children: [
//         {
//           type: 'eval',
//           value: 'person.name'
//         }
//       ]
//     }
//   ]
// }

// Provide arbitrary values to the context object

import _ from 'lodash';

const html = picolate.render ( '<p>{{_.startCase ( name )}}</p>', { _: lodash, name: 'some-name' } );

// Escape interpolated values
// Values are converted to a string by passing them to the "String" function, so you can provide your own "String" function to escape values

import {escape} from 'html-escaper';

const html = picolate.render ( 'Escaped: {{`& < > " \'`}}', { String: value => escape ( String ( value ) ) } ); // Escaped: &amp; &lt; &gt; &quot; &#39;

// Custom delimiters
// Every exported function supports an optional argument that you can use to set custom delimiters

const options = { delimiters: ['[[', ']]'] };
const html = picolate.render ( '<p>[[name]]</p>', { name: 'John' }, options );

// Compose multiple building blocks together

const template = `
  <h1>People</h1>
  {{!-- Rendering something for each person --}}
  {{#each people as person}}
    <div>
      <h2>
        <span>{{person.name}}</span>
        {{#if person.admin}}
          <span>(admin)</span>
        {{/if}}
      </h2>
    </div>
  {{/each}}
  {{!-- Rendering some fallback content if there are no people --}}
  {{#if !people.length}}
    <p>No people...</p>
  {{/if}}
`;

License

MIT © Fabio Spampinato