Skip to main content

A module which attempts to load the corresponding templates directly from URLs, without the need to write a view for each URL.

Project description

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_template mandatory: (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's template_folder root.
  • context optional: (e.g.: context: {title: "Welcome", description: "A welcome page"}) A dictionary of extra key / value pairs to pass through to the template context.
  • markdown_includes optional: (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's template_folder root.

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

canonicalwebteam.templatefinder-0.2.4.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file canonicalwebteam.templatefinder-0.2.4.tar.gz.

File metadata

  • Download URL: canonicalwebteam.templatefinder-0.2.4.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.6

File hashes

Hashes for canonicalwebteam.templatefinder-0.2.4.tar.gz
Algorithm Hash digest
SHA256 04dff10d71276c4c0ac9195a224e5bbd9cdb28e0aef607cc8052ca81b5a1c1b6
MD5 886c01370c8746bfb2f5530d1c1515cf
BLAKE2b-256 be1612d5aabb63f976f65c8b14125389cb9761b22742152d5f698e013d2dd550

See more details on using hashes here.

File details

Details for the file canonicalwebteam.templatefinder-0.2.4-py3-none-any.whl.

File metadata

  • Download URL: canonicalwebteam.templatefinder-0.2.4-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.6

File hashes

Hashes for canonicalwebteam.templatefinder-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 d3991f2194990fffbc243523ee0c4f4103fd598e0e95f1a5f2c8321e2cc7c73a
MD5 88cb7b5b10bfb355a5e30eabfa1277b1
BLAKE2b-256 f5ff29abfddf8cef94f7ea7b848ff2e4d88bde59a3d51d86fd9201e3345d98e9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page