Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 865897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:42:45+00:00 2026-05-15T09:42:45+00:00

In Jinja2, how can one access assigned variables (i.e. {% set X=Y %} )

  • 0

In Jinja2, how can one access assigned variables (i.e. {% set X=Y %}) within files incorporated with include?

I’d expect the following to work given two Jinja2 files:

A.jinja:

Stuff
{% include 'B.jinja' -%}
B has {{ N }} references

B.jinja:

{% set N = 12 %}

I’d expect that A.jinja, when compiled with Jinja2, would produce the following output:

Stuff 
B has 12 references

However, it produces:

Stuff
B has  references

I’d be much obliged for any input as to how to access the Jinja2 variables, such as N above, in the file that includes the file where N is set.

Thank you for reading.

Brian

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T09:42:45+00:00Added an answer on May 15, 2026 at 9:42 am

    There’s a difference between include and import, although you should be able to do both.

    • include 'B.jinja simply renders the template and ignores any variable assignments or macros within it.
    • import 'B.jinja' as B, imports B as if it were a module, so you have to output B.N.
    • from 'B.jinja' import N imports variable N directly.

    Change your import line to the last option and see if that fixes things. If you need more help, look at the documentation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way I can load a jinja2 template from within another template
what sort of conditions can we use for branching in jinja2? I mean can
I am working on getting Jinja2 to work with Google AppEngine. I have the
With Jinja2, how can I make an iteration like the folllowing which works with
I generally work with Python to create web apps and love how I can
I've struggled to get this Jinja2 custom extension to work -- the docs weren't
I am using Jinja2 to generate HTML files which are typically very huge in
Does Jinja2 support template-relative paths e.g. %(here)s/other/template.html , to include other templates relative to
The following code works correctly: from jinja2 import Template mylist = ['some text \xc3']
from jinja2 import Template template = Template('Hello {{ name }}!') template.render(name='John Doe') I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.