{"id":13780482,"url":"https://github.com/mikeckennedy/jinja_partials","last_synced_at":"2025-04-04T19:12:26.891Z","repository":{"id":38795141,"uuid":"389731072","full_name":"mikeckennedy/jinja_partials","owner":"mikeckennedy","description":"Simple reuse of partial HTML page templates in the Jinja template language for Python web frameworks. #pypackage","archived":false,"fork":false,"pushed_at":"2024-04-12T17:27:52.000Z","size":2946,"stargazers_count":200,"open_issues_count":0,"forks_count":7,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-10-10T19:08:47.061Z","etag":null,"topics":["flask","html","jinja2","python","template-engine"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mikeckennedy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["mikeckennedy"]}},"created_at":"2021-07-26T18:25:00.000Z","updated_at":"2024-10-10T18:39:29.000Z","dependencies_parsed_at":"2024-04-12T17:53:08.573Z","dependency_job_id":null,"html_url":"https://github.com/mikeckennedy/jinja_partials","commit_stats":{"total_commits":26,"total_committers":2,"mean_commits":13.0,"dds":"0.15384615384615385","last_synced_commit":"31e813008dc60d1e8c0e7c2ad0fb6045ae54009a"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikeckennedy%2Fjinja_partials","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikeckennedy%2Fjinja_partials/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikeckennedy%2Fjinja_partials/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikeckennedy%2Fjinja_partials/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mikeckennedy","download_url":"https://codeload.github.com/mikeckennedy/jinja_partials/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247234923,"owners_count":20905854,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["flask","html","jinja2","python","template-engine"],"created_at":"2024-08-03T18:01:16.215Z","updated_at":"2025-04-04T19:12:26.873Z","avatar_url":"https://github.com/mikeckennedy.png","language":"Python","funding_links":["https://github.com/sponsors/mikeckennedy"],"categories":["Third Party Packages 📦 \u003ca name = \"tools\"\u003e\u003c/a\u003e"],"sub_categories":["Helper Libraries"],"readme":"# Jinja Partials\n\nSimple reuse of partial HTML page templates in the Jinja template language for Python web frameworks.\n(There is also a [Pyramid/Chameleon version here](https://github.com/mikeckennedy/chameleon_partials).)\n\n## Overview\n\nWhen building real-world web apps with Flask + Jinja2, it's easy to end up with repeated HTML fragments.\nJust like organizing code for reuse, it would be ideal to reuse smaller sections of HTML template code.\nThat's what this library is all about.\n\n## Example\n\nThis project comes with a sample flask application (see the `example` folder). This app displays videos\nthat can be played on YouTube. The image, subtitle of author and view count are reused throughout the\napp. Here's a visual:\n\n![](https://raw.githubusercontent.com/mikeckennedy/jinja_partials/main/readme_resources/reused-html-visual.png)\n\nCheck out the [**demo / example application**](https://github.com/mikeckennedy/jinja_partials/tree/main/example) \nto see it in action. \n\n## Installation\n\nIt's just `pip install jinja-partials` and you're all set with this pure Python package.\n\n## Usage\n\nUsing the library is incredible easy. The first step is to register the partial method with Jinja and Flask.\nDo this once at app startup:\n\n```python\nimport flask\nimport jinja_partials\n\napp = flask.Flask(__name__)\n\njinja_partials.register_extensions(app)\n# ...\n```\n\nYou can also use this library in your FastAPI (or Starlette) project!\n```python\nfrom fastapi.templating import Jinja2Templates\n# or `from starlette.templating import Jinja2Templates`\n\nimport jinja_partials\n\ntemplates = Jinja2Templates(\"tests/test_templates\")\n\njinja_partials.register_starlette_extensions(templates)\n# ...\n```\n\nOr directly using a jijna environment!\n```python\nimport jinja_partials\nfrom jinja2 import Environment, FileSystemLoader\n\nenvironment = Environment(loader=FileSystemLoader(\"tests/test_templates\"))\njinja_partials.register_environment(environment)\n# ...\n```\n\nNext, you define your main HTML (Jinja2) templates as usual. Then \ndefine your partial templates. I recommend locating and naming them accordingly:\n\n```\n├── templates\n│   ├── home\n│   │   ├── index.html\n│   │   └── listing.html\n│   └── shared\n│       ├── _layout.html\n│       └── partials\n│           ├── video_image.html\n│           └── video_square.html\n```\n\nNotice the `partials` subfolder in the `templates/shared` folder.\n\nThe templates are just HTML fragments. Here is a stand-alone one for the YouTube thumbnail from\nthe example app:\n\n```html\n\u003cimg src=\"https://img.youtube.com/vi/{{ video.id }}/maxresdefault.jpg\"\n     class=\"img img-responsive {{ ' '.join(classes) }}\"\n     alt=\"{{ video.title }}\"\n     title=\"{{ video.title }}\"\u003e\n```\n\nNotice that an object called `video` and list of classes are passed in as the model.\n\nTemplates can also be nested. Here is the whole single video fragment with the image as well as other info\nlinking out to YouTube:\n\n```html\n\u003cdiv\u003e\n    \u003ca href=\"https://www.youtube.com/watch?v={{ video.id }}\" target=\"_blank\"\u003e\n        {{ render_partial('shared/partials/video_image.html', video=video) }}\n    \u003c/a\u003e\n    \u003ca href=\"https://www.youtube.com/watch?v={{ video.id }}\" target=\"_blank\"\n       class=\"author\"\u003e{{ video.author }}\u003c/a\u003e\n    \u003cdiv class=\"views\"\u003e{{ \"{:,}\".format(video.views) }} views\u003c/div\u003e\n\u003c/div\u003e\n```\n\nNow you see the `render_partial()` method. It takes the subpath into the templates folder and\nany model data passed in as keyword arguments.\n\nWe can finally generate the list of video blocks as follows:\n\n```html\n{% for v in videos %}\n\n    \u003cdiv class=\"col-md-3 video\"\u003e\n        {{ render_partial('shared/partials/video_square.html', video=v) }}\n    \u003c/div\u003e\n\n{% endfor %}\n```\n\nThis time, we reframe each item in the list from the outer template (called `v`) as the `video` model\nin the inner HTML section.\n\n\n## Why not just use `include` or `macro` from Jinja?\n\nThe short answer is they are nearly the same, but both fall short in different ways. \nFor a more detailed response, see the discussion on [**issue #1**](https://github.com/mikeckennedy/jinja_partials/issues/1)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmikeckennedy%2Fjinja_partials","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmikeckennedy%2Fjinja_partials","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmikeckennedy%2Fjinja_partials/lists"}