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

cantinflas

v1.0.3

Published

Tiny mustache-like template engine

Downloads

98

Readme

Cantinflas is a tiny, logic-less, template engine and subset of Mustache. Perfect for project scaffolding and other non-performance critical tasks.

Install

$ npm install cantinflas

Core Features

  • lightweight: zero dependencies and only ~50 LOC
  • powerful: support all the features you'd expect from a template-engine
  • safe: No eval() or new Function()
  • minimal API: one function call, that's all it take
  • familiar: Cantinflas is almost a full mustache implementation
  • formatting: respects the formatting of your source files

Why

I needed a simple way to replace a few placeholders as a part of a scaffolding tool. There's no need for big high-performance template engines with tons of dependencies. I see a lot of CLI tools depend on projects like handlebars for stuff like this which in my opinion is overkill. KISS. XO.

Usage

import cantinflas from 'cantinflas';

const template = `Hello {{name}}!`
const data = { name: 'World' };

const output = cantinflas(template, data);
// => "Hello World"

Tag Types

Just as Mustache, tags are indicated by the double curly brackets. {{person}} is a tag, as is {{#person}}.

Variables

The most basic tag type is the variable. A {{name}} tag in a basic template will try to find the name key in the current context. If there is no name key found, nothing will be rendered. Keys that are not found in the data object are ignored.

Template
* {{name}}
* {{age}}
* {{company}}
Data
{
  "name": "Robin",
  "company": "Wayne Enterprises"
}
Output
* Robin
*
* Wayne Enterprises

Sections

Sections render blocks of text one or more times, depending on the value of the key in the current context. A section begins with a pound and ends with a slash. That is, {{#person}} begins a "person" section while {{/person}} ends it. The behavior of the section is determined by the value of the key.

False Values or Empty Lists

If the person key exists and has a value of false or an empty list, the content between the pound and slash will not be displayed.

Template
Shown.
{{#person}}
  Never shown!
{{/person}}
Data
{
  "person": false
}
Output
Shown.

Non-empty Lists

If the person key exists and has a non-false value, the content between the pound and slash will be rendered and displayed one or more times. When the value is a non-empty array or object, the text in the block will be displayed once for each item in the list. The context of the block will be set to the current item for each iteration. In this way we can loop over collections.

When interating lists, there's four special variables availble for you.

  • .: This is the current item in the array.
  • @index: This is the index of the current item.
  • @first: True if the current iteration is the first.
  • @last: True if the current iteration is the last.
Template
{{#repo}}
 - {{@index}} {{name}}
{{/repo}}
Data
{
  "repo": [
    { "name": "resque" },
    { "name": "hub" },
    { "name": "rip" }
  ]
}
Output
- 0 resque
- 1 hub
- 2 rip

Function

When the value is a function, it will be invoked and passed the block of text. The text passed is the literal block, unrendered. {{tags}} will not have been expanded.

Template
{{#wrapped}}
  {{name}} is awesome.
{{/wrapped}}
Data
{
  "name": "Willy",
  "wrapped": function(inner) {
    return `${inner}`
  }
}
Output
Willy is awesome.

Non-False Values

When the value is non-false but not a object, it will be used as the context for a single rendering of the block.

Template
{{#person?}}
  Hi {{name}}!
{{/person?}}
Object
{
  "person?": { "name": "Jon" }
}
Output
Hi Jon!

Inverted Sections

An inverted section begins with a caret (hat) and ends with a slash. That is {{^person}} begins a "person" inverted section while {{/person}} ends it. Inverted sections render text once based on the inverse value of the key. That is, they will be rendered if the key doesn't exist, is false, or is an empty list.

Template
{{#repo}}
  - {{name}}
{{/repo}}
{{^repo}}
  No repos :(
{{/repo}}
Object
{
  "repo": []
}
Output
No repos :(

API

cantinflas(template, data)

Return: String

Compiled template.

template

Type: string

A string template. This the input you want to compile.

data

Type: Object

Data object to use for the compiling.

License

MIT © Terkel Gjervig