jinja2-neverundefined


Namejinja2-neverundefined JSON
Version 0.6.0 PyPI version JSON
download
home_pagehttps://github.com/james4ever0/jinja_neverundefined
SummaryJinja2 extension that never undefined
upload_time2024-01-05 05:43:35
maintainer
docs_urlNone
authorJames Brown
requires_python
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Jinja: NeverUndefined

End the lawless era of Jinja.

`NeverUndefined` is more restrictive than `StrictUndefined`. See [this issue](https://github.com/pallets/jinja/issues/1923).

## Usage

1. Install the package:

    ```bash
    pip3 install jinja2_neverundefined
    ```

2. Create a template file named `template.j2` with the following content:

    ```jinja
    {% macro test(a, b, c) %}
    a macro with three parameters but not used
    {%endmacro%}

    {{test()}}
    ```

3. Create a Python script named `example.py` with the following content:

    ```python
    from jinja2 import Environment, FileSystemLoader
    from jinja2_neverundefined import NeverUndefined

    # Create Jinja2 environment with the extension
    env = Environment(
        loader=FileSystemLoader('.'),
        undefined=NeverUndefined
    )

    # Render the template
    template = env.get_template('template.j2')
    rendered = template.render()

    # Print the rendered output
    print(rendered)
    ```

4. Run the script:

    ```bash
    python3 example.py
    ```

The script will raise an exception because parameters `a b c` are not provided when calling macro `test` in the template.

Take another example:

```jinja
{% macro mtest(a, b, c) %}
append undefined parameters to list
{% set s = [] %}
{% do s.append(a) %}
{{ s }}
{% endmacro %}
{{ mtest() }}
```

`StrictUndefined` will not throw error. It will produce:

```
append undefined parameters to list
[Undefined]
```

However, `NeverUndefined` will:

```
  File "template.j2", line 9, in top-level template code
    {{ mtest() }}
      └ <Macro 'mtest'>
  File "/usr/local/lib/python3.9/dist-packages/jinja2/runtime.py", line 777, in _invoke
    rv = self._func(*arguments)
         │           └ [missing, missing, missing]
         └ <Macro 'mtest'>
  File "template.j2", line 2, in template
    {% macro mtest(a, b, c) %}
  File "/usr/local/lib/python3.9/dist-packages/jinja2_neverundefined/__init__.py", line 14, in __init__
    raise Exception(info)
                    └ "parameter 'a' was not provided"
Exception: parameter 'a' was not provided
```

By using the `NeverUndefined` extension, the Jinja environment will raise an exception when encountering an undefined variable. This helps catch potential issues or missing variable bindings in your templates.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/james4ever0/jinja_neverundefined",
    "name": "jinja2-neverundefined",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "James Brown",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/d0/57/f562d967ed17308269f69583c8db0bd0ebbca56df02a41be9a2602f08636/jinja2_neverundefined-0.6.0.tar.gz",
    "platform": null,
    "description": "# Jinja: NeverUndefined\n\nEnd the lawless era of Jinja.\n\n`NeverUndefined` is more restrictive than `StrictUndefined`. See [this issue](https://github.com/pallets/jinja/issues/1923).\n\n## Usage\n\n1. Install the package:\n\n    ```bash\n    pip3 install jinja2_neverundefined\n    ```\n\n2. Create a template file named `template.j2` with the following content:\n\n    ```jinja\n    {% macro test(a, b, c) %}\n    a macro with three parameters but not used\n    {%endmacro%}\n\n    {{test()}}\n    ```\n\n3. Create a Python script named `example.py` with the following content:\n\n    ```python\n    from jinja2 import Environment, FileSystemLoader\n    from jinja2_neverundefined import NeverUndefined\n\n    # Create Jinja2 environment with the extension\n    env = Environment(\n        loader=FileSystemLoader('.'),\n        undefined=NeverUndefined\n    )\n\n    # Render the template\n    template = env.get_template('template.j2')\n    rendered = template.render()\n\n    # Print the rendered output\n    print(rendered)\n    ```\n\n4. Run the script:\n\n    ```bash\n    python3 example.py\n    ```\n\nThe script will raise an exception because parameters `a b c` are not provided when calling macro `test` in the template.\n\nTake another example:\n\n```jinja\n{% macro mtest(a, b, c) %}\nappend undefined parameters to list\n{% set s = [] %}\n{% do s.append(a) %}\n{{ s }}\n{% endmacro %}\n{{ mtest() }}\n```\n\n`StrictUndefined` will not throw error. It will produce:\n\n```\nappend undefined parameters to list\n[Undefined]\n```\n\nHowever, `NeverUndefined` will:\n\n```\n  File \"template.j2\", line 9, in top-level template code\n    {{ mtest() }}\n      \u2514 <Macro 'mtest'>\n  File \"/usr/local/lib/python3.9/dist-packages/jinja2/runtime.py\", line 777, in _invoke\n    rv = self._func(*arguments)\n         \u2502           \u2514 [missing, missing, missing]\n         \u2514 <Macro 'mtest'>\n  File \"template.j2\", line 2, in template\n    {% macro mtest(a, b, c) %}\n  File \"/usr/local/lib/python3.9/dist-packages/jinja2_neverundefined/__init__.py\", line 14, in __init__\n    raise Exception(info)\n                    \u2514 \"parameter 'a' was not provided\"\nException: parameter 'a' was not provided\n```\n\nBy using the `NeverUndefined` extension, the Jinja environment will raise an exception when encountering an undefined variable. This helps catch potential issues or missing variable bindings in your templates.\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Jinja2 extension that never undefined",
    "version": "0.6.0",
    "project_urls": {
        "Homepage": "https://github.com/james4ever0/jinja_neverundefined"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d057f562d967ed17308269f69583c8db0bd0ebbca56df02a41be9a2602f08636",
                "md5": "33117c75425ca5dd9759b4ef76c3de0a",
                "sha256": "16201ed940e86fe42f1312559e47a26d5490d9aa4dc2f03a52544da57e5ae781"
            },
            "downloads": -1,
            "filename": "jinja2_neverundefined-0.6.0.tar.gz",
            "has_sig": false,
            "md5_digest": "33117c75425ca5dd9759b4ef76c3de0a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 2732,
            "upload_time": "2024-01-05T05:43:35",
            "upload_time_iso_8601": "2024-01-05T05:43:35.409217Z",
            "url": "https://files.pythonhosted.org/packages/d0/57/f562d967ed17308269f69583c8db0bd0ebbca56df02a41be9a2602f08636/jinja2_neverundefined-0.6.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-01-05 05:43:35",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "james4ever0",
    "github_project": "jinja_neverundefined",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "jinja2-neverundefined"
}
        
Elapsed time: 0.17603s