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. Default Global Client

Every Template created without an explicit client= argument shares a single process-wide global client. This means partials and helpers registered on one template are immediately available to all other templates that use the default client.

from pyhandlebars import Template

# Both templates share the global client — the partial is visible to t2.
_ = Template("Hi {{name}}", name="greeting")
t = Template("{{> greeting}}")
print(t.format({"name": "Bob"}))  # Hi Bob

4. 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.

Each helper receives two arguments: params (a list of positional arguments from the template call) and context (the full data object passed to format).

Option A — PyHandlebars.helper on the global client

Access helper on the class (not an instance) to register directly on the global client — no explicit PyHandlebars() needed.

from pyhandlebars import PyHandlebars, Template

@PyHandlebars.helper
def shout(params: list, context: dict):
    return f"{params[0].upper()} from {context['location']}"

# Supply a custom template name with name=
@PyHandlebars.helper(name="whisper")
def my_whisper_fn(params: list, context: dict):
    return params[0].lower()

t = Template("{{shout name}} / {{whisper name}}")
assert t.format({"name": "Alice", "location": "Wonderland"}) == "ALICE from Wonderland / alice"

Option B — register_helper / @client.helper() on a dedicated client

Use an explicit PyHandlebars() instance when you want helpers isolated from the global client.

from pyhandlebars import PyHandlebars, Template

def shout(params: list, context: dict):
    return f"{params[0].upper()} from {context['location']}"

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

# Decorator style:
@client.helper()
def whisper(params: list, context: dict):
    return params[0].lower()

t = Template("{{shout name}} / {{whisper name}}", client=client)
assert t.format({"name": "Alice", "location": "Wonderland"}) == "ALICE from Wonderland / alice"

5. 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.2.tar.gz (100.6 kB 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.2-cp313-cp313-win_amd64.whl (413.2 kB view details)

Uploaded CPython 3.13Windows x86-64

pyhandlebars-0.2.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (582.4 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

pyhandlebars-0.2.2-cp313-cp313-macosx_11_0_arm64.whl (513.5 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pyhandlebars-0.2.2-cp312-cp312-win_amd64.whl (413.3 kB view details)

Uploaded CPython 3.12Windows x86-64

pyhandlebars-0.2.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (583.0 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

pyhandlebars-0.2.2-cp312-cp312-macosx_11_0_arm64.whl (513.5 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

pyhandlebars-0.2.2-cp311-cp311-win_amd64.whl (414.5 kB view details)

Uploaded CPython 3.11Windows x86-64

pyhandlebars-0.2.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (584.6 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

pyhandlebars-0.2.2-cp311-cp311-macosx_11_0_arm64.whl (515.8 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

pyhandlebars-0.2.2-cp310-cp310-win_amd64.whl (414.4 kB view details)

Uploaded CPython 3.10Windows x86-64

pyhandlebars-0.2.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (584.7 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

pyhandlebars-0.2.2-cp310-cp310-macosx_11_0_arm64.whl (516.0 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: pyhandlebars-0.2.2.tar.gz
  • Upload date:
  • Size: 100.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pyhandlebars-0.2.2.tar.gz
Algorithm Hash digest
SHA256 2c38da4d1ac1227104f2df90e9b72a6a1e98557aebe01ba97a57e9ff6f6645c7
MD5 83a3431bdd3c8ae3b0a2596e729fba87
BLAKE2b-256 2be6a78e83577370bb6084eea1b1b801ca2e635ac00c499f4c9fee8b0d340ef2

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhandlebars-0.2.2.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.2-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for pyhandlebars-0.2.2-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 0a5bf0981dd5d2dd07e6fd7610c4a24887925d08d61e60a7ed0088c3d99dac5b
MD5 c60853b8ceaaeab7589d0d81e2912095
BLAKE2b-256 d7531163d276b9d9ede5704950256cba7bf4e9320672338dd6209fa49bd33ca7

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhandlebars-0.2.2-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.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyhandlebars-0.2.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 fb6bf59f6230e7ca94036e3b959dae8737afddf4fb0cb95c08a37217ce9cdacb
MD5 e5a7d238478c49b38970d21b5f8bb538
BLAKE2b-256 835564e19110bd075098fa5d7d2e22693a023de6b858080564f4bb1a5ce6bd05

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhandlebars-0.2.2-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.2-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyhandlebars-0.2.2-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2e6ff0f8bfbac69c0c10938182390da2ed51a647140c2823d03f7f64e4b8d5c2
MD5 54cb61a8ccc38d34c178d75ac0b60254
BLAKE2b-256 24a340ddd169117ce49e4ba5b6bba87ef677d8432e374fb280fac17ac14d5c7d

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhandlebars-0.2.2-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.2-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for pyhandlebars-0.2.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 c03b780130afca3ce14b21d33aab5f9d0708b7e954f54f8a8af63f0ef1f8520b
MD5 836555ef5682b08c26a1bcf3ba62c71d
BLAKE2b-256 bdd74b933abf3d971a797b96e496078251b77a3d7675f79d00581fc87edebe92

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhandlebars-0.2.2-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.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyhandlebars-0.2.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bda43d933b06403359c81526639a7836b0fbf6cf737cf87131482151df368bf9
MD5 e16030b5b9ec665c01accb6863ffe361
BLAKE2b-256 faa576478c7e1fcf69aed218909a87825755e387b8289757950f353d5050f2e5

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhandlebars-0.2.2-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.2-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyhandlebars-0.2.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d278a3e1fbbf94117de56646de1dbccea96999bf68509d3d251e62e45c38ae69
MD5 1c7974d0d59c642a8f008f1be5a2fc5f
BLAKE2b-256 b27d79b281c158e1854b1b237bc1b3159a6f55e6a2e8839c55b4180be7c4aaff

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhandlebars-0.2.2-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.2-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for pyhandlebars-0.2.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 a43e8a4741169be7bc09be31cfa7655ad461be0ca99503482d1dd441e838fd10
MD5 66f6a8e63e8d62a502437e2798becc7f
BLAKE2b-256 11130e521c6ae40e30c1b0b681f42169b4e11937746fa5f8bb27126fcf2bdb66

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhandlebars-0.2.2-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.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyhandlebars-0.2.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bbaea55cc3acf887ea219ee0768b4532b587c1d914df89406136775e8e59185b
MD5 d2f70c44ca8bfbc1bc3be9543b505039
BLAKE2b-256 c4ea9ec798afc1eaaa8f6c92e4342f19383fdef59a45450259754ef3bf3d523b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhandlebars-0.2.2-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.2-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyhandlebars-0.2.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7324929215b4787a1173e3790e979a0c38271834555629134ed541bad89644d9
MD5 ee2d8221ad1a5cb68cf21bc9a138ff63
BLAKE2b-256 8a4b27b72cdb92bb40c6d78767390ad6e690372d7fdf9be8e36208a768781f69

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhandlebars-0.2.2-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.2-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for pyhandlebars-0.2.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 ba2c4244d9844984a9e620367932f13ba1aec2a58f3098f8065f4649f71a7b9b
MD5 ebc03955b8864fde562f3d66ac040fd1
BLAKE2b-256 54409977f5f4355703251e920c0b40b6fddd16e091395e87b56dd056d97face4

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhandlebars-0.2.2-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.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyhandlebars-0.2.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4701b048bc384e0d3014c20dd366fb626e865f60df373f39dc8788ce2cda0017
MD5 d26ff2d0faf6ad6da1b6a9bbe3c67eea
BLAKE2b-256 61f9591098df0a30daf8686799bfa5fe5f9f288070dc2be2525650836ff7d93d

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhandlebars-0.2.2-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.2-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyhandlebars-0.2.2-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6e89f6ff906961befe56ce186873ba20aabd4c6e0e0eae85808ed31fa91a1b75
MD5 0e31f6db9018e5f56786aab677a682f5
BLAKE2b-256 62ce55431575ed4d44b54f0687dee368eace9d3f54a7719dc5e3469f943a827f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyhandlebars-0.2.2-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