Comparing version 2.8.1 to 2.9.0
@@ -788,2 +788,29 @@ --- | ||
{% raw %} {%- httpImg brand.logos.default -%} {% endraw %} // data:image/png;base64,77+9UE5HDQoaCgAAAA1JSERSAAA... | ||
``` | ||
### Supported filters | ||
* ## keys | ||
Given an Object converts it to an array of keys. | ||
For this object: | ||
```json | ||
{ | ||
"prop1": "value1", | ||
"prop2": "value2" | ||
} | ||
``` | ||
You can iterate over the object keys | ||
```liquid | ||
{% raw %} | ||
{%- assign objectKeys = brand.logos.default -%} | ||
{%- for aKey in objectKeys -%} | ||
Key number {%forloop.index%} : {{aKey}} | ||
{%- endfor -%} | ||
{% endraw %} | ||
//"Key number 1: prop1" | ||
//"Key number 2: prop2" | ||
``` |
{ | ||
"name": "btrz-pdf", | ||
"version": "2.8.1", | ||
"version": "2.9.0", | ||
"description": "Generates pdf documents based on a liquid template", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -16,2 +16,3 @@ const {Liquid} = require("liquidjs"); | ||
const {HttpImg} = require("./httpimg.js"); | ||
const {Keys} = require("./object.js"); | ||
const pdfjs = require("pdfjs"); | ||
@@ -118,4 +119,4 @@ | ||
engine.plugin(HttpImg); | ||
engine.plugin(Keys); | ||
const str = await engine.parseAndRender(liquidTemplate, data); | ||
try { | ||
@@ -122,0 +123,0 @@ const obj = JSON.parse(str); |
@@ -655,2 +655,32 @@ describe("index.js", () => { | ||
}); | ||
it("should allow to loop over an object keys", async () => { | ||
const pdf = require("../src/index"); | ||
const template = `{ | ||
"content": [ | ||
{% assign objectKeys = ticket.displayCurrency | keys %} | ||
{% for aKey in objectKeys %} | ||
{% if forloop.first %} | ||
{ | ||
{% endif %} | ||
{% unless forloop.last %} | ||
"key{{forloop.index}}": "{{aKey}}", | ||
{% else %} | ||
"key{{forloop.index}}": "{{aKey}}"} | ||
{% endunless %} | ||
{% endfor %} | ||
] | ||
}` | ||
const documentDefinition = await pdf.toDocumentDefinition(template, data); | ||
expect(documentDefinition).to.be.eql({ | ||
"content": [ | ||
{ | ||
"key1": "isocode", | ||
"key2": "symbol", | ||
"key3": "buy", | ||
"key4": "sell", | ||
} | ||
] | ||
}); | ||
}); | ||
}); |
417852
47
10067