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

Variables from extra_template_vars() not exposed in _context=1 #693

Closed
simonw opened this issue Mar 2, 2020 · 3 comments
Closed

Variables from extra_template_vars() not exposed in _context=1 #693

simonw opened this issue Mar 2, 2020 · 3 comments
Labels
bug minor Minor bugs (not high priority) plugins

Comments

@simonw
Copy link
Owner

simonw commented Mar 2, 2020

The _context=1 debugging mode does not show variables that should have been added to the context by the extra_template_vars() plugin hook.

@simonw simonw added bug plugins minor Minor bugs (not high priority) labels Mar 2, 2020
@simonw
Copy link
Owner Author

simonw commented Mar 2, 2020

Here's why:

if (
request
and request.args.get("_context")
and self.ds.config("template_debug")
):
return Response.html(
"<pre>{}</pre>".format(
jinja2.escape(json.dumps(template_context, default=repr, indent=4))
)
)
return Response.html(
await self.ds.render_template(template, template_context, request=request)
)

It's bypassing the .ds.render_template() call - and that's where the plugin hook is executed:

datasette/datasette/app.py

Lines 520 to 564 in 613f6fa

async def render_template(
self, templates, context=None, request=None, view_name=None
):
context = context or {}
if isinstance(templates, Template):
template = templates
select_templates = []
else:
if isinstance(templates, str):
templates = [templates]
template = self.jinja_env.select_template(templates)
select_templates = [
"{}{}".format(
"*" if template_name == template.name else "", template_name
)
for template_name in templates
]
body_scripts = []
# pylint: disable=no-member
for script in pm.hook.extra_body_script(
template=template.name,
database=context.get("database"),
table=context.get("table"),
view_name=view_name,
datasette=self,
):
body_scripts.append(Markup(script))
extra_template_vars = {}
# pylint: disable=no-member
for extra_vars in pm.hook.extra_template_vars(
template=template.name,
database=context.get("database"),
table=context.get("table"),
view_name=view_name,
request=request,
datasette=self,
):
if callable(extra_vars):
extra_vars = extra_vars()
if asyncio.iscoroutine(extra_vars):
extra_vars = await extra_vars
assert isinstance(extra_vars, dict), "extra_vars is of type {}".format(
type(extra_vars)
)

@simonw
Copy link
Owner Author

simonw commented Apr 5, 2020

So I should move the template_debug and _context logic into the render_template() method.

@simonw
Copy link
Owner Author

simonw commented Apr 5, 2020

Fixed.

@simonw simonw closed this as completed Apr 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug minor Minor bugs (not high priority) plugins
Projects
None yet
Development

No branches or pull requests

1 participant