jinja2utils


Namejinja2utils JSON
Version 0.1.1 PyPI version JSON
download
home_pagehttps://github.com/vd2org/jinja2utils
SummaryCollection of utils for jinja2
upload_time2024-02-26 12:56:25
maintainer
docs_urlNone
author
requires_python<3.13,>=3.8
licenseCopyright 2017-2024 Vd <jinja2utils@vd2.org> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords jinja2 jinja2-template jinja2-extention template t_factory plural pluralize elapsed remaining uchar unicode
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # jinja2utils

Useful utils for jinja2

## Install

```bash
pip install jinja2utils
```

## t_factory

t_factory is a small tool which helps prefixed templates easily.

See examples below.

#### Example:

- ./templates/en/home/welcome

```text
Welcome, {{username}}!
```

- ./templates/en/home/goodbye

```text
Welcome, {{username}}!
```

- ./templates/ru/home/welcome

```text
Welcome, {{username}}!
```

- ./templates/ru/home/goodbye

```text
Goodbye, {{username}}!
```

- ./main.py

```python
from jinja2 import Environment, PrefixLoader, FileSystemLoader
from jinja2utils import t_factory

loader = PrefixLoader({
    'en': FileSystemLoader('./templates/en'),
    'ru': FileSystemLoader('./templates/ru'),
})

jinja = Environment(loader=loader)
get_t = t_factory(jinja)


def print_templates(t, username):
    rendered1 = t('home/welcome', username=username)
    print(rendered1)

    rendered2 = t('home/goodbye', username=username)
    print(rendered2)


print_templates(get_t('en'), 'John Doe')
# Expected output:
# Welcome, John Doe!
# Goodbye, John Doe!

print_templates(get_t('ru'), 'Иван')
# Expected output:
# Привет, Иван!
# Пока, Иван!
``` 

## plural

`plural` is jinja2 filter function for easy text pluralization.

#### Example:

- ./templates/info/users

```text
System have {{users}} active {{users|plural('en','user','users')}}.
```

```python
# main.py
from jinja2 import Environment, FileSystemLoader
from jinja2utils import plural

jinja = Environment(loader=FileSystemLoader('./templates'))
jinja.filters['plural'] = plural

template1 = jinja.get_template('info/users')
rendered1 = template1.render(users=1)
print(rendered1)  # System have 1 active user.
rendered2 = template1.render(users=23)
print(rendered2)  # System have 23 active users.
``` 

## elapsed and remaining

Calculates and formats elapsed time to string like this:
`25d 4h 3m 35s`. Can be used as jinja2 filter.

#### Example:

- ./templates/info

```text
System uptime: {{started|elapsed(show_seconds=True)}}.
``` 

- ./templates/newyear

```text
System uptime: {{started|elapsed(show_seconds=True)}}.
```

```python
# main.py
from jinja2 import Environment, FileSystemLoader
from jinja2utils import elapsed, remaining
import datetime

jinja = Environment(loader=FileSystemLoader('./templates'))
jinja.filters['elapsed'] = elapsed
jinja.filters['remaining'] = remaining

started = datetime.datetime.now()
newyear = datetime.datetime(2025, 1, 1, 0, 0, 0)

username = 'John Doe'
template1 = jinja.get_template('info/uptime')
rendered1 = template1.render(started=started)
print(rendered1)  # System uptime: 25d 4h 3m 35s.

template2 = jinja.get_template('info/newyear')
rendered2 = template2.render(newyear=newyear)
print(rendered2)  # To next year remaining 295d 10h 13m 10s!
``` 

## uchar

Simple jinja2 function to insert unicode characters
by unicode names. Very useful for inserting emoji.

#### Example:

- ./templates/info/users

```text
Welcome, {{username}} {{UN('THUMBS UP SIGN')}}!
```

```python
# main.py
from jinja2 import Environment, FileSystemLoader
from jinja2utils import uchar

jinja = Environment(loader=FileSystemLoader('./templates'))
jinja.globals['UN'] = uchar

template1 = jinja.get_template('info/users')
rendered1 = template1.render(username='John Doe')
print(rendered1)  # Welcome, John Doe 👍!
``` 


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/vd2org/jinja2utils",
    "name": "jinja2utils",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "<3.13,>=3.8",
    "maintainer_email": "",
    "keywords": "jinja2 jinja2-template jinja2-extention template t_factory plural pluralize elapsed remaining uchar unicode",
    "author": "",
    "author_email": "vd <jinja2utils@vd2.org>",
    "download_url": "https://files.pythonhosted.org/packages/a7/84/d6c404bad6765f313d103d387694fac5e983c35c4362351a624878bdadb1/jinja2utils-0.1.1.tar.gz",
    "platform": null,
    "description": "# jinja2utils\n\nUseful utils for jinja2\n\n## Install\n\n```bash\npip install jinja2utils\n```\n\n## t_factory\n\nt_factory is a small tool which helps prefixed templates easily.\n\nSee examples below.\n\n#### Example:\n\n- ./templates/en/home/welcome\n\n```text\nWelcome, {{username}}!\n```\n\n- ./templates/en/home/goodbye\n\n```text\nWelcome, {{username}}!\n```\n\n- ./templates/ru/home/welcome\n\n```text\nWelcome, {{username}}!\n```\n\n- ./templates/ru/home/goodbye\n\n```text\nGoodbye, {{username}}!\n```\n\n- ./main.py\n\n```python\nfrom jinja2 import Environment, PrefixLoader, FileSystemLoader\nfrom jinja2utils import t_factory\n\nloader = PrefixLoader({\n    'en': FileSystemLoader('./templates/en'),\n    'ru': FileSystemLoader('./templates/ru'),\n})\n\njinja = Environment(loader=loader)\nget_t = t_factory(jinja)\n\n\ndef print_templates(t, username):\n    rendered1 = t('home/welcome', username=username)\n    print(rendered1)\n\n    rendered2 = t('home/goodbye', username=username)\n    print(rendered2)\n\n\nprint_templates(get_t('en'), 'John Doe')\n# Expected output:\n# Welcome, John Doe!\n# Goodbye, John Doe!\n\nprint_templates(get_t('ru'), '\u0418\u0432\u0430\u043d')\n# Expected output:\n# \u041f\u0440\u0438\u0432\u0435\u0442, \u0418\u0432\u0430\u043d!\n# \u041f\u043e\u043a\u0430, \u0418\u0432\u0430\u043d!\n``` \n\n## plural\n\n`plural` is jinja2 filter function for easy text pluralization.\n\n#### Example:\n\n- ./templates/info/users\n\n```text\nSystem have {{users}} active {{users|plural('en','user','users')}}.\n```\n\n```python\n# main.py\nfrom jinja2 import Environment, FileSystemLoader\nfrom jinja2utils import plural\n\njinja = Environment(loader=FileSystemLoader('./templates'))\njinja.filters['plural'] = plural\n\ntemplate1 = jinja.get_template('info/users')\nrendered1 = template1.render(users=1)\nprint(rendered1)  # System have 1 active user.\nrendered2 = template1.render(users=23)\nprint(rendered2)  # System have 23 active users.\n``` \n\n## elapsed and remaining\n\nCalculates and formats elapsed time to string like this:\n`25d 4h 3m 35s`. Can be used as jinja2 filter.\n\n#### Example:\n\n- ./templates/info\n\n```text\nSystem uptime: {{started|elapsed(show_seconds=True)}}.\n``` \n\n- ./templates/newyear\n\n```text\nSystem uptime: {{started|elapsed(show_seconds=True)}}.\n```\n\n```python\n# main.py\nfrom jinja2 import Environment, FileSystemLoader\nfrom jinja2utils import elapsed, remaining\nimport datetime\n\njinja = Environment(loader=FileSystemLoader('./templates'))\njinja.filters['elapsed'] = elapsed\njinja.filters['remaining'] = remaining\n\nstarted = datetime.datetime.now()\nnewyear = datetime.datetime(2025, 1, 1, 0, 0, 0)\n\nusername = 'John Doe'\ntemplate1 = jinja.get_template('info/uptime')\nrendered1 = template1.render(started=started)\nprint(rendered1)  # System uptime: 25d 4h 3m 35s.\n\ntemplate2 = jinja.get_template('info/newyear')\nrendered2 = template2.render(newyear=newyear)\nprint(rendered2)  # To next year remaining 295d 10h 13m 10s!\n``` \n\n## uchar\n\nSimple jinja2 function to insert unicode characters\nby unicode names. Very useful for inserting emoji.\n\n#### Example:\n\n- ./templates/info/users\n\n```text\nWelcome, {{username}} {{UN('THUMBS UP SIGN')}}!\n```\n\n```python\n# main.py\nfrom jinja2 import Environment, FileSystemLoader\nfrom jinja2utils import uchar\n\njinja = Environment(loader=FileSystemLoader('./templates'))\njinja.globals['UN'] = uchar\n\ntemplate1 = jinja.get_template('info/users')\nrendered1 = template1.render(username='John Doe')\nprint(rendered1)  # Welcome, John Doe \ud83d\udc4d!\n``` \n\n",
    "bugtrack_url": null,
    "license": "Copyright 2017-2024 Vd <jinja2utils@vd2.org>\n        \n        Permission is hereby granted, free of charge, to any person obtaining a copy of\n        this software and associated documentation files (the \"Software\"), to deal in\n        the Software without restriction, including without limitation the rights to\n        use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n        the Software, and to permit persons to whom the Software is furnished to do so,\n        subject to the following conditions:\n        \n        The above copyright notice and this permission notice shall be included in all\n        copies or substantial portions of the Software.\n        \n        THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\n        FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\n        COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\n        IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\n        CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
    "summary": "Collection of utils for jinja2",
    "version": "0.1.1",
    "project_urls": {
        "Homepage": "https://github.com/vd2org/jinja2utils",
        "Issues": "https://github.com/vd2org/jinja2utils/issues",
        "Repository": "https://github.com/vd2org/jinja2utils.git"
    },
    "split_keywords": [
        "jinja2",
        "jinja2-template",
        "jinja2-extention",
        "template",
        "t_factory",
        "plural",
        "pluralize",
        "elapsed",
        "remaining",
        "uchar",
        "unicode"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "790f4b667a62749e829872d066a6e7e083b12bb673d41a9cf0c082e936077737",
                "md5": "bbf374f56065003fe9d33ae69dabca98",
                "sha256": "9fad0e7394bc936bb0f0dfb206f428f7762393dd382873ac608633db24c0b9b0"
            },
            "downloads": -1,
            "filename": "jinja2utils-0.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "bbf374f56065003fe9d33ae69dabca98",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<3.13,>=3.8",
            "size": 7114,
            "upload_time": "2024-02-26T12:56:24",
            "upload_time_iso_8601": "2024-02-26T12:56:24.631080Z",
            "url": "https://files.pythonhosted.org/packages/79/0f/4b667a62749e829872d066a6e7e083b12bb673d41a9cf0c082e936077737/jinja2utils-0.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a784d6c404bad6765f313d103d387694fac5e983c35c4362351a624878bdadb1",
                "md5": "f4da8ee885dd49a6273fcdec68b1b6d6",
                "sha256": "6ef8454b35761585986dbc6255781abb9988296abda8cbd288e601d08cda958a"
            },
            "downloads": -1,
            "filename": "jinja2utils-0.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "f4da8ee885dd49a6273fcdec68b1b6d6",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<3.13,>=3.8",
            "size": 6131,
            "upload_time": "2024-02-26T12:56:25",
            "upload_time_iso_8601": "2024-02-26T12:56:25.806499Z",
            "url": "https://files.pythonhosted.org/packages/a7/84/d6c404bad6765f313d103d387694fac5e983c35c4362351a624878bdadb1/jinja2utils-0.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-26 12:56:25",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "vd2org",
    "github_project": "jinja2utils",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "jinja2utils"
}
        
Elapsed time: 0.19132s