Registry / serialization / jinja2-strcase

jinja2-strcase

JSON →
library0.0.2pypypi✓ verified 21d ago

jinja2-strcase is a Python package that provides a set of filters for converting string cases within Jinja2 templates, including common formats like snake_case, kebab-case, and camelCase. It is currently at version 0.0.2 and is a port of the Go `strcase` package. The package has an 'Alpha' development status, indicating it is not yet stable for production use.

pip install jinja2-strcase
INSTALL
IMPORT
SIG · JINJA2-STRCASE
J
jinja2-strcase
serializationpythonv0.0.2
Install
1.8s avg
Import
149ms
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.0.2 · 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.95 runs
installs and imports cleanly · install 0.0s · import 0.152s · 18.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.8s · import 0.146s · 19MB
17MB installed
● package 17MB
Code
Verified usage

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

StrcaseExtension
from jinja2_strcase import StrcaseExtension
This is the primary extension to enable the case conversion filters in Jinja2.
Environment
from jinja2 import Environment
Required from the core Jinja2 library to create a templating environment.

This quickstart demonstrates how to initialize a Jinja2 environment with the `StrcaseExtension` and then use various case conversion filters (e.g., `to_snake`, `to_screaming_snake`, `to_camel`) on strings within a template.

from jinja2 import Environment from jinja2_strcase import StrcaseExtension # Create a Jinja2 environment and load the StrcaseExtension env = Environment(extensions=[StrcaseExtension]) # Define a template using the 'to_snake' filter template = env.from_string("{{ 'Any kind of string' | to_snake }}") print(f"to_snake: {template.render()}") # Example with another filter: to_screaming_snake template_screaming_snake = env.from_string("{{ 'Another Test String' | to_screaming_snake }}") print(f"to_screaming_snake: {template_screaming_snake.render()}") # Example with to_camel template_camel = env.from_string("{{ 'this is a camel case' | to_camel }}") print(f"to_camel: {template_camel.render()}")
Debug
Known issues
breakingThe package is currently in 'Development Status :: 3 - Alpha'. This means the API might change significantly in future versions without maintaining backward compatibility.
fix
Review the project's GitHub repository for any changes before upgrading and test thoroughly.
affects: 0.0.1, 0.0.2
gotchaJinja2 extensions, including jinja2-strcase, need to be explicitly loaded into the Jinja2 Environment via the `extensions` parameter. Forgetting this step will result in template filter not found errors.
fix
Ensure `extensions=['jinja2_strcase.StrcaseExtension']` is passed to the `jinja2.Environment` constructor.
affects: All
gotchaWhen using Jinja2 templates, if a variable or attribute does not exist, it will return an undefined value. Operations on undefined values, or attempting to iterate over non-iterable objects, can lead to `NoneType` errors.
fix
Always check if a variable is defined or iterable using `{% if variable is defined %}` or `{% if variable is iterable %}` before operating on it. Use default values where appropriate, e.g., `{{ variable | default('fallback_value') }}`.
affects: All
gotchaComparing different data types (e.g., string and integer) directly in Jinja2 templates without proper type conversion can lead to runtime errors.
fix
Use Jinja2 filters like `|int` or `|float` to convert variables to the desired numeric type before comparison, e.g., `{% if '3'|int > 0 %}`.
affects: All
Errors
Common errors & fixes
jinja2.exceptions.UndefinedError: 'to_snake' is undefined
The 'jinja2-strcase' extension, `StrcaseExtension`, was not loaded into the Jinja2 environment, causing the case conversion filters to be unavailable.
fix
Initialize your Jinja2 Environment by explicitly passing `extensions=['jinja2_strcase.StrcaseExtension']` to its constructor.
```python
from jinja2 import Environment
from jinja2_strcase import StrcaseExtension

env = Environment(extensions=[StrcaseExtension])
template = env.from_string("{{ 'HelloWorld' | to_snake }}")
print(template.render())
```
ModuleNotFoundError: No module named 'jinja2_strcase'
The `jinja2-strcase` package is not installed or not accessible in the current Python environment.
fix
Install the package using pip in your active Python environment:
```bash
pip install jinja2-strcase
```
jinja2.exceptions.UndefinedError: 'None' has no attribute 'to_snake'
A Jinja2 filter, such as `to_snake`, is being applied to a variable that evaluates to `None` or an undefined value, and Jinja2 attempts to access an attribute on this `None` object.
fix
Ensure the variable is defined and is a string before applying the filter, using Jinja2's `default` filter or conditional statements.
```jinja2
{{ my_variable | default('') | to_snake }}
{# or #}
{% if my_variable is defined %}
  {{ my_variable | to_snake }}
{% else %}
  {# Handle undefined case, e.g., print an empty string or a default value #}
  ''
{% endif %}
```
Upgrade
Version history
0.0.2latest on PyPI · released Jun 21, 2020
Audit
Dependencies
Jinja2requiredCore templating engine that this library extends.
Agent activity
5 hits · last 30 days
node
4
Resources
jinja2-strcase — pip install jinja2-strcase · libregistry