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

html-template-compiler

v0.1.2

Published

A simple HTML template compiler that generates compiled templates which are environment-agnostic. (The compiler itself runs in Node.)

Downloads

44

Readme

A simple HTML template compiler that generates compiled templates which are environment-agnostic. (The compiler itself runs in Node.)

Usage

$ npx html-template-compiler templates/

This emits TypeScript code, for the files "templates/*.html", to stdout. (You can use glob-syntax in your shell, too.) The generated code can be called to render templates, and includes relatively comprehensive types.

By default this imports this package's runtime code to render templates. You can pass flag -i to inline the code instead. Either way, use a tree-shaking compiler.

The 'rendered' object is something with a toString() helper.

import { renderIndex } from './generated-template.ts';
const out = renderIndex({ prop: 'hello', there: 'jim' });
const s = out.toString();

Syntax

This supports simple rendering of passed properties:

<div id="{{idName}}" ?disabled="{{foo}}">{{content}}</div>
{{object.property.hello}}

You can use custom tags to handle conditionals:

<hc:if i="foo">
  <div class="foo-is-truthy">{{foo}}</div>
  <hc-else />
  <div class="else">Or else?</div>
</hc-if>

You can pass e.g., !foo to invert the conditional.

Or loops:

<hc:loop iter="foo" v="eachFoo">
  <div class="each-foo-bar">{{eachFoo.bar}}</div>
</hc:loop>
<hc:if iter="!foo">
  <!-- Iterates the first item, don't use on generators -->
  <div class="empty">No items available</div>
</hc:if>

You can also use <hc:else /> within a loop to denote an empty block.

Unsafe

To include unsafe HTML inside other templates, first mark something as unsafe:

import { unsafe } from 'html-template-compiler';
const out = renderIndex({ body: unsafe('<div>hello</div>') });

The rendered objects generated by the compiled code are already denoted unsafe.

TODOs

  • This does not currently support Promise arguments, but it could be modified to do so
  • It doesn't care or know anything about events or live DOM: this is purely for backend or static generation