Registry / web-framework / pybars3

pybars3

JSON →
library0.9.7pypypi✓ verified 87d ago

Pybars3 is a Python library that provides a templating system compatible with Handlebars.js, supporting both Python 2 and Python 3. It is a fork of the original `pybars` project, enhancing it with Python 3 compatibility and features from Handlebars.js up to version 2.0. The current version is 0.9.7, released in November 2019, indicating an infrequent release cadence and a status of maintenance.

pip install pybars3
INSTALL
IMPORT
SIG · PYBARS3
P
pybars3
web-frameworkpythonv0.9.7
Install
3.1s avg
Import
1856ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.9.7 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 1.933s · 19.4MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 3.1s · import 1.779s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Compiler
from pybars import Compiler
The main class for compiling Handlebars templates.

This quickstart demonstrates compiling a Handlebars template, registering a custom block helper, defining a partial, and rendering the template with contextual data. Note the use of `u""` for template strings for compatibility, though on Python 3 plain strings are sufficient.

from pybars import Compiler # Get a compiler compiler = Compiler() # Define a helper function def _list_helper(this, options, items): result = ['<ul>'] for item in items: result.append('<li>') result.extend(options['fn'](item)) # Render the inner block result.append('</li>') result.append('</ul>') return result # Register helpers and partials helpers = {'list': _list_helper} header_template = compiler.compile(u'<h1>{{title}}</h1>') partials = {'header': header_template} # Compile the main template source source = u""" {{>header}} <p>Hello, {{name}}!</p> {{#list people}}{{firstName}} {{lastName}}{{/list}} """ template = compiler.compile(source) # Data to render data = { 'title': 'People List', 'name': 'World', 'people': [ {'firstName': 'Yehuda', 'lastName': 'Katz'}, {'firstName': 'Carl', 'lastName': 'Lerche'}, {'firstName': 'Alan', 'lastName': 'Johnson'} ] } # Render the template output = template(data, helpers=helpers, partials=partials) print(output)
Debug
Known issues
gotchaPybars3 does not fully implement all features of Handlebars.js 2.0 or even 1.1. Specific features like `@root`, `_parent`, `../` accessors, `each` helper with `@index`, `@key`, `@first`, `@last`, dynamic partials, and raw blocks are supported, but full compatibility with the latest Handlebars.js features should not be assumed.
fix
Consult the `pybars3` GitHub README for a partial list of supported Handlebars.js features to confirm functionality. If a feature is missing, consider implementing it as a custom helper.
affects: <=0.9.7
gotchaCustom helper functions in Pybars3 have different calling conventions than their JavaScript counterparts. Block helpers must accept `this, options, *args, **kwargs`. Other helpers and closures in the context should accept `this, *args, **kwargs`.
fix
Ensure your Python helper functions adhere to the specified argument signatures: `def my_block_helper(this, options, arg1, kwarg1='val'): ...` for block helpers, and `def my_helper(this, arg1, kwarg1='val'): ...` for other helpers.
affects: All
gotchaWhen passing keyword arguments to helpers (e.g., `{{foo bar quux=1}}`), the keyword names in Handlebars templates must not be Python reserved words. Using a reserved word like `print` as a keyword argument name will result in a Python syntax error.
fix
Avoid using Python reserved keywords as names for keyword arguments in your Handlebars templates when they are processed by Pybars3. Choose alternative names that are valid Python identifiers.
affects: All
gotchaFor efficiency and control over HTML escaping, Pybars3 uses a `strlist` class. When a helper returns a `strlist` instance, its content will NOT be escaped, similar to Handlebars.js `SafeString`. Helpers in inner loops are recommended to return `list` or `strlist` for performance.
fix
Be mindful of the return type of your helpers. If you need to output unescaped HTML, return a `strlist`. For content that should be HTML-escaped by the template engine, return a standard Python string (or `unicode` in Python 2).
affects: All
Errors
Common errors & fixes
PybarsError: Template source must be a unicode string
This error occurs when the template source provided to `compiler.compile()` is not a Unicode string. This is particularly common in Python 2 environments if string literals are not explicitly marked with `u''` or if byte strings are passed. In Python 3, it can happen if byte strings are inadvertently passed.
fix
Ensure your template source string is a Unicode string. In Python 2, use `u"your template here"`. In Python 3, all standard string literals are Unicode, but verify no byte strings (`b""`) are being used as template input.
AttributeError: 'str' object has no attribute 'get'
This error can occur within templates or helpers when attempting to access properties of a context variable that is a simple string, but the template expects an object (like a dictionary or an object with attributes). Older versions of Pybars3 (prior to 0.9.2) had issues where dictionary calls did not always take precedence over attributes, leading to unexpected resolution.
fix
Ensure the data context passed to the template matches the structure expected by the template. If accessing dictionary keys, ensure the context is a dictionary. Upgrade to Pybars3 0.9.2 or later, which improved the precedence of dictionary calls over attributes and `.get()` methods.
TypeError: 'list' object is not callable (when using options['fn'])
This typically happens in a custom block helper when `options['fn']` (which represents the inner block of the Handlebars helper) is not correctly invoked. It might be due to a missing `options` argument in the helper definition or calling `options.fn` instead of `options['fn']`.
fix
Verify that your block helper's signature includes `options` as the second positional argument and that you are correctly invoking the inner block using `options['fn'](scope_for_inner_block)`. For example: `result.extend(options['fn'](item))`.
Upgrade
Version history
0.9.7latest on PyPI · released Nov 5, 2019
Audit
Dependencies

No dependency data recorded yet.

Agent activity
10 hits · last 30 days
node
10
Resources
pybars3 — pip install pybars3 · libregistry