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

@triskel/template

v1.0.9

Published

JS template engine

Downloads

86

Readme

@triskel/template

Simple, customizable template engine for JavaScript, based on @triskeljs/con-text

ᴋɪʟᴛ ᴊs Build Status dependencies Status GitHub license

Installation

npm install @triskel/template --save

Example data

var data = {
  foo: 'bar',
  crash: {
    test: 'dummy'
  },
  list: ['foo', 'bar', 'foobar'],
  map: {
    hi: 'all',
    bye: 'nobody'
  }
};

Caching templates

template.put('partial-map', '$each{ item,key in map }[${foo}:${key}:${item}]{/}');

template.put('partial-list', '$each{ item,i in list }[${foo}:${i}:${item}]{/}');

// cached templates can be invoked with $include{}

console.log( template('$if{ foo !== \'bar\' }whoops{:}map: $include{\'partial-map\'} {/}', data) );
// returns 'map: [bar:hi:all][bar:bye:nobody]'

console.log( template('$if{ foo !== \'bar\' }whoops{:}list: $include{\'partial-list\'} {/}', data) );
// returns 'list: [bar:0:foo][bar:1:bar][bar:2:foobar]'

Filters


template.filter('months', function (nMonths) {
  return nMonths + (nMonths > 1 ? ' meses' : ' mes' );
});

console.log( template('${ nMonths | months }')({ nMonths: 5 }) );
// returns '5 meses'
console.log( template('${ nMonths | months }')({ nMonths: 1 }) );
// returns '1 mes'

var messages {
  greeting: template('Hi ${name}!')
};

template.filter('message', function (key, data) {
  return messages[key](data);
});

console.log( template('${ person.last_name }: ${ \'greeting\' | message: { name: person.first_name } }')({
  person: {
    first_name: 'John',
    last_name: 'Smith'
  }
}) );
// returns 'Smith: Hi John!'

HTML Example

tests/example.html

<!DOCTYPE html>
<html lang="${ lang }">
  <head>
    <meta charset="utf-8">
    <title>Hello world</title>
  </head>
  <body>

  <p>Hi! My name is ${ profile.first_name }</p>

  <ul>
    $each{ text in list }<li>${ text }</li>{/}
  </ul>

  $if{ is_dev }
    <script type="application/javascript">
    (function (h,s) {
    s.type='text/javascript';s.src='//' + location.hostname + ':35729/livereload.js';h.appendChild(s);
    })(document.getElementsByTagName('head')[0], document.createElement('script') );
    </script>
  {/}
  </body>
</html>

processing HTML (template TESTS)

var fs = require('fs'),
    triskel = require('triskel');

var html = fs.readFileSync( 'src/index.html', { encoding: 'utf8' }),
    renderTemplate = triskel(html);

fs.writeFileSync( 'public/index.html' , renderTemplate({
  lang: 'en-US',
  is_dev: true,
  profile: {
    first_name: 'John'
  },
  list: [
    'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.',
    'Aliquam tincidunt mauris eu risus.',
    'Vestibulum auctor dapibus neque.',
    'Nunc dignissim risus id metus.',
    'Cras ornare tristique elit.',
    'Vivamus vestibulum ntulla nec ante.',
    'Praesent placerat risus quis eros.',
    'Fusce pellentesque suscipit nibh.',
    'Integer vitae libero ac risus egestas placerat.',
    'Vestibulum commodo felis quis tortor.',
    'Ut aliquam sollicitudin leo.',
    'Cras iaculis ultricies nulla.',
    'Donec quis dui at dolor tempor interdum.',
  ]
}), { encoding: 'utf8' });

Resulting HTML (tests/example-result.html)

<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <title>Hello world</title>
  </head>
  <body>

  <p>Hi! My name is John</p>

  <ul>
    <li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
    <li>Aliquam tincidunt mauris eu risus.</li>
    <li>Vestibulum auctor dapibus neque.</li>
    <li>Nunc dignissim risus id metus.</li>
    <li>Cras ornare tristique elit.</li>
    <li>Vivamus vestibulum ntulla nec ante.</li>
    <li>Praesent placerat risus quis eros.</li>
    <li>Fusce pellentesque suscipit nibh.</li>
    <li>Integer vitae libero ac risus egestas placerat.</li>
    <li>Vestibulum commodo felis quis tortor.</li>
    <li>Ut aliquam sollicitudin leo.</li>
    <li>Cras iaculis ultricies nulla.</li>
    <li>Donec quis dui at dolor tempor interdum.</li>
  </ul>

  <script type="application/javascript">
    (function (h,s) {
    s.type='text/javascript';s.src='//' + location.hostname + ':35729/livereload.js';h.appendChild(s);
    })(document.getElementsByTagName('head')[0], document.createElement('script') );
  </script>

  </body>
</html>