Skip to content

Feature Request: Extend the on_page_context event with the reference to the Jinja2 Environment #3709

Open

Description

@kamilkrzyskow

For complete context you can read more on squidfunk/mkdocs-material#7175

The blog plugin from mkdocs-material supports multiple instances:
https://squidfunk.github.io/mkdocs-material/plugins/#multiple-instances

However, it adds a date Jinja2 filter function to the Environment in the on_env event, that converts the datetime object to a string given a format: https://squidfunk.github.io/mkdocs-material/plugins/blog/#config.post_date_format
As every blog instance runs the same event, only the date filter from the last instance will be used in the global environment.

I've proposed different changes in the linked Issue, basically proposing that the Post (Page-like) object should have a reference to the Blog instance it was created with. This would allow to pass the whole page object to the "date" filter and the function could be then instance aware and use the post_date_format from the correct instance.

squidfunk wants to abide to proper plugin-development standards, so before hacking a solution together he wanted me to report it here first. Personally, I don't see an issue if he decided to put a self.env reference in the plugin in the on_env and later use it in on_page_context, but I also agree it would be nice for it to be the MkDocs-approved approach 😉

Activity

  1. pawamoy commented on May 20, 2024

    @pawamoy
    Contributor

    Filters can be decorated with pass_context (and also pass_environment, but the former is enough). So if you attach the blog instance to each post, the date (meta) filter can use it to access the right date formatting function.

    post.published_at | date
    @on_context
    def do_date(context, datetime):
        return context["post"].blog.format_date(datetime)

    Something like that :)

    But it still feels a bit like a workaround. on_env and multiple plugin instances do not play well together indeed.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions