Package: jinja_partials
Jinja PartialsSimple reuse of partial HTML page templates in the Jinja template language for Python web frameworks.
(There is also aPyramid/Chameleon version here.)OverviewWhen building real-world web apps with Flask + Jinja2, it's easy to end up with repeated HTML fragments.
Just like organizing code for reuse, it would be ideal to reuse smaller sections of HTML template code.
That's what this library is all about.ExampleThis project comes with a sample flask application (see theexamplefolder). This app displays videos
that can be played on YouTube. The image, subtitle of author and view count are reused throughout the
app. Here's a visual:Check out thedemo / example applicationto see it in action.InstallationIt's justpip install jinja-partialsand you're all set with this pure Python package.UsageUsing the library is incredible easy. The first step is to register the partial method with Jinja and Flask.
Do this once at app startup:importflaskimportjinja_partialsapp=flask.Flask(__name__)jinja_partials.register_extensions(app)# ...You can also use this library in your FastAPI (or Starlette) project!fromfastapi.templatingimportJinja2Templates# or `from starlette.templating import Jinja2Templates`importjinja_partialstemplates=Jinja2Templates("tests/test_templates")jinja_partials.register_starlette_extensions(templates)# ...Or directly using a jijna environment!importjinja_partialsfromjinja2importEnvironment,FileSystemLoaderenvironment=Environment(loader=FileSystemLoader("tests/test_templates"))jinja_partials.register_environment(environment)# ...Next, you define your main HTML (Jinja2) templates as usual. Then
define your partial templates. I recommend locating and naming them accordingly:├── templates
│   ├── home
│   │   ├── index.html
│   │   └── listing.html
│   └── shared
│       ├── _layout.html
│       └── partials
│           ├── video_image.html
│           └── video_square.htmlNotice thepartialssubfolder in thetemplates/sharedfolder.The templates are just HTML fragments. Here is a stand-alone one for the YouTube thumbnail from
the example app:<imgsrc="https://img.youtube.com/vi/{{ video.id }}/maxresdefault.jpg"class="img img-responsive {{ ' '.join(classes) }}"alt="{{ video.title }}"title="{{ video.title }}">Notice that an object calledvideoand list of classes are passed in as the model.Templates can also be nested. Here is the whole single video fragment with the image as well as other info
linking out to YouTube:<div><ahref="https://www.youtube.com/watch?v={{ video.id }}"target="_blank">{{ render_partial('shared/partials/video_image.html', video=video) }}</a><ahref="https://www.youtube.com/watch?v={{ video.id }}"target="_blank"class="author">{{ video.author }}</a><divclass="views">{{ "{:,}".format(video.views) }} views</div></div>Now you see therender_partial()method. It takes the subpath into the templates folder and
any model data passed in as keyword arguments.We can finally generate the list of video blocks as follows:{% for v in videos %}<divclass="col-md-3 video">{{ render_partial('shared/partials/video_square.html', video=v) }}</div>{% endfor %}This time, we reframe each item in the list from the outer template (calledv) as thevideomodel
in the inner HTML section.Why not just useincludeormacrofrom Jinja?The short answer is they are nearly the same, but both fall short in different ways.
For a more detailed response, see the discussion onissue #1