Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
cantinflas
Advanced tools
Readme
Cantinflas is a tiny, logic-less, template engine and subset of Mustache. Perfect for project scaffolding and other non-performance critical tasks.
$ npm install cantinflas
eval()
or new Function()
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.
import cantinflas from 'cantinflas';
const template = `Hello {{name}}!`
const data = { name: 'World' };
const output = cantinflas(template, data);
// => "Hello World"
Just as Mustache, tags are indicated by the double curly brackets. {{person}}
is a tag, as is {{#person}}
.
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.
* {{name}}
* {{age}}
* {{company}}
{
"name": "Robin",
"company": "Wayne Enterprises"
}
* Robin
*
* Wayne Enterprises
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.
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.
Shown.
{{#person}}
Never shown!
{{/person}}
{
"person": false
}
Shown.
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.{{#repo}}
- {{@index}} {{name}}
{{/repo}}
{
"repo": [
{ "name": "resque" },
{ "name": "hub" },
{ "name": "rip" }
]
}
- 0 resque
- 1 hub
- 2 rip
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.
{{#wrapped}}
{{name}} is awesome.
{{/wrapped}}
{
"name": "Willy",
"wrapped": function(inner) {
return `${inner}`
}
}
Willy is awesome.
When the value is non-false but not a object, it will be used as the context for a single rendering of the block.
{{#person?}}
Hi {{name}}!
{{/person?}}
{
"person?": { "name": "Jon" }
}
Hi Jon!
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.
{{#repo}}
- {{name}}
{{/repo}}
{{^repo}}
No repos :(
{{/repo}}
{
"repo": []
}
No repos :(
Return: String
Compiled template.
Type: string
A string template. This the input you want to compile.
Type: Object
Data object to use for the compiling.
MIT © Terkel Gjervig
FAQs
Tiny mustache-like template engine
The npm package cantinflas receives a total of 21 weekly downloads. As such, cantinflas popularity was classified as not popular.
We found that cantinflas demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.