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

htl-template-loader

v6.2.0

Published

Webpack loader for HTL/Sightly templates

Downloads

4,648

Readme

htl-template-loader

npm version license travis tests last commit pull requests commits

Webpack loader for HTL/Sightly data-sly-template templates. Based on @adobe/htlengine.

Installation

npm install --save htl-template-loader @adobe/htlengine

Usage

Entire files

  1. Add loader to webpack.config.js:
{
  module: {
    rules: [
      {
        test: /\.htl$/,
        use: ["htl-template-loader"],
      },
    ];
  }
}
  1. Create a template file template.htl:
<h1>Hello World</h1>
  1. Import render method
import renderMain from './demo.html';

// Render the entire file
console.log(await renderMain());

Templates

  1. Add loader to webpack.config.js:
{
  module: {
    rules: [
      {
        test: /\.htl$/,
        use: ["htl-template-loader"],
      },
    ];
  }
}
  1. Create a template file template.htl:
<template data-sly-template.greeter="${@ name}">
  <h1>Hello ${name}!</h1>
</template>
  1. Import render method
import { render } from './demo.html';

// If no template name is given use the first exported data-sly-template
console.log(await render({ name: 'Alex' }));

// To call a specific template pass the name as first parameter
console.log(await render('greeter', { name: 'Alex' }));

Loader options

templateRoot

The @adobe/htl-engine ships with a scriptResolver to align with the AEM template resolution logic.
The templateRoot option allows to specify a base folder to lookup relative template paths like example/headline.htl.

The following example would look up example/headline.htl in /my-project/templates/example/headline.htl:

<sly
  data-sly-use.headline="example/headline.htl"
  data-sly-call="${headline.headline @ text=text}"
/>
{
  module: {
    rules: [
      {
        test: /\.htl$/,
        use: {
          loader: "htl-template-loader",
          options: {
            templateRoot: "/my-project/templates",
          },
        },
      },
    ];
  }
}

Typescript Typings

htl-template-loader provides optional typescript typings. If you would like to define that all *.htl files export the htl-template-loader functions you can use:

declare module "*.htl" {
  export const {
    render,
    renderMain,
    getTemplate,
    getTemplates,
    getTemplateNames,
  }: typeof import("htl-template-loader/types");
  export default renderMain;
}

This will give you autocomplete and type detection:

import { render } from "./demo.html";
console.log(await render({ name: "Alex" }));

Unfortunately the htl-template-loader is not able to extract the typings for a specific template.
However you can define the template parameters by hand:

import { getTemplate } from "./demo.html";
const greetTemplate = getTemplate<{ name: string }>("greet");

console.log(await greetTemplate({ name: "Alex" }));

Runtime Models

htl-template-loader allows to define models which can be used inside a template

<template data-sly-template.headline="${@ text}">
  <sly data-sly-use.myModel="com.foo.core.models.myModel" />
  <h1>${myModel.salutation} ${text}</h1>
</template>

Define the model for com.foo.core.models.myModel and execute the template:

import { render } from "./demo.html";
render(
  { text: "Alex" },
  {
    models: { "com.foo.core.models.myModel": { salutation: "hi" } },
  }
);

Runtime Globals

htl-template-loader allows to define global variables which can be used inside a template

<template data-sly-template.editMode="${@ text}">
  <h1>${text}</h1>
  <div data-sly-test="${wcmmode.edit}">Edit mode</div>
  <div data-sly-test="${!wcmmode.edit}">Live mode</div>
</template>

Define the data for wcmmode.edit and execute the template:

import { render } from "./demo.html";
render(
  { text: "Alex" },
  {
    globals: { wcmmode: { edit: true } },
  }
);

Changelog

Since 5.0 the changelog can be found here https://github.com/jantimon/htl-template-loader/releases
Old changelog entries can be found here https://github.com/jantimon/htl-template-loader/blob/b47c6d242903f5ab75c2f7f0935a4e2431dafd1d/CHANGELOG.md

License

MIT