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 debug mode that outputs template context #654

Closed
simonw opened this issue Dec 22, 2019 · 3 comments
Closed

Template debug mode that outputs template context #654

simonw opened this issue Dec 22, 2019 · 3 comments
Labels

Comments

@simonw
Copy link
Owner

@simonw simonw commented Dec 22, 2019

It would make writing templates (including custom templates) easier if there was an option to dump out the full template context - maybe ?_context=1

@simonw

This comment has been minimized.

Copy link
Owner Author

@simonw simonw commented Dec 22, 2019

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)
         )
@simonw

This comment has been minimized.

Copy link
Owner Author

@simonw simonw commented Dec 22, 2019

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 template_debug:1 config setting.

@simonw simonw closed this in d54318f Dec 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.