Skip to main content

No project description provided

Project description

starlette-async-jinja

Code style: crackerjack

Jinja2 is_async template support for Starlette +

About

  • Provides AsyncJinja2Templates class which enables use of the is_async Jinja environment flag with Startlette and Starlette based applications like FastAPI
  • Integrated Jinja Partials and Jinja Fragments
  • Supports asynchronous template loaders (examples here) (not yet tested but should work)

Issues

  • Only asynchronous template loaders (not yet tested but should work) are currently supported

  • The Jinja bytecodecache requires an asynchronous Redis backend

Usage

Import AsyncJinja2 Templates:

from starlette_async_jinja import AsyncJinja2Templates

Replace Jinja2Templates with AsyncJinja2Templates:

templates = Jinja2Templates(directory='templates')

Render the async template in the response:

async def homepage(request: Request):
    return await templates.AsyncTemplateResponse('index.html', {'request': request})

Even simpler:

async def homepage(request: Request):
    return await templates.render_template('index.html', {'request': request})

To render Jinja Fragments or 'blocks':

async def only_block(request: Request):
    return await templates.render_block(
        "page.html.jinja2",
        {"request": request, "magic_number": 42},
        block_name="content"
    )

To render Jinja Partials:

in a template

{% for v in row %}
  <div class="col-md-3 video">
      {{ render_partial('shared/partials/video_square.html', video=v) }}
  </div>
{% endfor %}

or programmatically

async def render_partial(template: str, **kwargs):
  return await templates.render_partial(template, **kwargs)

Acknowledgements

License

BSD-3-Clause

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

starlette_async_jinja-1.4.3.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

starlette_async_jinja-1.4.3-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file starlette_async_jinja-1.4.3.tar.gz.

File metadata

  • Download URL: starlette_async_jinja-1.4.3.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.11.2 CPython/3.12.1

File hashes

Hashes for starlette_async_jinja-1.4.3.tar.gz
Algorithm Hash digest
SHA256 621254a5b8e1c33018a547351bb122fbc7a4c25840a73365cb9e32b97246d5c8
MD5 e253a5444321ffee78db21a19e3378ff
BLAKE2b-256 bc94d12f0ef79b789522d57c05bd7bf818e3e89007a59858f44dd8afd978774b

See more details on using hashes here.

File details

Details for the file starlette_async_jinja-1.4.3-py3-none-any.whl.

File metadata

File hashes

Hashes for starlette_async_jinja-1.4.3-py3-none-any.whl
Algorithm Hash digest
SHA256 dceb9393a326266d6c9cedc5e747394181d69c9737b34bd8b4a6f7c354b7d8d9
MD5 fa693930a988bcdd6d0b00adee12f7de
BLAKE2b-256 15642fc173d48a357513b585012cbf71e4ff226ca180c86ebd0d5642e6aeffa9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page