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

Path parameters for custom pages #944

Closed
simonw opened this issue Aug 19, 2020 · 5 comments
Closed

Path parameters for custom pages #944

simonw opened this issue Aug 19, 2020 · 5 comments

Comments

@simonw
Copy link
Owner

simonw commented Aug 19, 2020

Custom pages let you e.g. create a templates/pages/about.html page and have it automatically served at /about.

It would be useful if these pages could capture path patterns. I like the Python format string syntax for this (also used by Starlette): /foo/bar/{slug}.

So... how about embedding those patterns in the filenames themselves?

templates/pages/museums/{slug}.html

Would capture any hits to /museums/something and use that page to serve them.

@simonw
Copy link
Owner Author

simonw commented Aug 19, 2020

I created a template file called templates/pages/museums/{slug}.html and used the debugger to see if Jinja could see it. This worked:

(Pdb) self.ds.jinja_env.list_templates()
['500.html', '_codemirror.html', '_codemirror_foot.html', '_description_source_license.html', '_footer.html',
'_table.html', 'allow_debug.html', 'base.html', 'database.html', 'default:500.html', 'default:_codemirror.html',
'default:_codemirror_foot.html', 'default:_description_source_license.html', 'default:_footer.html',
'default:_table.html', 'default:allow_debug.html', 'default:base.html', 'default:database.html',
'default:index.html', 'default:logout.html', 'default:messages_debug.html', 'default:patterns.html',
'default:permissions_debug.html', 'default:query.html', 'default:row.html', 'default:show_json.html',
'default:table.html', 'forbidden.html', 'index.html', 'logout.html', 'messages_debug.html',
'pages/about.html', 'pages/museums/{slug}.html', 'patterns.html', 'permissions_debug.html',
'query.html', 'row.html', 'show_json.html', 'table.html']

The pages/museums/{slug}.html template is in that list.

Here's the implementation of that list_templates() method - it does some filesystem walking so it may be a bit expensive to run it on every request: https://github.com/pallets/jinja/blob/ca8b0b0287e320fe1f4a74f36910ef7ae3303d99/src/jinja2/loaders.py#L197-L212

But caching it would be pretty easy - either until the server is restarted or as an in-memory cache for a few seconds.

@simonw
Copy link
Owner Author

simonw commented Aug 19, 2020

These templates will need a way to raise a 404 - so that if the template itself is deciding if the page exists (for example using datasette-template-sql or the proposed datasette.get() method from #943 or the graphql() template function in simonw/datasette-graphql#50) it can return a regular 404 page.

This can imitate the custom_redirect() function from https://docs.datasette.io/en/stable/custom_templates.html#custom-redirects:

{{ custom_redirect("https://github.com/simonw/datasette", 301) }}

It could be as simple as this:

{{ raise_404("Museum not found") }}

@simonw simonw added this to the Datasette 0.49 milestone Sep 13, 2020
@simonw
Copy link
Owner Author

simonw commented Sep 14, 2020

I'm going to cache the list_templates() result in memory. If you want to add a new template-defined route you will need to restart the server. I think that's acceptable.

@simonw
Copy link
Owner Author

simonw commented Sep 14, 2020

Actually don't need {{ raise_404("Museum not found") }} because we already have {{ custom_status(404) }}.

@simonw simonw closed this as completed in cc77fcd Sep 14, 2020
simonw added a commit that referenced this issue Sep 14, 2020
simonw added a commit to natbat/rockybeaches that referenced this issue Sep 14, 2020
@simonw
Copy link
Owner Author

simonw commented Sep 14, 2020

Having tried this out I think it does need a raise_404() mechanism - which needs to be smart enough to trigger the default 404 handler without accidentally going into an infinite loop.

simonw added a commit that referenced this issue Sep 14, 2020
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