MagicMirror icon indicating copy to clipboard operation
MagicMirror copied to clipboard

incorporate remote njk template file configuration for weather

Open Kayakbabe opened this issue 3 years ago • 7 comments

I want to do a pull request so I can modify the weather module. By changing it slightly, it will not affect users if they update, they will not have to edit their configs.. existing configs and future updates to MM will continue to work with my modification.

Now that templating is a real thing for MagicMirror, I think that having a config variable for a custom weather template would make updating less stressful and easier for those who have modified their weather. Custom templates can have different names than the default current.njk and forcast.njk and could even be placed in the config folder if a path is given for the template file.

To that end I have modified my local weather module instance as follows.

In the weather module weather.js defaults section I added

defaults: {
	remoteTemplate: "current.njk",

and I altered the getTemplate function to the following:

// Select the template depending on the display type.
getTemplate: function () {
	
	if (this.config.remoteTemplate !="") {return this.config.remoteTemplate}
	 else {
	
		switch (this.config.type.toLowerCase()) {
			case "current":
				return "current.njk";
			case "hourly":
				return "hourly.njk";
			case "daily":
			case "forecast":
				return "forecast.njk";
			//Make the invalid values use the "Loading..." from forecast
			default:
				return "forecast.njk";
		}
   }
},

If the config value is not placed in config.js, the module will not fail, it will get the template as it does without the modification.

However, this modification allows using a custom template that will not be overwritten by updates.

Benefit of this mod is being able to change the css and html layout without having to create a new module. A user can put their own template in the same folder as current.njk and forecast.njk or as I did they can use a relative or absolute path in the config.

my usage of this mode. I created mycurrent.njk and myforcast.njk and put them in the mm config folder.

Then, in the weather section of my config.js I added the relative path to my custom template file.

config: {
	remoteTemplate: "../../../../config/kellyforecast.njk",

MagicMirror² Version: 2.20.0

Kayakbabe avatar Sep 11 '22 14:09 Kayakbabe

Hi @Kayakbabe this sounds like an interesting and helpful thing. Do you have the PR already in progress or need some help?

rejas avatar Sep 14 '22 10:09 rejas

by the way: is that a duplicate of https://github.com/MichMich/MagicMirror/issues/2880 ? can one be closed

rejas avatar Sep 17 '22 18:09 rejas

Seems like it would be more reasonable to be able to override any template from any module than to make it for this specific case. Maybe by having the module loader check in a folder for overrides before trying the bundled one?

Could also argue that this could be done when running the docker image with a -v custom_current.njk:/opt/magic_mirror/modules/default/weather/current.njk

buxxi avatar Sep 20 '22 17:09 buxxi

Yes, in the long run this might be interesting for other modules as well.

But I would propose to first do it in this here since @Kayakbabe seems to be invested in this module. That would not bloat the scope of a potential PR and allow to see how it might be forged into a more general approach later.

rejas avatar Sep 20 '22 18:09 rejas

to be honest, I forgot this issue but meanwhile I implemented https://gitlab.com/khassel/MMM-WeatherBridge because I do not like the default weather styling.

@rejas if you find some spare time you can take a look, maybe we can move parts of it into the default weather module.

khassel avatar Jun 24 '24 22:06 khassel