Template fragments for jinja-like engines
Project description
Template fragments for jinja-like engines
Usage:
from template_fragments import filter_template
template_source = """
<html>
<head>
<title>Example</title>
</head>
<body>
<div>
{% fragment content %}
<!-- content here -->
{% endfragment %}
</div>
</body>
</html>
"""
content_full = filter_template(template_source, "")
content_item = filter_template(template_source, "item")
from template_fragments.jinja import FragmentLoader
app.jinja_loader = FragmentLoader(app.jinja_loader)
@app.route("/")
def get_index():
return render_template("index.html", ...)
@app.route("/item/<item>")
def get_item(item):
return render_template("index.html#item", ...)
API reference
template_fragments.split_templates
template_fragments.split_templates(src: str) -> Dict[str, str]
Return all fragments contained in the template
The key ""
gives the source template with any fragment directives removed.
template_fragments.filter_template
template_fragments.filter_template(src: str, fragment: str = '') -> str
Return the parts of the template for the given fragment
Parameters:
src
: the template sourcefragment
: the fragment to return. Iffragment = ""
, removes any fragment directives
template_fragments.split_path
template_fragments.split_path(path: str) -> Tuple[str, str]
Split the fragment from the path
template_fragments.TemplateFragmentError
template_fragments.TemplateFragmentError
Common base class for all non-exit exceptions.
template_fragments.jinja
Jinja specific helpers
template_fragments.jinja.FragmentLoader
template_fragments.jinja.FragmentLoader(base_loader: jinja2.loaders.BaseLoader)
A loader that filters fragments
License
The MIT License (MIT)
Copyright (c) 2022 - 2023 Christopher Prohm
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for template-fragments-23.6.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | a2c26a6033d21ad1cf2548f02e4b44ab6db23ea4c26e47cb3080780d1039561e |
|
MD5 | 14edd14424058ec7af21cb7e480069b5 |
|
BLAKE2b-256 | e38d75fc70c683de72c65353525343c17791a78772e20446ae43f656406544ed |
Hashes for template_fragments-23.6.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 012bdb310dcf3aeb58517dd2f219fec4af96b1a7ea2fddc074e0b884958780a9 |
|
MD5 | 84db6b1fadb4be6fd4f6eb9c295f31d2 |
|
BLAKE2b-256 | 809dc2fcfcf84312e85d6c5fe40ec7792384a8205993a30f9fb5f4208b6d77c1 |