Template imported/rendered twice
See original GitHub issueI am just getting started with jinja2 and I ran into a really weird issue, also described on issue #255 (closed, but there was no followup by the reporter).
I’m on Windows 7 x64 running Python 3.4.1 I installed jinja2 with “easy_install Jinja2”
I have a …\jinja2-test\jinja2-test.py containing this:
from jinja2 import Environment, PackageLoader
env = Environment(loader=PackageLoader('jinja2-test', 'templates'))
template = env.get_template('mytemplate.html')
print(env.list_templates())
print(template.render(the='variables', go='here'))
and a template …\jinja2-test\templates\mytemplate.html
<html>
<head></head>
<body>
<h1>Rendered template</h1>
This template shows that the {{ the }} go {{ go }}
</body>
</html>
on the command line I do: python jinja2-test.py and the templates are listed and rendered twice:
...\jinja2-test> python jinja2-test.py
['mytemplate.html']
<html>
<head></head>
<body>
<h1>Rendered template</h1>
This template shows that the variables go here
</body>
</html>
['mytemplate.html']
<html>
<head></head>
<body>
<h1>Rendered template</h1>
This template shows that the variables go here
</body>
</html>
Issue Analytics
- State:
- Created 9 years ago
- Reactions:1
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Why is ng-template in Angular 10 rendered twice?
I figured out a problem with twice rendered components when a ng-template and ... import { Component, OnInit, Input, ViewChild, ...
Read more >Using a template twice on the same page - Google Groups
Is there a way to use a template twice on the same page? We don't want to have to create a duplicate template...
Read more >Import Permit Program - Frequently Asked Questions - CDC
A detailed description of how the material was rendered noninfectious. Is there a fee for obtaining a CDC import permit? No. Currently, there...
Read more >Phoenix LiveView v0.18.3 - HexDocs
LiveView provides rich, real-time user experiences with server-rendered HTML. ... For each LiveView in the root of a template, mount/3 is invoked twice: ......
Read more >Stories for multiple components - Storybook - JS.ORG
It's useful to write stories that render two or more components at once if those components are designed to work together. For example,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Had the same issue on arch linux with python 3.4.2
The PackageLoader constructor is running the whole script a second time, it’s not 2 prints in the script itself. Use FileSystemLoader instead and it works fine:
The documentation was a little misleading there 😉
This is equivalent to creating a file
example.py
that imports itself:python example.py
first importsexample
as__main__
, then it imports itself asexample
. Because of the way the import cache works, this results in two executions of the module. That’s just Python, it’s not something Jinja can do anything about.If you set up a real package with
__init__.py
and run it as a module, or as an entry point, this doesn’t happen. Or put the main code in anif __name__ == "__main__":
block.