easy-template-x
Advanced tools
Comparing version 2.1.0 to 3.0.0
# Change Log | ||
## [3.0.0 - 2022-07-04](https://github.com/alonrbar/easy-template-x/tree/v3.0.0) | ||
### Added | ||
- **BREAKING** - Add support for nesting tags, loops and other conditions inside simple conditions (fixes [#49](https://github.com/alonrbar/easy-template-x/issues/49)). This requires changing the way some values are look up in the input data. Please see the readme file for details (https://github.com/alonrbar/easy-template-x#nested-conditions). | ||
## [2.1.0 - 2021-07-29](https://github.com/alonrbar/easy-template-x/tree/v2.1.0) | ||
@@ -4,0 +10,0 @@ |
@@ -11,2 +11,4 @@ import { ScopeData, Tag, TemplateContext } from '../../compilation'; | ||
private compile; | ||
private updatePathBefore; | ||
private updatePathAfter; | ||
} |
{ | ||
"name": "easy-template-x", | ||
"version": "2.1.0", | ||
"version": "3.0.0", | ||
"description": "Generate docx documents from templates, in Node or in the browser.", | ||
@@ -42,37 +42,37 @@ "keywords": [ | ||
"dependencies": { | ||
"jszip": "3.6.0", | ||
"jszip": "3.10.0", | ||
"lodash.get": "4.4.2", | ||
"xmldom": "0.5.0" | ||
"xmldom": "0.6.0" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "7.13.10", | ||
"@babel/plugin-proposal-class-properties": "7.13.0", | ||
"@babel/plugin-proposal-nullish-coalescing-operator": "7.13.8", | ||
"@babel/plugin-proposal-object-rest-spread": "7.13.8", | ||
"@babel/plugin-proposal-optional-catch-binding": "7.13.8", | ||
"@babel/plugin-proposal-optional-chaining": "7.13.8", | ||
"@babel/plugin-transform-modules-commonjs": "7.13.8", | ||
"@babel/preset-typescript": "7.13.0", | ||
"@rollup/plugin-replace": "2.4.1", | ||
"@types/jest": "26.0.20", | ||
"@types/jszip": "3.4.0", | ||
"@types/node": "14.14.35", | ||
"@babel/core": "7.18.6", | ||
"@babel/plugin-proposal-class-properties": "7.18.6", | ||
"@babel/plugin-proposal-nullish-coalescing-operator": "7.18.6", | ||
"@babel/plugin-proposal-object-rest-spread": "7.18.6", | ||
"@babel/plugin-proposal-optional-catch-binding": "7.18.6", | ||
"@babel/plugin-proposal-optional-chaining": "7.18.6", | ||
"@babel/plugin-transform-modules-commonjs": "7.18.6", | ||
"@babel/preset-typescript": "7.18.6", | ||
"@rollup/plugin-replace": "4.0.0", | ||
"@types/jest": "28.1.4", | ||
"@types/jszip": "3.4.1", | ||
"@types/node": "18.0.1", | ||
"@types/ts-nameof": "4.2.1", | ||
"@types/xmldom": "0.1.30", | ||
"@typescript-eslint/eslint-plugin": "4.18.0", | ||
"@typescript-eslint/parser": "4.18.0", | ||
"babel-jest": "26.6.3", | ||
"babel-loader": "8.2.2", | ||
"@types/xmldom": "0.1.31", | ||
"@typescript-eslint/eslint-plugin": "5.30.4", | ||
"@typescript-eslint/parser": "5.30.4", | ||
"babel-jest": "28.1.2", | ||
"babel-loader": "8.2.5", | ||
"babel-plugin-ts-nameof": "4.2.1", | ||
"eslint": "7.22.0", | ||
"jest": "26.6.3", | ||
"jest-junit": "12.0.0", | ||
"lorem-ipsum": "2.0.3", | ||
"eslint": "8.19.0", | ||
"jest": "28.1.2", | ||
"jest-junit": "14.0.0", | ||
"lorem-ipsum": "2.0.8", | ||
"rimraf": "3.0.2", | ||
"rollup": "2.41.4", | ||
"rollup": "2.75.7", | ||
"rollup-plugin-auto-external": "2.0.0", | ||
"rollup-plugin-babel": "4.3.3", | ||
"rollup-plugin-babel": "4.4.0", | ||
"rollup-plugin-node-resolve": "5.2.0", | ||
"typescript": "4.2.3" | ||
"typescript": "4.7.4" | ||
} | ||
} |
@@ -5,6 +5,5 @@ # easy-template-x | ||
[![CircleCI](https://circleci.com/gh/alonrbar/easy-template-x.svg?style=shield)](https://circleci.com/gh/alonrbar/easy-template-x) | ||
[![ci](https://github.com/alonrbar/easy-template-x/actions/workflows/ci.yaml/badge.svg)](https://github.com/alonrbar/easy-template-x/actions/workflows/ci.yaml) | ||
[![npm version](https://img.shields.io/npm/v/easy-template-x.svg)](https://www.npmjs.com/package/easy-template-x) | ||
[![npm license](https://img.shields.io/npm/l/easy-template-x.svg)](https://www.npmjs.com/package/easy-template-x) | ||
[![dependencies](https://david-dm.org/alonrbar/easy-template-x.svg)](https://github.com/alonrbar/easy-template-x) | ||
@@ -18,2 +17,3 @@ - [Node Example](#node-example) | ||
- [Conditions](#conditions) | ||
- [Nested Conditions](#nested-conditions) | ||
- [Image plugin](#image-plugin) | ||
@@ -191,3 +191,3 @@ - [Link plugin](#link-plugin) | ||
The example below shows two lines being rendered, each with different content depeneding on the truthy value. | ||
The example below shows two lines being rendered, each with different content depending on the truthy value. | ||
@@ -213,4 +213,47 @@ Input template: | ||
_For a more powerful conditional syntax see the [alternative syntax](#advanced-syntax-and-custom-resolvers) section._ | ||
#### Nested Conditions | ||
Nested conditions are also supported, so you can nest other tags including loop tags and even other conditions in them. When doing so remember to format your data accordingly. See the example below for clarification: | ||
Input template: | ||
![input template](./docs/assets/nested-conditions-in.png?raw=true) | ||
Input data: | ||
Notice how even though `name` and `members` are nested in the template under the `show` condition their values are adjacent to it in the input data. | ||
```javascript | ||
{ | ||
"teams": [ | ||
{ | ||
show: true, | ||
name: "A-Team", | ||
members: [ | ||
{ name: "Hannibal" }, | ||
{ name: "Face" }, | ||
{ name: "Murdock" }, | ||
{ name: "Baracus" }, | ||
] | ||
}, | ||
{ | ||
show: false, | ||
name: "B-Team", | ||
members: [ | ||
{ name: "Alice" }, | ||
{ name: "Bob" }, | ||
{ name: "Charlie" }, | ||
{ name: "Dave" }, | ||
] | ||
} | ||
], | ||
} | ||
``` | ||
Output document: | ||
![output document](./docs/assets/nested-conditions-out.png?raw=true) | ||
_If you are looking for a yet more powerful conditional syntax see the [alternative syntax](#advanced-syntax-and-custom-resolvers) section._ | ||
### Image plugin | ||
@@ -217,0 +260,0 @@ |
@@ -1,2 +0,2 @@ | ||
import { ScopeData, Tag, TemplateContext } from '../../compilation'; | ||
import { PathPart, ScopeData, Tag, TemplateContext } from '../../compilation'; | ||
import { TemplateData } from '../../templateData'; | ||
@@ -30,3 +30,4 @@ import { last } from '../../utils'; | ||
// Non array value - treat as a boolean condition. | ||
if (!Array.isArray(value)) { | ||
const isCondition = !Array.isArray(value); | ||
if (isCondition) { | ||
if (!!value) { | ||
@@ -58,3 +59,3 @@ value = [{}]; | ||
// search through the text again) | ||
const compiledNodes = await this.compile(repeatedNodes, data, context); | ||
const compiledNodes = await this.compile(isCondition, repeatedNodes, data, context); | ||
@@ -79,3 +80,3 @@ // merge back to the document | ||
private async compile(nodeGroups: XmlNode[][], data: ScopeData, context: TemplateContext): Promise<XmlNode[][]> { | ||
private async compile(isCondition: boolean, nodeGroups: XmlNode[][], data: ScopeData, context: TemplateContext): Promise<XmlNode[][]> { | ||
const compiledNodeGroups: XmlNode[][] = []; | ||
@@ -92,5 +93,5 @@ | ||
// compile the new root | ||
data.pathPush(i); | ||
const conditionTag = this.updatePathBefore(isCondition, data, i); | ||
await this.utilities.compiler.compile(dummyRootNode, data, context); | ||
data.pathPop(); | ||
this.updatePathAfter(isCondition, data, conditionTag); | ||
@@ -108,2 +109,30 @@ // disconnect from dummy root | ||
} | ||
private updatePathBefore(isCondition: boolean, data: ScopeData, groupIndex: number): PathPart { | ||
// if it's a condition - don't go deeper in the path | ||
// (so we need to extract the already pushed condition tag) | ||
if (isCondition) { | ||
if (groupIndex > 0) { | ||
// should never happen - conditions should have at most one (synthetic) child... | ||
throw new Error(`Internal error: Unexpected group index ${groupIndex} for boolean condition at path "${data.pathString()}".`); | ||
} | ||
return data.pathPop(); | ||
} | ||
// else, it's an array - push the current index | ||
data.pathPush(groupIndex); | ||
return null; | ||
} | ||
private updatePathAfter(isCondition: boolean, data: ScopeData, conditionTag: PathPart): void { | ||
// reverse the "before" path operation | ||
if (isCondition) { | ||
data.pathPush(conditionTag); | ||
} else { | ||
data.pathPop(); | ||
} | ||
} | ||
} | ||
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
394974
9317
557
+ Addedjszip@3.10.0(transitive)
+ Addedsetimmediate@1.0.5(transitive)
+ Addedxmldom@0.6.0(transitive)
- Removedjszip@3.6.0(transitive)
- Removedset-immediate-shim@1.0.1(transitive)
- Removedxmldom@0.5.0(transitive)
Updatedjszip@3.10.0
Updatedxmldom@0.6.0