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

Add THEME_TEMPLATE_OVERRIDES. Refs #2021 #2072

Merged
merged 1 commit into from Mar 5, 2018
Merged

Add THEME_TEMPLATE_OVERRIDES. Refs #2021 #2072

merged 1 commit into from Mar 5, 2018

Conversation

pedrohdz
Copy link
Contributor

Allow for overriding individual templates from the theme by configuring
the Jinja2 Environment loader to search for templates in the
THEME_TEMPLATE_OVERRIDES path before the theme's templates/
directory.

@pedrohdz pedrohdz changed the title Add THEME_TEMPLATE_OVERRIDES. Refs 2021 Add THEME_TEMPLATE_OVERRIDES. Refs 2021 (Take 2) Dec 25, 2016
@pedrohdz
Copy link
Contributor Author

Continuation of #2026. I slipped up and closed that PR accidentally. Ties in with getpelican/pelican-themes#466.

@avaris
Copy link
Member

avaris commented Dec 26, 2016

Doesn't this make EXTRA_TEMPLATES_PATHS obsolete?

Also, IMO, the docs for this should go to the Themes subsection, rather than Template pages.

@pedrohdz
Copy link
Contributor Author

@avaris,

Doesn't this make EXTRA_TEMPLATES_PATHS obsolete?

Might deprecate EXTRA_TEMPLATES_PATHS. I did not want to eliminate, or modify EXTRA_TEMPLATES_PATHS since it might mess up existing users. Should we axe EXTRA_TEMPLATES_PATHS in this change or give people time to transition? Maybe there's an argument for keeping both?

Also, IMO, the docs for this should go to the Themes subsection, rather than Template pages.

Maybe? I figured since it is one replacing or enhancing EXTRA_TEMPLATES_PATHS it should be put in the same section. The TEMPLATE in `THEME_TEMPLATE_OVERRIDES was another reason for putting it in the Template pages section.

I'm amenable on either of these.

@avaris
Copy link
Member

avaris commented Dec 28, 2016

Deprecating EXTRA_TEMPLATES_PATHS here with a warning should be fine. It can be moved to the new THEME_TEMPLATE_OVERRIDES to be backwards compatible. Only issue I see is that, EXTRA_TEMPLATES_PATHS wasn't overriding the existing templates, whereas this one will. However, that wasn't a feature before, so I don't see any reason for people to have 'overriding' templates in EXTRA_TEMPLATES_PATHS since they won't do anything and will be ignored anyway.

The reason I say, doc should go to Themes section is the primary use of this setting. It's to modify an existing theme with adding or replacing templates. Template pages is more about TEMPLATE_PAGES setting which are one-off templates that produce single output. They are more like Page contents. EXTRA_TEMPLATES_PATHS was put on there, I think, because DIRECT_TEMPLATES was there [*]. Although, it's also misleading. It says "These templates need to use DIRECT_TEMPLATES setting.", which isn't exactly true. You should be able to use any template in that setting like other theme templates (e.g. custom template for a content using template metadata).

[*]: Why is DIRECT_TEMPLATES there? It's in a grey area. Not exactly a theme setting, at the same time not really a TEMPLATE_PAGES but similar enough. However, conventionally, it's tied to a theme more than it's tied to TEMPLATE_PAGES

@pedrohdz
Copy link
Contributor Author

Noted... I should be able to get to updating the PR next week...

Thanks!

@pedrohdz
Copy link
Contributor Author

pedrohdz commented Jan 9, 2017

All set @avaris.

@almet
Copy link
Member

almet commented Apr 21, 2017

This looks all good to me. I'm in favor of merging it. Any other thoughts?

@ssteinerx
Copy link

@almet Is the discussion of child themes separate from this, or does this PR obsolete that discussion? In my mind a simple override, while useful, is not the same as being to inherit from and extend a parent theme.

@pedrohdz
Copy link
Contributor Author

@ssteinerx,
I totally agree, template overrides is not the same as being to inherit and extend from an existing parent theme. Though THEME_TEMPLATE_OVERRIDES and inheritance are not mutually exclusive either. Some things to note:

  • This works as a stop gap until Inherit from arbitrary theme #1092 gets figured out. That request has dragged on for 3+ years and some of the original implementation details are being rehashed.
  • It fixes what I view as a hobbled EXTRA_TEMPLATES_PATHS while maintaining backwards compatibility. There might have been a reason to implement EXTRA_TEMPLATES_PATHS the way it was, but I question it. (I could be wrong here :-) )
  • Moves us away from having to fork themes and have to deal with merging.
  • Even if we implement Inherit from arbitrary theme #1092 tomorrow, it still feels like overkill when all that it is needed is replacing a template file or two.
  • THEME_TEMPLATE_OVERRIDES and inheritance can coexist, when Inherit from arbitrary theme #1092 is implemented. If anything THEME_TEMPLATE_OVERRIDES can leverage inheritance.

THEME_TEMPLATE_OVERRIDES was inspired by how Hugo theme customization works. I tested out Hugo when I was experimenting with various static site generators. Hugo came a close second, but Jinja templating swayed me towards Pelican.

@ssteinerx
Copy link

@digitalrounin I agree, and vote to merge this to allow overriding parts of a template completely while we workout/bike shed #1092.

@almet
Copy link
Member

almet commented Apr 22, 2017

Lets summon @avaris on this one :-) any feedback ?

Copy link
Member

@avaris avaris left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry. I thought I had approved this. Apparently I forgot. Good to go.

Copy link
Contributor

@ingwinlu ingwinlu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have several issues with this pr as shown in the review comments.

Additionally:

  • Why the effort of introducing a new setting instead of just moving the existing EXTRA_TEMPLATES_PATHS to where you now introduced _OVERRIDES?
  • You deprecate EXTRA_TEMPLATES_PATHS but not really because you don't do anything with it. Either move stuff from the old setting to the new one or don't deprecate.

def test_theme_overrides(self):
"""
Test that the THEME_TEMPLATE_OVERRIDES configuration setting is
being utilized correctly the Generator.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utilized correctly in the Generator

A list of paths you want Jinja2 to search for templates. Can be used to
separate templates from the theme. Example: projects, resume, profile ...
These templates need to use ``DIRECT_TEMPLATES`` setting.
``EXTRA_TEMPLATES_PATHS`` is being depricated, use
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there no sphinx/rst directive to better emphasize deprecation?

@@ -52,6 +52,7 @@ def __init__(self, context, settings, path, theme, output_path,
# templates cache
self._templates = {}
self._templates_path = []
self._templates_path += self.settings['THEME_TEMPLATE_OVERRIDES']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self._templates_path = []
self._templates_path += self.settings['THEME_TEMPLATE_OVERRIDES']

is equal to

self._templates_path = self.settings['THEME_TEMPLATE_OVERRIDES']

@@ -97,6 +97,7 @@ def load_source(name, path):
'RELATIVE_URLS': False,
'DEFAULT_LANG': 'en',
'DIRECT_TEMPLATES': ['index', 'tags', 'categories', 'authors', 'archives'],
'THEME_TEMPLATE_OVERRIDES': [],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we use theme_templates internally, might want to change it for consistency

Copy link
Member

@avaris avaris left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do have to un-approve this (thanks to @ingwinlu for pointing out).

I don't like the way deprecation handled here.

Either the old setting is gone and replaced with the new one:

  • remove EXTRA_TEMPLATES_PATHS from the docs.
  • show a warning if the old one is present in the settings and move the values to the new setting.
  • it won't be used anymore (i.e. no more self._templates_path += self.settings['EXTRA_TEMPLATES_PATHS']).

or we reuse the old setting with new functionality (as @ingwinlu suggested)

@ssteinerx
Copy link

My reading is that EXTRA_TEMPLATES_PATHS files have to be used by DIRECT_TEMPLATES while THEME_TEMPLATE_OVERRIDES is intended to allow overriding any template with that from another non-theme directory.

I find this really confusing but I also never used EXTRA_TEMPLATES_PATHS because I didn't really get the point of it.

I thought the point of this PR was to add a setting to allow overriding templates not used in DIRECT_TEMPLATES and, so, is a separate setting.

@avaris
Copy link
Member

avaris commented Apr 24, 2017

Current doc for EXTRA_TEMPLATES_PATHS is a bit misleading. Templates from EXTRA_TEMPLATES_PATHS can be treated like any other template. You can use it with DIRECT_TEMPLATES, as the docs suggest, but you can also use them for custom templates in Articles or Pages with template metadata.

The only difference between EXTRA_TEMPLATES_PATHS and THEME_TEMPLATE_OVERRIDES (as proposed in this PR) is the position in the lookup chain. EXTRA_TEMPLATES_PATHS was after the theme, so any conflicting templates were resolved from the theme (i.e. no way to override). THEME_TEMPLATE_OVERRIDES comes before the theme, so it has priority in case of conflict.

@ssteinerx
Copy link

If I'm reading this right, you can set the theme, then use EXTRA_TEMPLATES_PATHS instead of the the proposed PR and it's just a documentation issue.

@avaris
Copy link
Member

avaris commented Apr 24, 2017

No... EXTRA_TEMPLATES_PATH can't override theme templates (which is the whole purpose of this PR). Current lookup chain is like this:

  • templates coming from the THEME
  • EXTRA_TEMPLATES_PATHS
  • simple built-in theme

Jinja will look through these in this order and return the first matching template it finds. So, with EXTRA_TEMPLATES_PATHS you can't provide your alternate article.html template. Because the one in theme will be found earlier and used always.

This PR adds the new setting to the top of the chain, so if the template that's being searched is there, it'll be used (instead of the one from the theme).

That being said, there is also a documentation issue with EXTRA_TEMPLATES_PATHS currently. That setting does more than what is documented. It's not limited to DIRECT_TEMPLATES. It's basically an alternate template source for all template related functionality.

@avaris
Copy link
Member

avaris commented Apr 24, 2017

By the way, it might be worth to include the current theme as a PrefixLoader (similar to simple, say with !theme prefix) with this PR. Especially for the small variations of the override. Because with the way the PR is right now, you pretty much need to write the override from scratch (you can't access the theme template because it's being overridden). If the prefixed one is added, you can use the theme version as a base and override relevant blocks:

{% extends '!theme/article.html' %}
{% block someblock %}
My override
{% endblock %}

@ssteinerx
Copy link

@avaris So, if you put your parent theme in EXTRA_TEMPLATES_PATHS, and your "child template" as the regular theme, you accomplish the same thing with no code changes?

@avaris
Copy link
Member

avaris commented Apr 24, 2017

A theme is more than just the templates. You'd need to duplicate the static part (CSS, JS, etc) of the "parent theme" in your "child theme" which is no better than copying the theme, modifying whatever you want and using it as THEME.

@ssteinerx
Copy link

@avaris Yes, I suspected that the other chunks 'wouldn't come along for the ride.' Since I hadn't looked at EXTRA_TEMPLATES_PATHS carefully due to the documented limitation of its use to direct templates I was hoping that this behaviour might have been overlooked, too 😉.

@ssteinerx
Copy link

ssteinerx commented Apr 24, 2017

I wonder how traumatic it would be to just add inheritance of the CSS/JS etc. from EXTRA_TEMPLATES_PATHS and updating the documentation to reflect the previous undocumented behaviour and the new behaviour at the same time.

My guess is that EXTRA_TEMPLATES_PATHS was probably used very little due to its documented restriction and, since the theme will have its own CSS and JS for anyone using themes in the regular way, this shouldn't cause any problems at all.

I also agree that adding the PrefixLoader ability would make it hard to really come up with more to add for true "child themes."

@avaris
Copy link
Member

avaris commented Apr 24, 2017

Real theme inheritance is required if the child theme contains its own static content (e.g. customized main CSS). It could still be possible with certain settings config, but would certainly be "hacky". For template-only modifications, this PR should be sufficient.

@pedrohdz
Copy link
Contributor Author

@ingwinlu - Good stuff... Keep it coming... I just submitted a new round of changes. Let me know if I missed, or misinterpreted, anything.

@avaris - Good idea on !theme. I implemented it. Please check it out. I'm stumped on how to implement unit tests for it though. Any ideas?

@ssteinerx - Actually, anyone that wants to add custom pages via DIRECT_TEMPLATES would likely be utilizing EXTRA_TEMPLATES_PATHS.

If someone whats to go in and add their own static content (images, JS, CSS) STATIC_PATHS should be used and then update the corresponding template file, typically through forking a theme, but soon via THEME_TEMPLATES_OVERRIDES as well. There's also CUSTOM_CSS depending on the theme you are using.


In my mind, Pelican users can wear one of two hats:

  1. Content Creator - Blog writers and whatnot.
  2. Theme Maintainer/Creator - Name says it all.

A person can where one, or both. I view settings such as THEME_TEMPLATES_OVERRIDES, STATIC_PATHS, etc. as way of allowing Content Creators to modify the look and feel of their site without being forced into becoming Theme Maintainers. Avoiding the whole theme forking thing.

THEME_TEMPLATES_OVERRIDES is not meant to be used theme inheritance.

@almet
Copy link
Member

almet commented Apr 25, 2017

I love where this is going. Simplicity matters :-)

@pedrohdz
Copy link
Contributor Author

@almet, @ingwinlu, @avaris, @ssteinerx,
Any chance of wrapping this up? Anything else need changing?

@avaris
Copy link
Member

avaris commented Jun 11, 2017

@digitalrounin for testing it, you can create an override, create appropriate settings, create a generator and use its get_template for your override, then render it and check.

Allow for overriding individual templates from the theme by configuring
the Jinja2 `Environment` loader to search for templates in the
`THEME_TEMPLATES_OVERRIDES` path before the theme's `templates/`
directory.
@pedrohdz
Copy link
Contributor Author

Finally got some free time to work on this!

@avaris,
Tests for !theme prefix added. Added tests for !simple and bad prefixes as well.

@almet,
Any chance we can finally merge this? :-)

@justinmayer
Copy link
Member

Any last thoughts from @getpelican/reviewers? Your input would be greatly appreciated!

@astrojuanlu
Copy link

Any chance to move this forward?

@justinmayer
Copy link
Member

@avaris: Any chance you could take another look and see if anything else is required before merging?

Copy link
Member

@avaris avaris left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay. Looks good for me 👍

@justinmayer justinmayer added this to the 3.8.0 milestone Mar 5, 2018
@justinmayer justinmayer changed the title Add THEME_TEMPLATE_OVERRIDES. Refs 2021 (Take 2) Add THEME_TEMPLATE_OVERRIDES. Refs #2021 Mar 5, 2018
@justinmayer
Copy link
Member

Many thanks to @digitalrounin for the contribution and to everyone who helped review it!

@justinmayer justinmayer merged commit 72756a5 into getpelican:master Mar 5, 2018
@pedrohdz
Copy link
Contributor Author

pedrohdz commented Mar 5, 2018

Thanks Everyone!

@pedrohdz pedrohdz deleted the overrides_ref2021+merge_update branch April 2, 2018 09:39
@boxydog boxydog mentioned this pull request Oct 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants