Skip to main content

Fast and Typed Python Implementation for Handlebars based on Rust.

Project description

PyHandlebars

Python library for the handlebars templating language. It is a small wrapper around the handlebars-rust library.

Installation

Pyhandlebars is listed on PyPi:

pip install pyhandlebars

Getting Start

1. Simple Template

from pyhandlebars import Template

t: Template = Template("Hallo {{name}}")
rendered_text = t.format({"name": "world"})"

# Returns "Hello world"

2. Pydantic BaseModel Support

def test_simple_pydantic_support():
    from pyhandlebars import Template
    from pydantic import BaseModel

    class Person(BaseModel):
        name: str

    t: Template[Person] = Template("This is {{name}}!")
    rendered_text = t.format(Person(name="Alice"))
    assert rendered_text == "This is Alice!"

3. Adding Helper Functions

PyHandlebars allows you to register your own helper functions. However, keep in mind that these are not as fast and performant as built-in helpers.

    def test_custom_helper():
    from pyhandlebars import PyHandlebars, Template

    # 1. Define the helper function.
    def shout(params: list[str], context: dict):
        return f"{params[0].upper()} from {context['location']}"

    client = PyHandlebars()
    client.register_helper("shout", shout)

    t: Template[dict] = Template("{{shout name}}", client=client)
    assert t.format({"name": "Alice", "location": "Wonderland"}) == "ALICE from Wonderland"

4. More Examples

More examples can be found in the tests/test_examples.py file.

Supported Template Functions

The original HandlebarsJS supports different Built-in Helpers. PyHandlebars supports the following subset (given by handlebars-rust):

{{{{raw}}}} ... {{{{/raw}}}} Escape handlebars expression within the block

{{#if ...}} ... {{else}} ... {{/if}} if-else block
Boolean Operators can be used here, for example {{#if (gt 2 1)}} ...
- other operator: eq, ne, gt, gte, lt, lte, and, or, not

{{#unless ...}} ... {{else}} .. {{/unless}} if-not-else block

{{#each ...}} ... {{/each}} iterates over an array or object. Handlebars-rust doesn’t support mustache iteration syntax so use each instead.

{{#with ...}} ... {{/with}} change current context. Similar to {{#each}}, used for replace corresponding mustache syntax.

{{lookup ... ...}} get value from array or map by @index or @key

{{> ...}} include template by its name

{{len ...}} returns length of array/object/string

Comparison to other Template Engines in Python

We benchmarked PyHandlebars against 13 other Python template engines across 4 examples, running 1,000 iterations each. Each benchmark measures two phases:

  • Prepare — compiling / registering the template (done once per request cycle in a real app)
  • Render — filling data into the compiled template (the hot path)

Examples

Example What it tests
Invoice Simple variable substitution and list iteration over line items with a nested address
User profile A boolean conditional (verified badge) combined with iteration over social links
Deployment report Nested object access and dictionary lookups — status labels and region info keyed by name
Release report Deep nesting — components each containing a test suite and a dependency list, plus environment conditionals

Results

Each box shows the render-time distribution across 1,000 runs. Tools are sorted fastest → slowest by average render time. The top chart uses a linear scale; the bottom uses a log scale to make differences between fast engines visible.

Benchmark summary

Contribution

Any contribution to this library is welcomed. To get started into development! When you run into any problems, feel free to reach out to me.

Notes

I mainly setup the library for the use case of prompt templating. If you miss something, just send me a DM or feel free to jump in with a PR 💫

License

This library (PyHandlebars) is open sourced under the MIT License.

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

pyhandlebars-0.2.0.tar.gz (1.3 MB view details)

Uploaded Source

Built Distributions

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

pyhandlebars-0.2.0-cp313-cp313-win_amd64.whl (407.6 kB view details)

Uploaded CPython 3.13Windows x86-64

pyhandlebars-0.2.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (581.0 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

pyhandlebars-0.2.0-cp313-cp313-macosx_11_0_arm64.whl (509.2 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pyhandlebars-0.2.0-cp312-cp312-win_amd64.whl (408.0 kB view details)

Uploaded CPython 3.12Windows x86-64

pyhandlebars-0.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (581.7 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

pyhandlebars-0.2.0-cp312-cp312-macosx_11_0_arm64.whl (509.3 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

pyhandlebars-0.2.0-cp311-cp311-win_amd64.whl (408.7 kB view details)

Uploaded CPython 3.11Windows x86-64

pyhandlebars-0.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (583.7 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

pyhandlebars-0.2.0-cp311-cp311-macosx_11_0_arm64.whl (511.0 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

pyhandlebars-0.2.0-cp310-cp310-win_amd64.whl (408.9 kB view details)

Uploaded CPython 3.10Windows x86-64

pyhandlebars-0.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (583.9 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

pyhandlebars-0.2.0-cp310-cp310-macosx_11_0_arm64.whl (511.1 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

Details for the file pyhandlebars-0.2.0.tar.gz.

File metadata

  • Download URL: pyhandlebars-0.2.0.tar.gz
  • Upload date:
  • Size: 1.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyhandlebars-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e296563b82839f32a57f5757621bb53670e4d4e2eb8a0f660a4f3cebfe37dd38
MD5 aed92b593698f89808559e3d87e68b23
BLAKE2b-256 316da190695df6a4dbdba393a4143590366c0c8348d409bdd53f4b5af19f26b4

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhandlebars-0.2.0.tar.gz:

Publisher: ci.yml on sukram42/pyhandlebars

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyhandlebars-0.2.0-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for pyhandlebars-0.2.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 43aa3b7849c2ad2c413b3218965f7cc7d7d1c4962b931732ab0da8fb618543b1
MD5 07acf96c92be454a2171787df82066e3
BLAKE2b-256 21fc064cc7efbb4ed72e3e5e23df51ac1233012d2e24be5066ffba7d7bb12103

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhandlebars-0.2.0-cp313-cp313-win_amd64.whl:

Publisher: ci.yml on sukram42/pyhandlebars

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyhandlebars-0.2.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyhandlebars-0.2.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2357d2eaa98cb409ab4d1cd7bc1da9f51c0142a7ba526339afacfcf14419e3fa
MD5 ee39773209fe011a70901edd424ac8d6
BLAKE2b-256 51c960d99d578b50d6fc30ece1f88468eff81ec7462ed994398d61665dc2a778

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhandlebars-0.2.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: ci.yml on sukram42/pyhandlebars

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyhandlebars-0.2.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyhandlebars-0.2.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 765e1605f19e1f5634cbbd85c9aebe44e9243b9a1773a49399a0e8989b100b49
MD5 06a8a40b81b2e9a82ee60f63e268f4b3
BLAKE2b-256 d6945829ae311f8219407123e645d46e802b08424a5c8f8e359197ce282adef4

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhandlebars-0.2.0-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: ci.yml on sukram42/pyhandlebars

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyhandlebars-0.2.0-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for pyhandlebars-0.2.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 22bd5daeb42550b2d6ffbe20305e2afc3db78575528c3792e57ddb7ec8f35802
MD5 66dd0adee8dd4062b68101d70b27bf90
BLAKE2b-256 c4a175802edb8b6cc29213de10cb512448eaf7a1dd1cf7dfe347e8c7a7181757

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhandlebars-0.2.0-cp312-cp312-win_amd64.whl:

Publisher: ci.yml on sukram42/pyhandlebars

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyhandlebars-0.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyhandlebars-0.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6c3f4640a7a4b82500453ca974b440d46a154781be3280860284e587cb2ff1a3
MD5 b46ea0b677941b6bdf8861818e58dc13
BLAKE2b-256 7758ff16d4d5cc22e0c5545974cb34f8155aa542e6a35c00e2ba50290423240e

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhandlebars-0.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: ci.yml on sukram42/pyhandlebars

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyhandlebars-0.2.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyhandlebars-0.2.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 72574e68ee21a291145f039d3ead3f2a3cf5c396497a59e8cf1fba1751d67e73
MD5 a554320da42ed8a98a2f544a44137e6f
BLAKE2b-256 7734b273183503925da1eac49b8b56bf252d7e628f56d3eebdc28995d6cfada4

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhandlebars-0.2.0-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: ci.yml on sukram42/pyhandlebars

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyhandlebars-0.2.0-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for pyhandlebars-0.2.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 45c226e4367386ef2b4e4d7c1b3eb95b9ff120e858ff21fc1d83ce89001bf2b3
MD5 7d916a2484f9a0c867746aebaafdc0d7
BLAKE2b-256 89bf91d1871694a3e21daf788ced76d76d92adc630e53ed7e1d580e0a6dd509e

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhandlebars-0.2.0-cp311-cp311-win_amd64.whl:

Publisher: ci.yml on sukram42/pyhandlebars

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyhandlebars-0.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyhandlebars-0.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9ddbff2220a79e8a56b8b77d241da734985912b62f862d7f09fcc13ddbed2c1d
MD5 843d667a36bc6b9197596dad273dc84d
BLAKE2b-256 1bc10ea5513b0c4c8fb695ca946f162357fd0bc8029d7c5dc5fd2c0e8960d791

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhandlebars-0.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: ci.yml on sukram42/pyhandlebars

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyhandlebars-0.2.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyhandlebars-0.2.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8eb677466d4555b84882e10ae31f3478f8cf4b0bc581aa00eb0eb0c3bdf32210
MD5 cf7f4df9d04f11a63e551a4c906cac21
BLAKE2b-256 27853147e99ef6fd538aa3d77c2852243a5dab5c2534239ab544c961aebfd466

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhandlebars-0.2.0-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: ci.yml on sukram42/pyhandlebars

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyhandlebars-0.2.0-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for pyhandlebars-0.2.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 8fc51fa2bdae7fb696f8a99f5d07424cf6400d1b7688e45a06480874978d4b27
MD5 14fd61ccc5dd44e1b130a7c65805bedb
BLAKE2b-256 f7bea840e8b2087f6fbf318faa6bc3673ac00d0742bf68834358928d057f60fb

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhandlebars-0.2.0-cp310-cp310-win_amd64.whl:

Publisher: ci.yml on sukram42/pyhandlebars

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyhandlebars-0.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyhandlebars-0.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 39625f80dc21d7d9bf9b6f14555167ef0f7e8c1817511579db880d13a4b52d8c
MD5 98abad243f25848c1ba28037a98fa369
BLAKE2b-256 3a126eb837a461126fdbbac48c1d7f2b1b89b002af53db36a020071bcdba82a9

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhandlebars-0.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: ci.yml on sukram42/pyhandlebars

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyhandlebars-0.2.0-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyhandlebars-0.2.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ed28c6c477604825bf51b683770bd1ea40c9ded0f1d3b3eab9f47f1f7a647d53
MD5 08d3ce634eb269f7e0ae7eb26c580968
BLAKE2b-256 e3529d557212d28543660e0b4f41a3c8e8c489ecbb944cb6000bc1acb32e38d3

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhandlebars-0.2.0-cp310-cp310-macosx_11_0_arm64.whl:

Publisher: ci.yml on sukram42/pyhandlebars

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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