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

template tags pollutes global context with 'object' #132

Open
benzkji opened this issue Feb 8, 2023 · 4 comments
Open

template tags pollutes global context with 'object' #132

benzkji opened this issue Feb 8, 2023 · 4 comments

Comments

@benzkji
Copy link

benzkji commented Feb 8, 2023

I had a hard time debugging a project where this library was used. The problem is, that the {% snippet_fragment 'whatever' %} overrides the object of the current context. This is not as it should be...in my case, introducing a snippet in a detail view caused the main object of my detail view to change.

cms 3.11.1 snippet 3.1.0 django 3.2.16

@fsbraun
Copy link
Sponsor Member

fsbraun commented Feb 8, 2023

I'd assume that the snippet_fragment will not need access to the context, or will it? If not it can be solved by providing the template tag with its own context replacing context.update:

context.update({
'object': instance,
})
try:
if instance.template:
context.update({
'html': mark_safe(instance.html)
})
content = template.loader.render_to_string(
instance.template,
context.flatten(),
)
else:
t = template.Template(instance.html)
content = t.render(context)
except template.TemplateDoesNotExist:
content = _('Template %(template)s does not exist.') % {

@benzkji
Copy link
Author

benzkji commented Feb 8, 2023

It may be the case that some people use it this way - the way I dont want/need it: using context from "outside", in the snippet, so changing this might breakt their projects.. ;-) Alternative solutions could be: Clone the context (not very efficient, probably!), or use a different name for the snippet object in the context ('snippet'?).

I've not used djangocms-snippet before, so I'm no big help of how people use it. There is one closed issue, though, and how I can feel there, it should work as we two think. see #38

@fsbraun
Copy link
Sponsor Member

fsbraun commented Feb 9, 2023

If this is a potential use case the solution probably involves "popping" the updated context after rendering the content: context.pop() to uncover the old object context.

    context.update({'object': instance})
    # render here
    context.pop()  # uncover old object instance

Just one has to be aware that there are multiple updates.

@benzkji
Copy link
Author

benzkji commented Feb 16, 2023

Yeah, that would do it!

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

No branches or pull requests

2 participants