Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(newb) How to pass in context to included templates #501

Open
yatesco opened this issue Jun 24, 2021 · 4 comments
Open

(newb) How to pass in context to included templates #501

yatesco opened this issue Jun 24, 2021 · 4 comments

Comments

@yatesco
Copy link

yatesco commented Jun 24, 2021

Hi - I have a common template which renders a sortable table header. In a larger template I want to call it and pass in the context (e.g. name, whether it is sorted ascending/descending).

I understand the included template has access to the context of the template it is being included in, but how do I pass in additional context.

My outer template:

        <tr>
          {% include "tableHeaderSort.html" sort='ascending' title='Open'}
          {% include "tableHeaderSort.html" sort='none' title='Closed'}
          ...

Is this possible?

Thanks!

@djc
Copy link
Owner

djc commented Jun 24, 2021

No, but you can macros for this instead.

@yatesco
Copy link
Author

yatesco commented Jun 24, 2021

thanks @djc - let me have another read of the book to get familiar with those. Thanks :-)

@OskarPersson
Copy link

@djc How would one use macros for this - is there an example available anywhere? Or has something like Django's with keyword inside an include been implemented since this issue was reported?

@djc
Copy link
Owner

djc commented Mar 23, 2022

For macros you'd have to pass all the required context in explicitly, you wouldn't have access to the ambient context inside the macro as far as I remember. There's a little bit of documentation and some tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants