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

processhtmltemplate

v2.1.1

Published

process template HTML against data

Downloads

12

Readme

introduction

Basic typescript module that translate a template and data in a resulting output. You provide a template (HTML, XML, PDF ...) associate data in JSON and let the module merge them to have a personalized result.

This is base on the lodash.template method, so for detail on how to specify the template and the data, please refer to the lodash documentation (https://lodash.com/docs/4.17.15#template).

How to use template

You will have to insert tag with associated variable to obtain a desired result.

insert a variable

{ 'user': 'fred', 'description' : 'IT technician' }
// for inserting the user variable use the following tag in your template
<%= user %>!

Escaping HTML delimiters

{ 'value': '<script>', 'description' : 'example of escaping value' };
// using the HTML "escape" delimiter to escape data property values
'<b><%- value %></b>'

Repeating a pettern

{ 'users': ['fred', 'barney', 'henry', 'paul'] }
// using the "evaluate" delimiter to execute JavaScript and generate HTML
'<% _.forEach(users, function(user) { %><li><%- user %></li><% }); %>'

using the print command

{ 'user': 'barney', 'description' : 'cartoon artist' }
// using the internal `print` function in "evaluate" delimiters
'<% print("hello " + user); %>!'

using the variable insert

{ 'user': 'pebbles', 'description' : 'comment here' }
// using the ES delimiter as an alternative to the default "interpolate" delimiter
'hello ${ user }!'

Quick start

Installation

In your project folder (base on fact that npm has been installed globaly)

$ npm install processhtmltemplate --save

Don't forget the --save parameter if you want that the package register to the package.json

Usage (in typescript)

normal usage with callback

"use strict";

import * as testProcHtmlTemp from './index';
import * as util from './lib/ErrorManagement/InbediaError';

let myTemplateHTML : string = '<!doctype html><h1>Congratulation those clients win a prize!</h1><html><% _.forEach(users, function(user) { %><li><%- user %></li><% }); %><p>Country of origin : <%= continent.country[1] %></p><h4>You can participate every week.</h4><hr/><p>This has been generated with processhtmltemplate a node module from inbedia.</p></html>';
let myTemplateDataUser : object = { 'users': ['Fred Flimstone', 'Barney Nockeles', "Bill Belechek", "Mat Groening"], continent : {country : ["Canada", "USA", "Mexico"]} };

testModuleTemplateHTMLCB();
testModuleTemplateHTMLPromise();
testModuleTemplateHTMLAsync();
testgenerateError();

// Standard With callback
function testModuleTemplateHTMLCB() : any {
  
    // Normal using callback    
    let testHTMLtemplateCB : any  =  new testProcDocTemp.InbediaModule.ProcessDocumentTemplate(myTemplateHTML, myTemplateDataUser);
    testHTMLtemplateCB.ProcessDocument((resString : string) => { 
        if (typeof resString === "string")  {
            console.info("SUCCESS - Result normal using callback")
            console.log("Merge result = " + resString);
        }
        else
            console.error("Error - has been generated no processing done..."); 
    
    });  
};

// Standard with Promise
function testModuleTemplateHTMLPromise() : void {

    // Normal with promise
    let testHTMLPromise : any =  new testProcDocTemp.InbediaModule.ProcessDocumentTemplate(myTemplateHTML, myTemplateDataUser);
    testHTMLPromise.ProcessDocument().then((resolve : string)  => { 
        console.info("SUCCES - standard use with Promise...");
        console.log ("Result merg = " + resolve)
    }).catch((reject)  => {
        console.error ("ERROR - no valide processing done...");
    });
};

// Standard with async
async function testModuleTemplateHTMLAsync() : Promise<void> {
    let testHTMLAsync : any = new testProcDocTemp.InbediaModule.ProcessDocumentTemplate(myTemplateHTML, myTemplateDataUser);
    let respProcess : any = await testHTMLAsync.ProcessDocument();
    if (respProcess !== null)  {
        console.info("SUCCES - SUCCES - standard use with asych...");
        console.log("Result of async function call = " + respProcess);
    }
    else
        console.error("ERROR - function testModuleTemplateHTMLAsync return NULL");
};

// Generated error and outputing result
function testgenerateError() : void  {
    // Error param not present in data object
    let templDataError = { 'widget': ['fred', 'barney', "Bill", "Mack"], continent : {country : ["Cnada", "USA", "Mexico"]} };;
    let testHTMLError = new testProcDocTemp.InbediaModule.ProcessDocumentTemplate(myTemplateHTML, templDataError);
    
     
    testHTMLError.ProcessDocument((resError : any)   =>  {
        console.info("ERROR generated - param not present in data object...");
        if (typeof resError === "object")   {
            console.log("General error = " + resError.Message);
            console.log("Technical error = " + resError.TechnicalError);
            console.log("Technical error message = " + resError.TechnicalMessage);
            console.log("Stack = " + resError.Stack);
            console.log("Error level = " + resError.ErrorLevel);
            console.log("Error level number = " + resError.ErrorLevelNum);
            console.log("Type of return = " + typeof resError);
        }
        else  {
            console.error("No error generated !");
        }
    }); 
    
};

Result in HTML

<!doctype html>
<html>
    <body>
        <h1>Congratulation those clients win a prize!</h1>
        <ul>
            <li>Fred Flimstone</li>
            <li>Barney Nockeles</li>
            <li>Bill Belechek</li>
            <li>Mat Groening</li>
        </ul>
        <p>Country of origin : USA</p>
        <h4>You can participate every week.</h4>
        <hr/>
        <p>This has been generated with processhtmltemplate a node module from inbedia.</p>
    </body>
</html>

Result in the browser

Congratulation those clients win a prize!

  • Fred Flimstone
  • Barney Nockeles
  • Bill Belechek
  • Mat Groening

Country of origin : USA

You can participate every week.


This has been generated with processhtmltemplate a node module from inbedia.


Hint :

You can use the error management you want in casting the error result in the type you want.

Contribution guidelines

  • Inbedia

Who do I talk to?

[email protected]