Skip to content

Commit

Permalink
Added support for custom handlebar helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
navjotdhanawat committed Mar 15, 2019
1 parent 26be9ce commit 42fc76c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ npm install dynamic-html-pdf --save
<h1>Hi {{users.0.name}}</h1>
<div>
template
{{#ifCond 'v1' 'v2'}}
{{v1}} is equals to {{v2}}
{{else}}
Variables are not similar
{{/ifCond}}
</div>
</body>
</html>
Expand All @@ -35,13 +40,44 @@ For example:
</ul>
```

#### [Custom handlebar helpers](https://handlebarsjs.com/block_helpers.html)
For example:
Register helper inside js file:
```
// Custom If condition inside handlebar(JS file)
var pdf = require('dynamic-html-pdf');
pdf.registerHelper('ifCond', function (v1, v2, options) {
if (v1 === v2) {
return options.fn(this);
}
return options.inverse(this);
})
```

Utilize registered helper inside handlebar template:
```
{{#ifCond v1 v2}}
{{v1}} is equals to {{v2}}
{{else}}
Variables are not similar
{{/ifCond}}
```


#### How to use Dynamic HTML to PDF

```
var fs = require('fs');
var pdf = require('dynamic-html-pdf');
var html = fs.readFileSync('template.html', 'utf8');
// Custom handlebar helper
pdf.registerHelper('ifCond', function (v1, v2, options) {
if (v1 === v2) {
return options.fn(this);
}
return options.inverse(this);
})
var options = {
format: "A3",
Expand Down
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
var Handlebars = require('handlebars'),
pdf = require('html-pdf');

module.exports = {};
// Handlebar helper support
module.exports.registerHelper = (conditionName, callback) => {
Handlebars.registerHelper(conditionName, callback);
};

module.exports.create = (document, options) => {
// Compile handlebar template
return new Promise((resolve, reject) => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dynamic-html-pdf",
"version": "0.0.4",
"version": "1.0.0",
"description": "Create dynamic PDF from Handlebar template",
"main": "index.js",
"scripts": {},
Expand Down

0 comments on commit 42fc76c

Please sign in to comment.