question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Template cache, {% extends %} and globals - design issue?

See original GitHub issue

Say I have this template with an extends call:

foo.html:

{% extends 'base.html' %}

I load it:

jinja2_env.get_template('foo.html', globals=my_globals}

This causes two calls to Environment._load_template: First, for foo.html, the for base.html.

Environment._load_template caches both templates.

The crucial point is that for the second call, the “globals” parameter to Environment._load_template is missing my_globals.

If I now attempt to load the base template directly:

jinja2_env.get_template('base.html', globals=my_globals}

The cache will be used, and my globals will not be available.

Using 2.7.2

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Reactions:2
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
sophiebits-humucommented, Mar 30, 2021

If I understand correctly, the primary motivation for the cache is to avoid the cost of re-parsing the template HTML multiple times. My hope would be that Jinja could cache that parsing but return a new Template object, which should require only a single small allocation.

If Flask and Ansible don’t expose the ability to pass different globals in, then they wouldn’t be affected by a minor performance regression as long as the caching of templates without globals was not affected (I agree that that seems to be the more common usage pattern).

I would argue that even with a documented warning, this is a very large footgun (even if someone is not too likely to run into this, it can be an incredibly bad bug if it does crop up) and so would prefer a solution 2 or 3 – but a warning in the documentation would be still a significant improvement over the status quo.

1reaction
davidismcommented, Aug 24, 2020

It will be merged, along with all the other PRs we’ve gotten from the MLH program over the last 3 months. There have been so many I haven’t been able to get to them all yet.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Before you begin to deploy Global File Cache Edge instances
Quick steps. Deploy the Global File Cache Virtual Template, or Windows Server 2016 VM, or Windows Server 2019 Standard or Datacenter edition.
Read more >
Caching in GitLab CI/CD
If your project uses Go Modules to install Go dependencies, the following example defines cache in a go-cache template, that any job can...
Read more >
Why is Global State so Evil?
Concurrency issues - mutable global state typically requires some form ... bashing on the same global state causes cache contention and will ...
Read more >
Global Settings & Styles (theme.json) | Block Editor Handbook
To remove this caching issue, set either WP_DEBUG or SCRIPT_DEBUG to 'true' in your wp-config.php . This tells WordPress to skip the cache...
Read more >
Managing Global Template Options
Gobal template options are defined at the theme-level.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found