New plugin hook: extra_body_script

This commit is contained in:
Simon Willison 2018-08-28 01:56:44 -07:00
commit 5cf0c6c91c
No known key found for this signature in database
GPG key ID: 17E2DEA2588B7F52
6 changed files with 88 additions and 0 deletions

View file

@ -25,6 +25,11 @@ def extra_js_urls():
"Extra JavaScript URLs added by this plugin"
@hookspec
def extra_body_script(template, database, table, datasette):
"Extra JavaScript code to be included in <script> at bottom of body"
@hookspec
def publish_subcommand(publish):
"Subcommands for 'datasette publish'"

View file

@ -35,6 +35,11 @@
{% endif %}
{% endif %}
</div>
{% for body_script in body_scripts %}
<script>{{ body_script }}</script>
{% endfor %}
{% if select_templates %}<!-- Templates considered: {{ select_templates|join(", ") }} -->{% endif %}
</body>
</html>

View file

@ -52,6 +52,14 @@ class RenderMixin(HTTPMethodView):
"{}{}".format("*" if template_name == template.name else "", template_name)
for template_name in templates
]
body_scripts = []
for script in pm.hook.extra_body_script(
template=template.name,
database=context.get("database"),
table=context.get("table"),
datasette=self.ds
):
body_scripts.append(jinja2.Markup(script))
return response.html(
template.render(
{
@ -60,6 +68,7 @@ class RenderMixin(HTTPMethodView):
"app_css_hash": self.ds.app_css_hash(),
"select_templates": select_templates,
"zip": zip,
"body_scripts": body_scripts,
}
}
)