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 🙏

© 2025 – Pkg Stats / Ryan Hefner

eleventy-html-tag-templates

v2.0.1

Published

11ty plugin to use custom HTML tag templates in your code

Readme

Eleventy HTML Tag Templates Plugin

npm version

Author: David Yue
GitHub: https://github.com/CoconutJJ/eleventy-html-tag-templates-plugin

Transform your Eleventy templates with custom HTML tags that expand into regular HTML during build time. Think of it as a lightweight, template-focused alternative to React JSX components.

Why Use This Plugin?

While Eleventy's shortcodes excel at logic-heavy templates, HTML Tag Templates shine for markup and style-heavy components. This plugin makes it natural to add properties, classes, and IDs to your templates without the visual clutter that comes with repeated shortcode usage in HTML.

Installation

You can install this package with pnpm

$ pnpm add eleventy-html-tag-templates

npm and yarn should work as well.

Quick Start

Configure your .eleventy.js file:

import { HTMLTagTemplates } from "eleventy-html-tag-templates";
import Nunjucks from "nunjucks";

export default function (eleventyConfig) {
  // Set up Nunjucks environment
  let nunjucksEnvironment = new Nunjucks.Environment(
    new Nunjucks.FileSystemLoader("src/_includes")
  );

  // Optional: Enable custom Nunjucks features from your Eleventy config
  eleventyConfig.setLibrary("njk", nunjucksEnvironment);

  const htmlTagTemplates = new HTMLTagTemplates();

  eleventyConfig.addPlugin(htmlTagTemplates.eleventyPlugin(), {
    tagTemplateDirectory: "path/to/templates/dir",
    nunjucksEnvironment: nunjucksEnvironment,
    styleSheetPreprocessor: (filepath) => {
      return readFileSync(filepath);
    },
  });
}

Creating Tag Templates

Basic Templates

Create a Nunjucks file in your templates directory. The filename becomes the tag name:

File: button.njk

<button class="btn">{{ label }}</button>

Usage:

<button label="Click me" />

Custom Tag Names

Override the default tag name using frontmatter:

---
tag: "PrimaryButton"
---
<button class="btn btn-primary">{{ label }}</button>

Usage:

<PrimaryButton label="Submit" />

Template Structure Rules

Each tag template must have exactly one root element:

❌ Invalid - Multiple root elements:

<div class="left">{{ title }}</div>
<div class="right">{{ description }}</div>

✅ Valid - Single root element:

<div class="container">
  <div class="left">{{ title }}</div>
  <div class="right">{{ description }}</div>
</div>

Advanced Features

Paired Tags with Content

Create templates that wrap content using the special content variable:

Template:

---
tag: "Card"
---
<div class="card">
  <h2>{{ title }}</h2>
  <div class="card-body">{{ content }}</div>
</div>

Usage:

<Card title="Welcome">
  <p>This content goes inside the card body.</p>
</Card>

Nested Tag Templates

Tag templates support unlimited nesting levels, allowing you to create complex template hierarchies. You can nest templates in two ways:

  1. Within template definitions - When creating your template structure
  2. During template usage - Within the content wrapped by paired tags

[!WARNING] Exercise caution with nested templates. This plugin cannot detect infinite recursion loops in your templates. Since Nunjucks supports arbitrary logic within templates, it's theoretically impossible to automatically detect whether a template will cause infinite recursion (this is known as the halting problem). Currently, no recursion detection is implemented, so infinite loops will cause your application to hang or crash.

Integrated CSS Styling

Attach stylesheets directly to your templates:

---
tag: "Hero"
stylesheet: "src/_includes/css/hero.css"
---
<section class="hero">
  <h1>{{ title }}</h1>
  <p>{{ subtitle }}</p>
</section>

The stylesheet automatically gets injected into the <head> of pages that use this template. Unused templates won't add unnecessary CSS to your pages.

CSS Preprocessor Support

Use SASS, LESS, or other preprocessors by defining a custom processor function:

// For SASS
eleventyConfig.addPlugin(htmlTagTemplates.eleventyPlugin(), {
  tagTemplateDirectory: "src/_templates",
  nunjucksEnvironment: nunjucksEnvironment,
  styleSheetPreprocessor: (filepath) => {
    return sass.renderSync({ file: filepath }).css.toString();
  },
});

Attribute Forwarding

You can automatically forward all or a select number of attributes using the forward() nunjucks template function

<Tag label="a" name="b" />

with template definition

<AnotherTag {{ forward('label', 'name') }} />

The expansion will be

<AnotherTag label="a" name="b" />

If no argument are passed to forward, it will forward all attributes passed. You can also selectively exclude attributes from being forwarded. This is done using the ! symbol before the attribute name. To do this you first need to tell forward to include all attributes explicitly using *. This example shows how to forward all attributes except label.

<AnotherTag {{ forward('*', '!label') }} />

Configuration Options

| Option | Type | Description | |--------|------|-------------| | tagTemplateDirectory | string | Path to your tag template files | | nunjucksEnvironment | Nunjucks.Environment | Nunjucks environment for rendering | | styleSheetPreprocessor | function | Optional CSS preprocessor function |

Best Practices

  1. Keep templates focused - Each template should represent a single, reusable component
  2. Use semantic filenames - Template filenames should clearly indicate their purpose
  3. Leverage CSS integration - Colocate styles with templates for better maintainability
  4. Take advantage of attribute forwarding - Design templates to work naturally with standard HTML attributes
  5. Consider template composition - Use paired tags to create flexible, composable components

Example: Complete Component

File: src/_templates/alert.njk

---
tag: "Alert"
stylesheet: "src/_includes/css/alert.css"
---
<div class="alert alert-{{ type || 'info' }}" role="alert">
  {% if title %}
    <h4 class="alert-title">{{ title }}</h4>
  {% endif %}
  <div class="alert-content">{{ content }}</div>
</div>

Usage:

<Alert type="warning" title="Important Notice" class="mb-4">
  Please review the terms before proceeding.
</Alert>

This creates a flexible, styled alert component that integrates seamlessly with your existing HTML and CSS workflow.