processhtmltemplate
v2.1.1
Published
process template HTML against data
Downloads
12
Maintainers
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