|
1 |
| -# Template fragments for jinja-like engines |
2 |
| - |
3 |
| -Usage: |
4 |
| - |
5 |
| -```python |
6 |
| -from template_fragments import filter_template |
7 |
| - |
8 |
| -template_source = """ |
9 |
| -<html> |
10 |
| - <head> |
11 |
| - <title>Example</title> |
12 |
| - </head> |
13 |
| - <body> |
14 |
| - <div> |
15 |
| - {% fragment content %} |
16 |
| - <!-- content here --> |
17 |
| - {% endfragment %} |
18 |
| - </div> |
19 |
| - </body> |
20 |
| -</html> |
21 |
| -""" |
22 |
| - |
23 |
| -content_full = filter_template(template_source, "") |
24 |
| -content_item = filter_template(template_source, "item") |
25 |
| -``` |
26 |
| - |
27 |
| -Usage with [Flask]: |
28 |
| - |
29 |
| -```python |
30 |
| -from template_fragments.jinja import FragmentLoader |
31 |
| - |
32 |
| -app.jinja_loader = FragmentLoader(app.jinja_loader) |
33 |
| -``` |
34 |
| - |
35 |
| -[flask]: https://flask.palletsprojects.com/en/2.2.x/ |
36 |
| -[jinja2]: https://jinja.palletsprojects.com/en/3.0.x/ |
| 1 | +# Template fragments for jinja-like engines |
| 2 | + |
| 3 | +Usage: |
| 4 | + |
| 5 | +```python |
| 6 | +from template_fragments import filter_template |
| 7 | + |
| 8 | +template_source = """ |
| 9 | +<html> |
| 10 | + <head> |
| 11 | + <title>Example</title> |
| 12 | + </head> |
| 13 | + <body> |
| 14 | + <div> |
| 15 | + {% fragment content %} |
| 16 | + <!-- content here --> |
| 17 | + {% endfragment %} |
| 18 | + </div> |
| 19 | + </body> |
| 20 | +</html> |
| 21 | +""" |
| 22 | + |
| 23 | +content_full = filter_template(template_source, "") |
| 24 | +content_item = filter_template(template_source, "item") |
| 25 | +``` |
| 26 | + |
| 27 | +Usage with [Flask]: |
| 28 | + |
| 29 | +```python |
| 30 | +from template_fragments.jinja import FragmentLoader |
| 31 | + |
| 32 | +app.jinja_loader = FragmentLoader(app.jinja_loader) |
| 33 | +``` |
| 34 | + |
| 35 | +## API reference |
| 36 | + |
| 37 | +<!-- minidoc "module": "template_fragments", "header": false --> |
| 38 | + |
| 39 | +### `template_fragments.split_templates` |
| 40 | + |
| 41 | +[template_fragments.split_templates]: #template_fragmentssplit_templates |
| 42 | + |
| 43 | +`template_fragments.split_templates(src: str) -> Dict[str, str]` |
| 44 | + |
| 45 | +Return all fragments contained in the template |
| 46 | + |
| 47 | +The key `""` gives the source template with any fragment directives removed. |
| 48 | + |
| 49 | +### `template_fragments.filter_template` |
| 50 | + |
| 51 | +[template_fragments.filter_template]: #template_fragmentsfilter_template |
| 52 | + |
| 53 | +`template_fragments.filter_template(src: str, fragment: str = '') -> str` |
| 54 | + |
| 55 | +Return the parts of the template for the given fragment |
| 56 | + |
| 57 | +Parameters: |
| 58 | + |
| 59 | +- `src`: the template source |
| 60 | +- `fragment`: the fragment to return. If `fragment = ""`, removes any |
| 61 | + fragment directives |
| 62 | + |
| 63 | +### `template_fragments.split_path` |
| 64 | + |
| 65 | +[template_fragments.split_path]: #template_fragmentssplit_path |
| 66 | + |
| 67 | +`template_fragments.split_path(path: str) -> Tuple[str, str]` |
| 68 | + |
| 69 | +Split the fragment from the path |
| 70 | + |
| 71 | +### `template_fragments.TemplateFragmentError` |
| 72 | + |
| 73 | +[template_fragments.TemplateFragmentError]: #template_fragmentstemplatefragmenterror |
| 74 | + |
| 75 | +`template_fragments.TemplateFragmentError` |
| 76 | + |
| 77 | +Common base class for all non-exit exceptions. |
| 78 | + |
| 79 | +<!-- minidoc --> |
| 80 | + |
| 81 | +### `template_fragments.jinja`` |
| 82 | + |
| 83 | +<!-- minidoc "module": "template_fragments.jinja", "header": false --> |
| 84 | +Jinja specific helpers |
| 85 | + |
| 86 | +#### `template_fragments.jinja.FragmentLoader` |
| 87 | + |
| 88 | +[template_fragments.jinja.FragmentLoader]: #template_fragmentsjinjafragmentloader |
| 89 | + |
| 90 | +`template_fragments.jinja.FragmentLoader(base_loader: jinja2.loaders.BaseLoader)` |
| 91 | + |
| 92 | +A loader that filters fragments |
| 93 | + |
| 94 | +<!-- minidoc --> |
| 95 | + |
| 96 | + |
| 97 | +[flask]: https://flask.palletsprojects.com/en/2.2.x/ |
| 98 | +[jinja2]: https://jinja.palletsprojects.com/en/3.0.x/ |
0 commit comments