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

node-html-constructor

v4.1.0

Published

Easy Server Side Rendering!

Downloads

44

Readme

node-html-constructor

Easy Server Side Rendering! All information can be passed into a simple variable!

Installation

npm i node-html-constructor
import { v4 as render } from 'node-html-constructor';
// or
import render from 'node-html-constructor/versions/v4';
// require
const render = require('node-html-constructor').v4;

Usage

Usage will look like this:

const html = fs.readFileSync('./index.html', 'utf-8');

const cstr = {} // Properties to be passed into the html (see below)

const result = render(html, cstr);

The Constructor

The constructor is broken down into 3 parts:

  1. Replacing
  2. Looping
  3. Conditional Rendering

Replacing

This is very simple, any property in the constructor that is in the html as {{ property }} will be replaced with the value of the property. For example:

const html = '<h1>{{ title }}</h1>';

const cstr = {
    title: 'Hello World!'
}

const result = render(html, cstr); // <h1>Hello World!</h1>

The double brackets are used because it's unlikely any string of text in english will have double brackets in it. If you want to use double brackets in your html, you can escape them with a backslash: \\{\\{ and \\}\\}.

Looping

Looping is done using <repeat> tags. All tags used by node-html-constructor are lowercase and require an id to be used, otherwise they will be removed. For example:

const html = `
    <repeat id="users">
        <p>{{ name }}</p>
    </repeat>
`;

const cstr = {
    users: [
        { name: 'John' },
        { name: 'Jane' }
    ]
}

const result = render(html, cstr); // <p>John</p><p>Jane</p>

As you can see, the rendering function looks for users in cstr and loops through it, replacing {{ name }} with the value of name in each object.

Conditional Rendering

Conditional rendering is done using <script cstr> tags. These tags are used to run javascript code on the server side. For example:

const html = `
    <script cstr id="user">
        if (${name}) {
            return '<p>${name}</p>';
        } else {
            return '<p>No user found!</p>';
        }
    </script>
`;

const cstr = {
    user: {
        name: 'John'
    }
}

const result = render(html, cstr); // <p>John</p>

IMPORTANT:

  • The <script> tag MUST have the cstr attribute.
  • The javascript inside the tag must use return statements
  • This must return a string
  • The string does not need to be wrapped in html tags, but it can be
  • The code inside the tag is run in a sandboxed environment, so it cannot access any variables outside of the tag unless explicitly passed into the property of the cstr object. Don't pass in anything you don't want the code to have access to!

Nesting

All of these properties can be nested inside each other. For example:

const html = `
    <repeat id="users">
        <script cstr id="user">
            if (${name}) {
                return '<p>${name}</p>';
            } else {
                return '<p>No user found!</p>';
            }
        </script>
    </repeat>
`;

const cstr = {
    users: [
        { name: 'John' },
        { name: 'Jane' }
    ]
}

const result = render(html, cstr); // <p>John</p><p>Jane</p>

Basically, this calls render() on each object in users and replaces the <script> tag with the result. However, you cannot nest <repeat> tags inside a <script> tag. Maybe in the future!