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

js-email-template-compiler

v0.0.2

Published

JS Email Template Compiler with TypeScript support

Downloads

12

Readme

JS Email Template Compiler

A JavaScript library for compiling email templates with dynamic content, conditional rendering, and iteration using jsdom. The logic is handled via custom HTML attributes such as js-value, js-if, js-if-not, and js-each.

Table of Contents

Installation

Install the package using npm:

npm install js-email-template-compiler

Usage

JavaScript Example

const { renderTemplate } = require('js-email-template-compiler');

// Example HTML template
const htmlTemplate = `
    <div>
        <p js-value="user.name"></p>
        <p js-if="user.isAdmin">Admin Section</p>
        <p js-if-not="user.isAdmin">User Section</p>
        <ul>
            <li js-each="item in user.items">
                <span js-value="item.name"></span>
            </li>
        </ul>
    </div>
`;

// Example data
const data = {
    user: {
        name: 'John Doe',
        isAdmin: true,
        items: [
            { name: 'Item 1' },
            { name: 'Item 2' }
        ]
    }
};

// Render template
renderTemplate(htmlTemplate, data, true).then((renderedHtml) => {
    console.log(renderedHtml);
});

TypeScript Example

import { renderTemplate } from 'js-email-template-compiler';

const htmlTemplate = `
    <div>
        <h1 js-value="user.name">Placeholder</h1>
        <h2 js-if="user.isAdmin">Admin Section</h2>
        <h3 js-if-not="user.isAdmin">User Section</h3>
        <ul>
            <li js-each="order in user.orders">
                <span js-value="order.name"></span> - <span js-value="order.date"></span>
            </li>
        </ul>
    </div>
`;

const data = {
    user: {
        name: "John Doe",
        isAdmin: true,
        orders: [
            { name: "Order 1", date: "2023-01-01" },
            { name: "Order 2", date: "2023-02-01" }
        ]
    }
};

renderTemplate(htmlTemplate, data, true).then((renderedHtml) => {
    console.log(renderedHtml);
});

Options

The renderTemplate function accepts the following arguments:

  1. htmlString (string): The HTML template string to process.
  2. data (object): The data object for binding dynamic values.
  3. removeJsAttributes (boolean): Whether to remove js-* attributes after rendering. If true, these attributes will be removed from the resulting HTML.

Attributes

The library uses custom attributes to define dynamic logic inside the HTML template.

js-value

Inserts dynamic content into the template from the provided data.

Example:

<p js-value="user.name"></p>

This replaces the content of the <p> tag with the value of user.name from the data object.

js-if

Conditionally renders an element if the expression evaluates to true.

Example:

<p js-if="user.isAdmin">Admin Section</p>

This shows the element only if user.isAdmin is true.

js-if-not

Conditionally renders an element if the expression evaluates to false.

Example:

<p js-if-not="user.isAdmin">User Section</p>

This shows the element only if user.isAdmin is false.

js-each

Iterates over an array and renders multiple elements based on the items in the array.

Example:

<ul>
  <li js-each="item in user.items">
    <span js-value="item.name"></span>
  </li>
</ul>

This repeats the <li> element for each item in the user.items array.

Examples

Advanced Example with Nested Iteration

const { renderTemplate } = require('js-email-template-compiler');

// Example HTML template
const htmlTemplate = `
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Template Renderer</title>
</head>
<body>
    <h1 js-value="user.name">example name</h1>
    <h1 js-value="user.email">example email</h1>
    <h2 js-if="user.isAdmin">Admin Section
        <span js-if="user.isSuperAdmin">Super Admin Section</span>
    </h2>
    <table border="1">
        <tr>
            <th>Name</th>
            <th>Email</th>
            <th>Orders</th>
            <th>Admin</th>
        </tr>
        <tr js-each="user in users">
            <td js-value="user.name"></td>
            <td style="text-align: center" js-value="user.email"></td>
            <td>
                <table border="1">
                    <tr js-each="order in user.orders">
                        <td js-value="order.name">example order name</td>
                        <td js-value="order.date">example order date</td>
                        <td js-value="global.user.name">example global name</td>
                        <td js-value="user.name">example user name</td>
                    </tr>
                </table>
            </td>
            <td style="text-align: center" js-if="user.isAdmin">Admin</td>
            <td style="text-align: center" js-if-not="user.isAdmin">Not Admin</td>
        </tr>
    </table>

</body>
</html>
`;

// Example data
const data = {
    user: { name: "John Doe", email: "[email protected]", isAdmin: true, isSuperAdmin: false },
    users: [
        {
            name: "Alice",
            email: "[email protected]",
            isAdmin: true,
            orders: [
                { name: "Laptop", date: "2023-01-01" },
                { name: "Phone", date: "2023-02-01" }
            ]
        },
        {
            name: "Bob",
            email: "[email protected]",
            isAdmin: false,
            orders: [{ name: "Tablet", date: "2003-03-01" }]
        }
    ]
};

// Render the template with the data
renderTemplate(htmlTemplate, data, true).then((renderedHtml) => {
    console.log(renderedHtml);
});

License

This project is licensed under the ISC License.