Skip to content

chmp/template-fragments-py

Repository files navigation

Template fragments for jinja-like engines

API reference | License

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")

Usage with Flask and Jinja2:

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 source
  • fragment: the fragment to return. If fragment = "", 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.

About

Template fragments for jinja-like engines

Resources

License

Stars

Watchers

Forks

Packages

No packages published