Skip to content
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

incorporate remote njk template file configuration for weather #2909

Open
Kayakbabe opened this issue Sep 11, 2022 · 7 comments
Open

incorporate remote njk template file configuration for weather #2909

Kayakbabe opened this issue Sep 11, 2022 · 7 comments

Comments

@Kayakbabe
Copy link

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

@rejas
Copy link
Collaborator

rejas commented Sep 14, 2022

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

@rejas
Copy link
Collaborator

rejas commented Sep 17, 2022

by the way: is that a duplicate of #2880 ? can one be closed

@buxxi
Copy link
Contributor

buxxi commented Sep 20, 2022

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

@rejas
Copy link
Collaborator

rejas commented Sep 20, 2022

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.

@khassel
Copy link
Collaborator

khassel commented Jun 24, 2024

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.

@brilliant0111
Copy link

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

@sdetweil
Copy link
Collaborator

i think a better approach is to add 3 variables for the template file names, defaulting as they are now

in the function that returns the hard coded name just return the appropriate template config property

then the user can override any/all of them, no custom code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants