-
-
Couldn't load subscription status.
- Fork 141
Add "Rendering Component" page #333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
KristjanESPERANTO
merged 4 commits into
MagicMirrorOrg:master
from
JHWelch:add-rendering-page
Oct 25, 2025
Merged
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| # Rendering Component | ||
|
|
||
| There are two main approaches to rendering your component. Procedurally | ||
| generating a Dom object with `getDom`, or using the built in Nunjucks templating | ||
| engine. | ||
|
|
||
| With both approaches, rendering is first triggered when the module is first | ||
| loaded. When your module's data changes, you can call | ||
| [`this.updateDom()`](/module-development/core-module-file.md#thisupdatedomsspeed) | ||
| to trigger a re-render. | ||
|
|
||
| ## Using `getDom` | ||
|
|
||
| The `getDom` method is called by MagicMirror whenever it needs to update | ||
| information on the screen. This method should return an HTML | ||
| [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) which | ||
| contains all the information you would like to display. | ||
|
|
||
| **Example:** | ||
|
|
||
| ```js | ||
| getDom: function() { | ||
| const wrapper = document.createElement("div"); | ||
| wrapper.innerHTML = 'Hello world!'; | ||
| return wrapper; | ||
| } | ||
| ``` | ||
|
|
||
| ## Using Nunjucks Templates | ||
|
|
||
| If `getDom` is not overridden, MagicMirror will try and render a | ||
| [Nunjucks](https://mozilla.github.io/nunjucks/) template from `getTemplate`. | ||
|
|
||
| Nunjucks is a templating language for Javascript. You can read more about the | ||
| syntax in the | ||
| [Nunjucks Documentation](https://mozilla.github.io/nunjucks/templating.html). | ||
|
|
||
| ### Templates and Data | ||
|
|
||
| Templates are rendered by calling `getTemplate` to get the path to the template | ||
| and `getTemplateData` to get the data used in the template. | ||
|
|
||
| **Example:** | ||
|
|
||
| `MMM-MyModule.js` | ||
|
|
||
| ```js | ||
| getTemplate: function() { | ||
| return "MMM-MyModule.njk"; | ||
| }, | ||
|
|
||
| getTemplateData: function() { | ||
| return { | ||
| list: ["item 1", "item 2", "item 3"], | ||
| }; | ||
| } | ||
| ``` | ||
|
|
||
| `MMM-MyModule.njk` | ||
|
|
||
| ```nunjucks | ||
| <ul> | ||
| {% for item in list %} | ||
| <li>{{ item }}</li> | ||
| {% endfor %} | ||
| </ul> | ||
| ``` | ||
|
|
||
| Rendered HTML: | ||
|
|
||
| ```html | ||
| <ul> | ||
| <li>item 1</li> | ||
| <li>item 2</li> | ||
| <li>item 3</li> | ||
| </ul> | ||
| ``` | ||
|
|
||
| ### Filters | ||
|
|
||
| [Nunjucks Filters](https://mozilla.github.io/nunjucks/templating.html#filters) | ||
| are functions added to Nunjucks that can be applied to variables passed to the | ||
| template. | ||
|
|
||
| #### `translate` filter | ||
|
|
||
| MagicMirror provides a `translate` filter which can be used to access the same | ||
| functionality available in the | ||
| [`this.translate` module instance method](/module-development/core-module-file.md#thistranslateidentifier). | ||
|
|
||
| ```nunjucks | ||
| {{ "INFO" | translate }} | ||
| ``` | ||
|
|
||
| #### Adding filters | ||
|
|
||
| You can add your own filters by accessing the Nunjucks environment via | ||
| `this.nunjucksEnvironment()` | ||
|
|
||
| ```js | ||
| this.nunjucksEnvironment().addFilter("space", function (str) { | ||
| return str.split("").join(" "); | ||
| }); | ||
| ``` | ||
|
|
||
| For filter examples see the | ||
| [`weather` module](https://github.com/MagicMirrorOrg/MagicMirror/blob/master/modules/default/weather/weather.js#L221). | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Javascript -> JavaScript