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

@ignis-web/tpl

v1.0.2

Published

This library for convenient templating html and other text content in runtime without special bundlers (as Webpack and etc) or special template engine (as Pug, Handlebars and etc), only javascript

Downloads

8

Readme

Tpl

This library for convenient templating html and other text content in runtime without special bundlers (as Webpack and etc) or special template engine (as Pug, Handlebars and etc), only javascript. It allows to use condition operators if/else if/else and switch/case/default. The library allows you to collect html code in functional style with methods: forEach, each (forEach for object).

Install

npm i @ignis-web/tpl -S

List methods:

  • if/else/else if
  • switch/case/default
  • forEach
  • each
  • class

if/else_if/else

const tpl = require('@ignis-web/tpl');

const score = 12;
const html = `
  <div>
    ${tpl
      .if(score < 0, () => `<span>Negative: ${score}</span>`)
      .else_if(score > 0 && score < 100, () => `<span>From 0 to 100: ${score}</span>`)
      .else(() => `<span>Default: ${score}</span>`)
    }
  </div>
  `;
console.log(html); // => <div><span>From 0 to 100: 12</span></div>

You can use only if or if/else as in usual javascript code. Example if:

let score = -10;
let html = `
  <div>
    ${tpl.if(score < 0, () => `<span>Negative: ${score}</span>`)}
  </div>
  `;
console.log(html); // => <div><span>Negative: -10</span></div>

Example if/else if:

let score = 15;
let html = `
  <div>
    ${
      tpl
      .if(score < 0, () => `<span>Negative: ${score}</span>`)
      .else_if(score > 0 && score < 100, () => `<span>From 0 to 100: ${score}</span>`)
    }
  </div>
  `;
console.log(html); // => <div><span>From 0 to 100: 15</span></div>

Example if/else:


let score = 100;
let html = `
  <div>
    ${tpl
      .if(score < 0, () => `<span>Negative: ${score}</span>`)
      .else(() => `<span>Default: ${score}</span>`)
    }
  </div>
  `;
console.log(html); // =>  <div><span>Default: 100</span></div>
Note:

You can pass string as second argument instead of function, if you want to use static content:

const score = 12;
const html = `
  <div>
    ${tpl
      .if(score < 0, `<span>Negative: ${score}</span>`)
      .else_if(score > 0 && score < 100, `<span>From 0 to 100: ${score}</span>`)
      .else(`<span>Default: ${score}</span>`)
    }
  </div>
  `;
console.log(html); // => <div><span>From 0 to 100: 12</span></div>

switch/case/default

const score = 12;
const html = `
  <div>
    ${tpl
      .switch(score)
        .case(12, () => `<span>Total: ${score*2} === 24</span>`)
        .case(100, () => `<span>Total: ${score*2} === 200 </span>`)
        .default(() => `<span>Default: ${score}</span>`)
    }
  </div>
  `;
console.log(html); // => <div><span>Total 24</span></div>

You can pass string as second argument instead of function, if you want to use static content:

const score = 100;
const html = `
  <div>
    ${tpl
      .switch(score)
        .case(12, `<span>Total: ${score} === 24</span>`)
        .case(100, `<span>Total: ${score} === 100 </span>`)
        .default(`<span>Default: ${score}</span>`)
    }
  </div>
  `;
console.log(html); // => <div><span>Total: 100</span></div>

You can pass list as first argument in .case:

const score = 12;
const html = `
  <div>
    ${tpl
      .switch(score)
        .case([ 12, 25 ], () => `<span>Total: ${score}</span>`)
        .default(() => `<span>Default: ${score}</span>`)
    }
  </div>
  `;
console.log(html); // => <div><span>Total 12</span></div>

forEach

For rendering array:

const scores = [12, 100, 24];
const html = `
  <div>
    ${tpl.forEach(scores, (el, i) => `<p>index = ${i}, value = ${el}</p>`)}
  </div>
`;
console.log(html); // =>
/*
<div>
  <p>index = 0, value = 12</p>
  <p>index = 1, value = 100</p>
  <p>index = 2, value = 24</p>
</div>
*/

each

For rendering object:

const scores = { 'Player 1': 12, 'Player 2': 100, 'Player 3': 24 };
const html = `
  <div>
    ${tpl.each(scores, (name, score) => `<p>name = ${name}, scores = ${score}</p>`)}
  </div>
`;
console.log(html); // =>
/*
<div>
  <p>name = Player 1, scores = 12</p>
  <p>name = Player 2, scores = 100</p>
  <p>name = Player 3, scores = 24</p>
</div>
*/

class

Simple switcher for css classes on element like in Vue js:

const is_auth = true;
const html =
  `<div>
    <p ${tpl.class({ 'login-in': is_auth, 'login-out': !is_auth })}></p>
  </div>`;
);
console.log(html); // <div><p class="login-in"></p><div/>