-
-
Notifications
You must be signed in to change notification settings - Fork 677
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 debug mode that outputs template context #654
Comments
Proof of concept: diff --git a/datasette/views/base.py b/datasette/views/base.py
index 5182479..39d1f77 100644
--- a/datasette/views/base.py
+++ b/datasette/views/base.py
@@ -1,6 +1,7 @@
import asyncio
import csv
import itertools
+import json
import re
import time
import urllib
@@ -138,28 +139,31 @@ class BaseView(AsgiView):
)
extra_template_vars.update(extra_vars)
+ template_context = {
+ **context,
+ **{
+ "app_css_hash": self.ds.app_css_hash(),
+ "select_templates": select_templates,
+ "zip": zip,
+ "body_scripts": body_scripts,
+ "extra_css_urls": self._asset_urls(
+ "extra_css_urls", template, context
+ ),
+ "extra_js_urls": self._asset_urls(
+ "extra_js_urls", template, context
+ ),
+ "format_bytes": format_bytes,
+ "database_url": self.database_url,
+ "database_color": self.database_color,
+ },
+ **extra_template_vars,
+ }
+ if request.args.get("_context"):
+ return Response.html("<pre>{}</pre>".format(
+ escape(json.dumps(template_context, default=repr, indent=4))
+ ))
return Response.html(
- await template.render_async(
- {
- **context,
- **{
- "app_css_hash": self.ds.app_css_hash(),
- "select_templates": select_templates,
- "zip": zip,
- "body_scripts": body_scripts,
- "extra_css_urls": self._asset_urls(
- "extra_css_urls", template, context
- ),
- "extra_js_urls": self._asset_urls(
- "extra_js_urls", template, context
- ),
- "format_bytes": format_bytes,
- "database_url": self.database_url,
- "database_color": self.database_color,
- },
- **extra_template_vars,
- }
- )
+ await template.render_async(template_context)
) |
One problem with this: what if secrets end up being dumped out in this debug view? This won't happen with default Datasette but could potentially happen with a plugin. This feature should be opt-in - maybe a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It would make writing templates (including custom templates) easier if there was an option to dump out the full template context - maybe
?_context=1
The text was updated successfully, but these errors were encountered: