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

Plugin hook for adding extra template context variables #541

Closed
simonw opened this issue Jul 5, 2019 · 2 comments
Closed

Plugin hook for adding extra template context variables #541

simonw opened this issue Jul 5, 2019 · 2 comments

Comments

@simonw
Copy link
Owner

simonw commented Jul 5, 2019

It turns out I need this for simonw/datasette-auth-github#5

It can be modelled on the extra_body_script hook: https://datasette.readthedocs.io/en/stable/plugins.html#extra-body-script-template-database-table-view-name-datasette

@simonw
Copy link
Owner Author

simonw commented Jul 5, 2019

Example usage:

@hookimpl
def extra_template_vars(request):
    return {
        "auth": request.scope.get("auth"),
    }

@simonw
Copy link
Owner Author

simonw commented Jul 5, 2019

It should allow for awaitables too, to allow for execution of SQL. For example:

@hookimpl
def extra_template_vars(datasette):
    async def inner():
        first_db = list(datasette.databases.keys())[0]
        return {
            "sqlite_version": (
                await datasette.execute(first_db, "select sqlite_version()")
            ).rows[0][0]
        }
    return inner

simonw added a commit that referenced this issue Jul 5, 2019
@simonw simonw closed this as completed in fcfcae2 Jul 6, 2019
simonw added a commit that referenced this issue Jul 7, 2019
* extra_template_vars plugin hook

Closes #541

* Workaround for cwd bug

Based on pytest-dev/pytest#1235 (comment)
simonw added a commit that referenced this issue Nov 11, 2019
* extra_template_vars plugin hook

Closes #541

* Workaround for cwd bug

Based on pytest-dev/pytest#1235 (comment)
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

1 participant