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

@agarimo/template

v1.0.1

Published

Template engine

Downloads

53

Readme

template

It's able to template strings or objects and fill them from a context.

Example with string

You can use an string as template.

const { template } = require('@agarimo/template');

const result = template(`Hello {{ name }} {{ age }}`, { name: 'Anna', age: 30 });
console.log(result); 
// Hello Anna 30

Or you can compile the template and get a function that you can use with different contexts.

const { compile } = require('@agarimo/template');

const compiled = compile(`Hello {{ name }} {{ age }}`);
const result = compiled({ name: 'Anna', age: 30 });
console.log(result); 
// Hello Anna 30

Example with an object

You can use an object as template.

const { template } = require('@agarimo/template');

const source = {
  name: '{{ name }}',
  age: '{{ age }}',
}

const result = template(source, { name: 'Anna', age: 30 });
console.log(result);
// { name: 'Anna', age: '30' }

Or you can compile the template and get a function that you can use with different contexts.

const { compile } = require('@agarimo/template');

const source = {
  name: '{{ name }}',
  age: '{{ age }}',
}

const compiled = compile(source);
const result = compiled({ name: 'Anna', age: 30 });
console.log(result); 
// { name: 'Anna', age: '30' }

Example with forEach in string

You can use {{ #collection }} to start a forEach over collection, and {{ /# }} to end the forEach.

const { template } = require('@agarimo/template');

const context = {
  name: 'Products',
  category: 'software',
  products: [
    { code: 'SO1', name: 'Software 1', value: 500, parts: ['a', 'b', 'c'] },
    { code: 'SO2', name: 'Software 2', value: 600, parts: ['d', 'e', 'f'] },
    { code: 'SO3', name: 'Software 3', value: 700, parts: ['g', 'h', 'i'] },
  ]
}

const input = `For {{ name }} the category is {{ category }}. We have {{ products.length }} products that are: {{#products}}
- {{ code }}: {{ name }} ({{ value }}€) Composed by: {{ parts.join(', ')}}{{ /# }}`;

console.log(template(input, context));
// For Products the category is software. We have 3 products that are:
// - SO1: Software 1 (500€) Composed by: a, b, c
// - SO2: Software 2 (600€) Composed by: d, e, f
// - SO3: Software 3 (700€) Composed by: g, h, i

The collection can be a javascript expression. Example, you can use filter

const { template } = require('@agarimo/template');

const context = {
  name: 'Products',
  category: 'software',
  products: [
    { code: 'SO1', name: 'Software 1', value: 500, parts: ['a', 'b', 'c'] },
    { code: 'SO2', name: 'Software 2', value: 600, parts: ['d', 'e', 'f'] },
    { code: 'SO3', name: 'Software 3', value: 700, parts: ['g', 'h', 'i'] },
  ]
}

const input = `For {{ name }} the category is {{ category }}. We have {{ products.length }} products that are: {{#products.filter(product => product.value > 500)}}
- {{ code }}: {{ name }} ({{ value }}€) Composed by: {{ parts.join(', ')}}{{ /# }}`;

console.log(template(input, context));
// For Products the category is software. We have 3 products that are:
// - SO2: Software 2 (600€) Composed by: d, e, f
// - SO3: Software 3 (700€) Composed by: g, h, i

Example with iterator in object

If you have an array in an object, each element that contains the field iterator between underscores with a value that starts by #, then it will be consider an iterable object, like in the previous forEach example.

const { template } = require('@agarimo/template');

const source = {
  name: '{{ name }}',
  category: '{{ category }}',
  cards: [
    {
      _iterator_: '#products',
      name: '{{ name }}',
      value: '{{ value }}',
    },
  ],
};

const context = {
  name: 'Products',
  category: 'software',
  products: [
    { code: 'SO1', name: 'Software 1', value: 500, parts: ['a', 'b', 'c'] },
    { code: 'SO2', name: 'Software 2', value: 600, parts: ['d', 'e', 'f'] },
    { code: 'SO3', name: 'Software 3', value: 700, parts: ['g', 'h', 'i'] },
  ],
};

console.log(template(source, context));
// {
//   name: 'Products',
//   category: 'software',
//   cards: [
//     { name: 'Software 1', value: '500' },
//     { name: 'Software 2', value: '600' },
//     { name: 'Software 3', value: '700' }
//   ]
// }

Access the current element when iterating

Sometimes you want to access the current element. Also sometimes the elements are not objects, so you will want to access their values directly. You can do that with current between underscores.

const { template } = require('@agarimo/template');

const source = 'Product list: {{ #products }}\n- Product "{{ _current_ }}"{{ /# }}'

const context = {
  name: 'Products',
  category: 'software',
  products: ["SO1", "SO2", "SO3"]
};

console.log(template(source, context));
// Product list:
// - Product "SO1"
// - Product "SO2"
// - Product "SO3"

Access the parent when iterating

You can access the parent object with parent between underscores.

const { template } = require('@agarimo/template');

const source = 'Product list: {{ #products }}\n- Product "{{ _current_ }}" category is {{ _parent_.category }}{{ /# }}'

const context = {
  name: 'Products',
  category: 'software',
  products: ["SO1", "SO2", "SO3"]
};

console.log(template(source, context));
// Product list:
// - Product "SO1" category is software
// - Product "SO2" category is software
// - Product "SO3" category is software