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

super-templates

v1.2.1

Published

An async HTML/XML templating language for Node.js

Downloads

3

Readme

super-templates test

super-templates is an HTML/XML templating language similar to mustache and handlebars. It supports async/await and streaming. It's designed so that you won't need to prepare custom-formatted data for each template; each template is capable of efficiently fetching its own data, declaratively.

Documentation

Installation

npm install super-templates

Requires Node.js v14.x.x or later.

Usage

const ST = require('super-templates');

// The compiledTemplate is a string, which can be cached offline.
const compiledTemplate = await ST.compile('./template.html');

const template = ST.create(compiledTemplate);

for await (const string of template({ name: 'Josh' })) {
    process.stdout.write(string);
}
template.html
{{let name}}
<html>
    <head>
        <title>Welcome to my site!</title>
    </head>
    <body>
        My name is {{name}}.
    </body>
</html>

Server example

const http = require('http');
const stream = require('stream');
const ST = require('super-templates');

// First, compile the template.
const compiledTemplate = await ST.compile('./template.html');
const template = ST.create(compiledTemplate);

// Then, create the server.
const server = http.createServer((req, res) => {
    if (req.method !== 'GET') {
        // This server only supports GET requests.
        res.writeHead(405).end();
        return;
    }

    res.writeHead(200, { 'Content-Type': 'text/html' });

    // Invoke the template, convert it to a Readable stream, and pipe it to the response.
    stream.Readable.from(template(), { objectMode: false }).pipe(res);
});

server.listen(80);

Comparison with other templating languages

Better readability

In most other templating languages, the only way to know what data the template needs is by searching through every expression within the entire template (and all included templates). With super-templates, all data is explicitly declared (typically at the top of the template file).

Less "glue code"

In most other templating languages, each template depends on a separate "data-fetching" function, which needs to format data specifically for that template. With super-templates, templates are capable of calling async functions to fetch data on their own, and they can use arbitrary JavaScript to format data as needed for display purposes.

Lowest possible latency

In most other templating languages, you need to wait for all data to be fetched before you can render the template, and then you need to render the entire template before you can send it anywhere. With super-templates, rendering starts as soon as possible, even before any data has been fetched, and you can start streaming the output as soon as the first character is rendered.

This can drastically reduce the time it takes for your page to load. For example, if your HTML page's <head> section contains scripts and stylesheets, but the <body> section contains data from your SQL database, super-templates will output the <head> section while it's waiting for your SQL database to respond. Most modern browsers will start parsing your HTML page without waiting for the entire page to download, which means the browser can start requesting scripts and stylesheets before the entire HTML page has even been constructed on your server. You can further improve performance by preloading images and videos.

With super-templates, templates compile into async generator functions which yield strings as soon as possible, without waiting for the entire template to finish executing. You can convert it into a stream by using Readable.from().

Furthermore, when a template is compiled, all embedded expressions get analyzed, and each expression's dependencies are computed. For example, if one expression declares a variable userId, and another expression references userId, we know that the second expression depends on the first one (the actual computation is a bit more complicated, but this illustrates the basic idea). Using this analysis, super-templates is able to figure out the most optimal way of executing each expression. For example, if you invoke three async functions that each pull in data from a database, and all three calls are independent, super-templates will invoke them all in parallel, reducing round-trip latency. Conversely, if each call depends on the result of the previous call, super-templates will invoke them serially. For any template, super-templates is capable of automatically computing the most optimal way to fetch your data, so you don't need to manually code it yourself.

Restrictions

With super-templates, expressions within your template must not cause any observable side-effects. This is required because super-templates tries to invoke everything is parallel (for better performance), which means the order of execution is not predictable. For example, a template expression should not mutate data used in other parts of the template, since the order of mutations is arbitrary. Note that you can still do things such as logging, which doesn't effect the template's output. Don't worry: if you've used templating languages like mustache or handlebars, all of your templates already obey this restriction.

License

MIT