UNPKG

content-generator

Version:

Easy template generator to manage and save templates by key and languagecode.

212 lines (145 loc) 4.3 kB
# Content-Generator Content-Generator is a module to handle different contents in a mongo-db. ### Connection to MongoDB This module use the [mongodb-handler](https://www.npmjs.com/package/mongodb-handler) as sub-modul. To connect mongo-db please set follow ENV variables: - MDBHANDLER_CONSTRING: Defines the connectionstring to the database. Default is: 'mongodb://localhost:27017/mdbtest'; The module will store and handle all templates in collection ```CM_Templates```. You can define your own Collection-Name with follow ENV Variable: - CTM_COLLECTION: Defines the name of the collection ### Template Engine This module use the great template engine where powered by mozilla. You find the API Description [here](https://mozilla.github.io/nunjucks/templating.html) ### PDF generator - HTML_PDF_OPTIONS: Defines the options for HTML-PDF ### Usage Templatemanagement #### Global Properties This Template Manager is based in combination with the variables ```key``` and ```lang```. All variables free to declare as string. - key: Defines the global key of a template - lang: Defines the language in combination to the key properties. #### Create a template To Create a new template use the function ```createTemplate```: ``` const ctg = require('content-generator'); const obj = { html: '<div>Test</div>', lang: 'en_GB', subject: 'Test', key: 'TESTTEMPLATE' }; ctg.createTemplate(obj, (err) => { if (err) { throw err; } }); ``` #### Update a template To update a template use the funciton ```updateTemplate```: ``` const ctg = require('content-generator'); const obj = { html: '<div>Test</div>', lang: 'en_GB', subject: 'Test', key: 'TESTTEMPLATE' }; ctg.updateTemplate(obj, (err) => { if (err) { throw err; } }); ``` #### Delete a template To delete a template use the function ```deleteTemplate```: ``` const ctg = require('content-generator'); const obj = { lang: 'en_GB', key: 'TESTTEMPLATE' }; ctg.deleteTemplate(obj, (err) => { if (err) { throw err; } }); ``` #### Get a template To get a template use the function ```getTemplate```: ``` const ctg = require('content-generator'); const obj = { lang: 'en_GB', key: 'TESTTEMPLATE' }; ctg.getTemplate(obj, (err, template) => { if (err) { throw err; } console.log(template); }); ``` #### Get a Templateslist by key To get a templatelist by key use the function ```getTemplatesByKey```: ``` const ctg = require('content-generator'); ctg.getTemplate('TESTTEMPLATE', (err, templates) => { if (err) { throw err; } console.log(templates); }); ``` #### Get all templates from system To get all templates from the system use the function ```getTemplatesList```: ``` const ctg = require('content-generator'); ctg.getTemplatesList((err, templates) => { if (err) { throw err; } console.log(templates); }); ``` ### Usage Content Generator #### Generate a html Content This module will merge the variables from a JSON Object into the html-content: JSON: ``` { name: { firstname: "Martin", lastname: "Wiesmüller" }, address: { line1: "Werbas Innotec GmbH", line2: "Max-Eyth-Str. 42, 71088 Holzgerlingen" } } ``` HTML: ``` <div>Name: {{ name.firstname }}</div> <div>Lastname: {{ name.lastname }}</div> <br> <div>Adressline 1: {{ address.line1 }}</div> <div>Adressline 2: {{ address.line2 }}</div> ``` TO => ``` <div>Name: Martin</div> <div>Lastname: Wiesmüller</div> <br> <div>Adressline 1: Werbas Innotec GmbH"</div> <div>Adressline 2: Max-Eyth-Str. 42, 71088 Holzgerlingen</div> ``` Usage in Syntax: ``` const ctg = require('content-generator'); ctg.generateHtml(json, 'TESTTEMPLATE', 'en_GB', (err, res) => { if (err) { throw err; } // Output the html Content console.log(res.html); // Output the content as plain text console.log(res.plain); }); ``` #### Generate a PDF stream To generate a PDF Stream use the function ```generatePdf```: ``` const ctg = require('content-generator'); ctg.generatePdf(json, 'TESTTEMPLATE', 'en_GB', (err, res) => { if (err) { throw err; } // Output the html Content console.log(res.html); // Output the content as plain text console.log(res.plain); // Output the content as pdf stream res.stream.pipe(fs.createWriteStream('./foo.pdf')); }); ```