|
1 | 1 | <!-- default badges list --> |
2 | | - |
3 | | -[](https://supportcenter.devexpress.com/ticket/details/T1129779) |
| 2 | + |
| 3 | +[](https://supportcenter.devexpress.com/ticket/details/T1162966) |
4 | 4 | [](https://docs.devexpress.com/GeneralInformation/403183) |
5 | 5 | [](#does-this-example-address-your-development-requirementsobjectives) |
6 | 6 | <!-- default badges end --> |
7 | | -# DevExtreme Examples Template |
| 7 | +# HtmlEditor for DevExtreme - How to implement mail merge with variables |
8 | 8 |
|
9 | | -This is the repository template for creating new examples. |
| 9 | +The HtmlEditor component supports [variables](https://js.devexpress.com/Documentation/ApiReference/UI_Components/dxHtmlEditor/Configuration/variables/). This example demonstrates how to implement mail merge with variables. |
10 | 10 |
|
11 | | - |
| 11 | +The application contains HtmlEditor and two TextBoxes. Type values in the TextBoxes to see them merged with HtmlEditor text. |
12 | 12 |
|
13 | | -Use **DevExtreme _Product_ - _Task_** template for a title. |
| 13 | + |
14 | 14 |
|
15 | | -Describe the solved task in this section. |
| 15 | +The core implementation for mail merge can be found within the `replaceVariables(value, variablesMap)` utility method. The method accepts two parameters: HtmlEditor's [value](https://js.devexpress.com/Documentation/ApiReference/UI_Components/dxHtmlEditor/Configuration/#value) and an object map. These parameters are used to parse the value, then to find and replace the variables based on the map object. |
16 | 16 |
|
17 | | -Put a screenshot/gif that illustrates the result here. |
| 17 | +``` |
| 18 | +const DX_VARIABLE_CLASS = 'dx-variable'; |
| 19 | +const DATA_VAR_VALUE_ATTR = 'data-var-value'; |
18 | 20 |
|
19 | | -Then, add implementation details (steps, code snippets, and other technical information in a free form), or add a link to an existing document with implementation details. |
| 21 | +/* |
| 22 | +value: dxHtmlEditor.value |
| 23 | +variableMap: object = Must contain `key: value` pairs of variable content. |
| 24 | + { |
| 25 | + 'FirstName': 'John', |
| 26 | + 'LastName': 'Smith' |
| 27 | + } |
| 28 | +*/ |
| 29 | +const replaceVariables = (value, variablesMap) => { |
| 30 | + const parser = new DOMParser(); |
| 31 | +
|
| 32 | + replaceVariables = (value, variablesMap) => { |
| 33 | + const doc = parser.parseFromString(value, 'text/html'); |
| 34 | + const variables = doc.querySelectorAll(`.${DX_VARIABLE_CLASS}`); |
| 35 | +
|
| 36 | + variables.forEach(variable => { |
| 37 | + const variableValue = variablesMap[variable.getAttribute(DATA_VAR_VALUE_ATTR)]; |
| 38 | + variable.outerHTML = variableValue; |
| 39 | + }); |
| 40 | +
|
| 41 | + return doc.body.innerHTML.toString(); |
| 42 | + } |
| 43 | +
|
| 44 | + return replaceVariables(value, variablesMap); |
| 45 | +}; |
| 46 | +``` |
20 | 47 |
|
21 | 48 | ## Files to Review |
22 | 49 |
|
| 50 | +- **jQuery** |
| 51 | + - [index.js](jQuery/src/index.js) |
23 | 52 | - **Angular** |
24 | 53 | - [app.component.html](Angular/src/app/app.component.html) |
25 | 54 | - [app.component.ts](Angular/src/app/app.component.ts) |
26 | | -- **React** |
27 | | - - [App.tsx](React/src/App.tsx) |
28 | 55 | - **Vue** |
29 | | - - [App.vue](Vue/src/App.vue) |
30 | 56 | - [Home.vue](Vue/src/components/HomeContent.vue) |
31 | | -- **jQuery** |
32 | | - - [index.html](jQuery/src/index.html) |
33 | | - - [index.js](jQuery/src/index.js) |
34 | | -- **ASP.NET Core** |
| 57 | +- **React** |
| 58 | + - [App.tsx](React/src/App.tsx) |
| 59 | +- **NetCore** |
35 | 60 | - [Index.cshtml](ASP.NET%20Core/Views/Home/Index.cshtml) |
36 | 61 |
|
37 | 62 | ## Documentation |
38 | 63 |
|
39 | | -- link |
40 | | -- link |
41 | | -- ... |
42 | | - |
43 | | -## More Examples |
44 | | - |
45 | | -- link |
46 | | -- link |
47 | | -- ... |
| 64 | +- [Getting Started with HtmlEditor](https://js.devexpress.com/Documentation/Guide/UI_Components/HtmlEditor/Getting_Started_with_HtmlEditor/) |
| 65 | +- [HtmlEditor - Variables](https://js.devexpress.com/Documentation/ApiReference/UI_Components/dxHtmlEditor/Configuration/variables/) |
48 | 66 | <!-- feedback --> |
49 | 67 | ## Does this example address your development requirements/objectives? |
50 | 68 |
|
51 | | -[<img src="https://www.devexpress.com/support/examples/i/yes-button.svg"/>](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=devextreme-examples-template&~~~was_helpful=yes) [<img src="https://www.devexpress.com/support/examples/i/no-button.svg"/>](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=devextreme-examples-template&~~~was_helpful=no) |
| 69 | +[<img src="https://www.devexpress.com/support/examples/i/yes-button.svg"/>](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=devextreme-htmleditor-mail-merge-with-variables&~~~was_helpful=yes) [<img src="https://www.devexpress.com/support/examples/i/no-button.svg"/>](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=devextreme-htmleditor-mail-merge-with-variables&~~~was_helpful=no) |
52 | 70 |
|
53 | 71 | (you will be redirected to DevExpress.com to submit your response) |
54 | 72 | <!-- feedback end --> |
0 commit comments