Built-in Helpers¶
pydantic_handlebars includes all standard Handlebars.js helpers. Additional utility, comparison, and boolean helpers are available as opt-in extras.
Standard Helpers¶
These 6 helpers are always available — they're included in every HandlebarsEnvironment and in the
module-level render() function.
#if¶
Conditionally renders the block if the value is truthy.
Falsy values: false, None, "", 0, [].
Note: empty dict {} is truthy (matching Handlebars.js behavior).
from pydantic_handlebars import render
print(render('{{#if show}}visible{{else}}hidden{{/if}}', {'show': True}))
#> visible
Supports chained conditions:
#unless¶
The inverse of #if. Renders the block when the value is falsy.
#each¶
Iterates over arrays and objects.
For arrays, provides @index, @first, and @last:
For objects, also provides @key:
Supports {{else}} for empty collections:
Block parameters capture the value and index/key:
Access the parent context with ../:
#with¶
Changes the context for the block body.
Supports {{else}} for falsy contexts:
lookup¶
Dynamic property lookup:
from pydantic_handlebars import render
print(render('{{lookup colors 0}}', {'colors': ['red', 'green', 'blue']}))
#> red
print(render('{{lookup person field}}', {'person': {'name': 'Alice'}, 'field': 'name'}))
#> Alice
log¶
Logs values using Python's warnings.warn:
Extra Helpers¶
The helpers below are not part of the Handlebars.js spec. They must be explicitly enabled:
from pydantic_handlebars import HandlebarsEnvironment
env = HandlebarsEnvironment(extra_helpers=True)
Utility Helpers¶
json¶
Serialize a value to a JSON string:
from pydantic_handlebars import HandlebarsEnvironment
env = HandlebarsEnvironment(extra_helpers=True)
print(env.render('{{{json obj}}}', {'obj': {'a': 1}}))
#> {"a": 1}
uppercase¶
Convert a value to uppercase:
lowercase¶
Convert a value to lowercase:
trim¶
Strip leading and trailing whitespace:
join¶
Join array elements with a separator:
from pydantic_handlebars import HandlebarsEnvironment
env = HandlebarsEnvironment(extra_helpers=True)
print(env.render('{{join colors ", "}}', {'colors': ['red', 'green', 'blue']}))
#> red, green, blue
The default separator is ,.
truncate¶
Truncate a string to N characters:
The default length is 100.
default¶
Use a fallback value when the primary value is falsy:
Comparison Helpers¶
These return boolean values and are typically used inside #if via subexpressions.
eq / ne¶
Equality and inequality:
gt / gte / lt / lte¶
Numeric comparisons:
Boolean Helpers¶
and / or / not¶
Boolean combinators for use in subexpressions:
{{#if (and hasPermission isActive)}}
Allowed
{{/if}}
{{#if (or isAdmin isModerator)}}
Has access
{{/if}}
{{#if (not isDisabled)}}
Enabled
{{/if}}
and and or accept multiple arguments: