Skip to content

Commit cde7572

Browse files
authored
Add "Rendering Component" page (#333)
1 parent 550a14a commit cde7572

File tree

3 files changed

+114
-1
lines changed

3 files changed

+114
-1
lines changed

.vuepress/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export default {
7070
"/module-development/introduction.md",
7171
"/module-development/core-module-file.md",
7272
"/module-development/node-helper.md",
73+
"/module-development/rendering.md",
7374
"/module-development/helper-methods.md",
7475
"/module-development/logger.md",
7576
"/module-development/notifications.md",
@@ -118,4 +119,3 @@ export default {
118119
}),
119120
],
120121
};
121-

module-development/core-module-file.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ starts, or because your module asked a refresh using `this.updateDom()`), the
205205
system calls the getDom method. This method should therefore return a dom
206206
object.
207207

208+
Read more about Rendering Components
209+
[in the Rendering Component documentation](./rendering.md).
210+
208211
**Example:**
209212

210213
```js
@@ -228,6 +231,9 @@ data to the template with `getTemplateData`.
228231
An example of a default module that uses this method is
229232
[newsfeed](https://github.com/MagicMirrorOrg/MagicMirror/blob/master/modules/default/newsfeed/newsfeed.js).
230233

234+
Read more about Rendering Components
235+
[in the Rendering Component documentation](./rendering.md).
236+
231237
**Example:**
232238

233239
```js

module-development/rendering.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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

Comments
 (0)