|
| 1 | +# Rendering Component |
| 2 | + |
| 3 | +There are two main approaches to rendering your component. Procedurally |
| 4 | +generating a Dom object with `getDom`, or using the built in Nunjucks templating |
| 5 | +engine. |
| 6 | + |
| 7 | +With both approaches, rendering is first triggered when the module is first |
| 8 | +loaded. When your module's data changes, you can call |
| 9 | +[`this.updateDom()`](/module-development/core-module-file.md#thisupdatedomsspeed) |
| 10 | +to trigger a re-render. |
| 11 | + |
| 12 | +## Using `getDom` |
| 13 | + |
| 14 | +The `getDom` method is called by MagicMirror whenever it needs to update |
| 15 | +information on the screen. This method should return an HTML |
| 16 | +[Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) which |
| 17 | +contains all the information you would like to display. |
| 18 | + |
| 19 | +**Example:** |
| 20 | + |
| 21 | +```js |
| 22 | +getDom: function() { |
| 23 | + const wrapper = document.createElement("div"); |
| 24 | + wrapper.innerHTML = 'Hello world!'; |
| 25 | + return wrapper; |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +## Using Nunjucks Templates |
| 30 | + |
| 31 | +If `getDom` is not overridden, MagicMirror will try and render a |
| 32 | +[Nunjucks](https://mozilla.github.io/nunjucks/) template from `getTemplate`. |
| 33 | + |
| 34 | +Nunjucks is a templating language for JavaScript. You can read more about the |
| 35 | +syntax in the |
| 36 | +[Nunjucks Documentation](https://mozilla.github.io/nunjucks/templating.html). |
| 37 | + |
| 38 | +### Templates and Data |
| 39 | + |
| 40 | +Templates are rendered by calling `getTemplate` to get the path to the template |
| 41 | +and `getTemplateData` to get the data used in the template. |
| 42 | + |
| 43 | +**Example:** |
| 44 | + |
| 45 | +`MMM-MyModule.js` |
| 46 | + |
| 47 | +```js |
| 48 | +getTemplate: function() { |
| 49 | + return "MMM-MyModule.njk"; |
| 50 | +}, |
| 51 | + |
| 52 | +getTemplateData: function() { |
| 53 | + return { |
| 54 | + list: ["item 1", "item 2", "item 3"], |
| 55 | + }; |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +`MMM-MyModule.njk` |
| 60 | + |
| 61 | +```nunjucks |
| 62 | +<ul> |
| 63 | + {% for item in list %} |
| 64 | + <li>{{ item }}</li> |
| 65 | + {% endfor %} |
| 66 | +</ul> |
| 67 | +``` |
| 68 | + |
| 69 | +Rendered HTML: |
| 70 | + |
| 71 | +```html |
| 72 | +<ul> |
| 73 | + <li>item 1</li> |
| 74 | + <li>item 2</li> |
| 75 | + <li>item 3</li> |
| 76 | +</ul> |
| 77 | +``` |
| 78 | + |
| 79 | +### Filters |
| 80 | + |
| 81 | +[Nunjucks Filters](https://mozilla.github.io/nunjucks/templating.html#filters) |
| 82 | +are functions added to Nunjucks that can be applied to variables passed to the |
| 83 | +template. |
| 84 | + |
| 85 | +#### `translate` filter |
| 86 | + |
| 87 | +MagicMirror provides a `translate` filter which can be used to access the same |
| 88 | +functionality available in the |
| 89 | +[`this.translate` module instance method](/module-development/core-module-file.md#thistranslateidentifier). |
| 90 | + |
| 91 | +```nunjucks |
| 92 | +{{ "INFO" | translate }} |
| 93 | +``` |
| 94 | + |
| 95 | +#### Adding filters |
| 96 | + |
| 97 | +You can add your own filters by accessing the Nunjucks environment via |
| 98 | +`this.nunjucksEnvironment()` |
| 99 | + |
| 100 | +```js |
| 101 | +this.nunjucksEnvironment().addFilter("space", function (str) { |
| 102 | + return str.split("").join(" "); |
| 103 | +}); |
| 104 | +``` |
| 105 | + |
| 106 | +For filter examples see the |
| 107 | +[`weather` module](https://github.com/MagicMirrorOrg/MagicMirror/blob/master/modules/default/weather/weather.js#L221). |
0 commit comments