TemplateFinder
TemplateFinder is an extension of TemplateView which attempts to
load the corresponding templates directly from URLs, without the need to
write a view for each URL.
It can load HTML templates directly, or parse Markdown files that contain a "wrapper_template" frontmatter key.
Usage
To register the template finder in your Flask app you need to register the template folder in the application config, and specify which routes should be handled by it. The following example will handle everything via the templatefinder:
from canonicalwebteam.templatefinder import TemplateFinder
TEMPLATE_FOLDER =
app = Flask(
template_folder="templates",
static_folder="static",
)
app.config["TEMPLATE_FOLDER"] = "templates"
template_finder_view = TemplateFinder.as_view("template_finder")
app.add_url_rule("/", view_fun c=template_finder_view)
app.add_url_rule("/<path:subpath>", view_func=template_finder_view)
Template matching
The templatefinder can be used to automatically map .html and .md files to url on a website.
When included the finder will search for files at the given url in a specified template directory.
E.g. localhost/pages/test will look for the following files, in order:
$TEMPLATE_FOLDER/pages/test.html$TEMPLATE_FOLDER/pages/test/index.html$TEMPLATE_FOLDER/pages/test.md$TEMPLATE_FOLDER/pages/test/index.md
Markdown parsing
If the TemplateFinder encounters a Markdown file (ending .md) it
will look for the following keys in YAML
frontmatter:
wrapper_templatemandatory: (e.g.:wrapper_template: includes/markdown-wrapper.html) A path to an HTML template within which to place the parsed markdown content. This path must be relative to Flask'stemplate_folderroot.contextoptional: (e.g.:context: {title: "Welcome", description: "A welcome page"}) A dictionary of extra key / value pairs to pass through to the template context.markdown_includesoptional: (e.g.:markdown_includes: {nav: }) A mapping of key names to template paths pointing to Markdown files to include. Each template path will be parsed, the resulting HTML will be passed in the template context, under the relevant key. Paths must be relative to Flask'stemplate_folderroot.
Here's an example Markdown file:
---
wrapper_template: "includes/markdown-wrapper.html"
markdown_includes:
nav: "includes/nav.md"
context:
title: "Welcome"
description: "A welcome page"
---
Welcome to my website.
## GitHub
I also have [a GitHub page](https://github.com/me).
Tests
Tests can be run with pytest:
pip3 install -r requirements.txt
python3 -m pytest test
Metadata
Release files for canonicalwebteam.templatefinder 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| canonicalwebteam.templatefinder-1.0.0.tar.gz | 7.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| canonicalwebteam.templatefinder-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.1 kB
Release files / canonicalwebteam.templatefinder-1.0.0.tar.gz
| Download URL | canonicalwebteam.templatefinder-1.0.0.tar.gz |
|---|---|
| Size | 7.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5bb51a04aeb6f2a3f7298bb1439c31c52636cc8d5f507d7e440618d2c06ba829
|
|
BLAKE2b-256 checksum How to use checksums |
63f93943d05dbacc44e4b376339fa55c74dffee4fc6177553f87ba846c80e088
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7
|
Release files / canonicalwebteam.templatefinder-1.0.0-py3-none-any.whl
| Download URL | canonicalwebteam.templatefinder-1.0.0-py3-none-any.whl |
|---|---|
| Size | 8.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f9226bf00b378e2b8be1c09ab31c2aeebc97b6af53016464497e6bcc636a3a90
|
|
BLAKE2b-256 checksum How to use checksums |
2d9967439d1d9e8ac1cced42d48189f2a7046794bc0af17e486000725fe141f1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7
|