GithubHelp home page GithubHelp logo

html-tagged-template-stream's Introduction

HTML Tagged Template Stream demo

html-tagged-template-stream

Streaming HTML generation with tagged template literals.

Features

  • Tagged template literals are converted to generators for lazy evalutation.
  • React style suspense.
  • Expressions are automatically escaped.
  • Automatic caching of static template parts with umap and WeakSet (coming soon hopefully)

Installation

npm install html-tagged-template-stream
yarn add html-tagged-template-stream
pnpm add html-tagged-template-stream
bun add html-tagged-template-stream

Usage

import { render, html } from "html-tagged-template-stream";

const writable = process.stdout; // Could be anything with a 'write' method such as a HTTP Response or Writable Stream.

render(process.stdout, html`<h1>Hello World it's ${new Date()}</h1>`);

Async templates

Async templates are awaited blocking the renderer.

import { render, html } from "html-tagged-template-stream";

render(process.stdout, html`<h1>${Promise.resolve(html`Hi there`)}</h1>`);

Suspense

Async templates may be deferred and a fallback rendered in their place. The renderer will send down a script to swap the async template result into the fallbacks location in the dom.

import { render, html, suspense } from "html-tagged-template-stream";

render(
  process.stdout,
  html`<p>I will be rendered straight away.</p>
    ${suspense(
      html`I will be replaced in two seconds.`,
      new Promise((res) => {
        setTimeout(res(html`I am lazy loaded content.`), 2000);
      })
    )}
    <p>I will also be rendered straight away.</p>`
);

Arrays

Arrays are expected to be arrays of Templates.

import { render, html } from "html-tagged-template-stream";

const fruit = ["apple", "banana", "pear"];

render(
  process.stdout,
  html`<ul>
    ${fruit.map((fruit) => html`<li>${fruit}</li>`)}
  </ul>`
);

Syntax Highlighting

For syntax highlighting in VSCode install the 'lit-html' extension by Matt Bierner;

html-tagged-template-stream's People

Contributors

jacob-roling avatar github-actions[bot] avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.