Skip to content

Commit b81f988

Browse files
JHWelchveeck
andauthored
Add template methods to "Subclassable module methods" (#326)
* Add template methods to "Subclassable module methods" * Add example * Remove extra header * cleanup "Should return" formatting --------- Co-authored-by: veeck <[email protected]>
1 parent 00b3e86 commit b81f988

File tree

1 file changed

+57
-3
lines changed

1 file changed

+57
-3
lines changed

module-development/core-module-file.md

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ start: function() {
121121

122122
### `getScripts()`
123123

124-
**Should return: Array**
124+
**Should return:** Array
125125

126126
The getScripts method is called to request any additional scripts that need to
127127
be loaded. This method should therefore return an array with strings. If you
@@ -148,7 +148,7 @@ Therefore, it's advised not to use any external urls.
148148

149149
### `getStyles()`
150150

151-
**Should return: Array**
151+
**Should return:** Array
152152

153153
The getStyles method is called to request any additional stylesheets that need
154154
to be loaded. This method should therefore return an array with strings. If you
@@ -175,7 +175,7 @@ Therefore, it's advised not to use any external URLs.
175175

176176
### `getTranslations()`
177177

178-
**Should return: Dictionary**
178+
**Should return:** Dictionary
179179

180180
The getTranslations method is called to request translation files that need to
181181
be loaded. This method should therefore return a dictionary with the files to
@@ -216,6 +216,60 @@ getDom: function() {
216216

217217
```
218218

219+
### `getTemplate()`
220+
221+
**Should return:** String
222+
223+
Alternatively to using `getDom`, you may provide the path to a
224+
[Nunjucks template](https://mozilla.github.io/nunjucks/templating.html).
225+
MagicMirror will use this template to render your component. You may provide
226+
data to the template with `getTemplateData`.
227+
228+
An example of a default module that uses this method is
229+
[newsfeed](https://github.com/MagicMirrorOrg/MagicMirror/blob/master/modules/default/newsfeed/newsfeed.js).
230+
231+
**Example:**
232+
233+
```js
234+
getTemplate: function() {
235+
return 'MMM-Example.njk';
236+
}
237+
238+
```
239+
240+
**Example Template:** `./MMM-Example.njk`
241+
242+
```nunjucks
243+
<div>
244+
<header>
245+
{{ "INFO" | translate }}
246+
</header>
247+
248+
<p class="hello">
249+
{{ prompt }}
250+
</p>
251+
</div>
252+
253+
```
254+
255+
### `getTemplateData`
256+
257+
**Should return:** Object
258+
259+
Used in conjunction with `getTemplate`. The data passed to the Nunjucks template
260+
for use in the component.
261+
262+
**Example:**
263+
264+
```js
265+
getTemplateData: function() {
266+
return {
267+
prompt: this.data.prompt;
268+
};
269+
}
270+
271+
```
272+
219273
### `getHeader()`
220274

221275
**Should return:** String

0 commit comments

Comments
 (0)