Skip to content

Add a loader to enable jinja imports#88624

Closed
depoll wants to merge 1 commit into
home-assistant:devfrom
depoll:jinja-import
Closed

Add a loader to enable jinja imports#88624
depoll wants to merge 1 commit into
home-assistant:devfrom
depoll:jinja-import

Conversation

@depoll
Copy link
Copy Markdown
Contributor

@depoll depoll commented Feb 22, 2023

Breaking change

Proposed change

Breaking out part of #88575

Enables importing/including shared jinja code/macros from a custom_jinja folder in your config folder.

All of this enables the following code:

In config/custom_jinja/shared.jinja:

{% macro some_macro() -%}
A macro
{%- endmacro %}
{%- set some_constant = "A constant" %}

Elsewhere:

{%- import "shared.jinja" as shared %}
{{ shared.some_constant }}
{{ shared.some_macro() }}

With the following output:

A constant
A macro

I'm happy to update the documentation as well but wanted to first clear that this would be a welcome change.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

@depoll depoll requested a review from a team as a code owner February 22, 2023 18:36
@home-assistant home-assistant Bot added cla-signed core new-feature small-pr PRs with less than 30 lines. labels Feb 22, 2023
@depoll depoll mentioned this pull request Feb 22, 2023
19 tasks
@MartinHjelmare MartinHjelmare changed the title Adds a loader to enable jinja imports Add a loader to enable jinja imports Feb 22, 2023
@balloob
Copy link
Copy Markdown
Member

balloob commented Feb 22, 2023

Duplicate of #85665

@balloob balloob marked this as a duplicate of #85665 Feb 22, 2023
@balloob balloob closed this Feb 22, 2023
@depoll
Copy link
Copy Markdown
Contributor Author

depoll commented Feb 22, 2023

@balloob Thanks for the pointers. Quick question: if the issue is primarily I/O in the event loop, would it be ok to preload these files (in the given directory) into memory at startup or when a service is called to reload (like with automations) so that import never does I/O? Then we would use DictLoader in the template environment so that imports never go to disk.

@balloob
Copy link
Copy Markdown
Member

balloob commented Feb 23, 2023

Yeah, I think that could work. Let me give it some thoughts on how we would want this to be exposed to users.

@depoll
Copy link
Copy Markdown
Contributor Author

depoll commented Feb 23, 2023

Awesome. I have some prototype code (just wanted to see what it would take to load everything into memory -- very straightforward) updated in my branch that does this, though I haven't hooked it into any lifecycle yet (just happens once at startup at the moment). Happy to hack on it if you have suggestions for the shape it should take.

@github-actions github-actions Bot locked and limited conversation to collaborators Feb 24, 2023
@home-assistant home-assistant unlocked this conversation Feb 25, 2023
@github-actions github-actions Bot locked and limited conversation to collaborators Feb 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants