"Gestion simplifiée des fichiers modèles"
Project description
scrippy_template
Simplified management of template files for the Scrippy
framework.
Prerequisites
Python modules
List of required modules
The modules listed below will be automatically installed.
- jinja2
Installation
Manual
git clone https://codeberg.org/scrippy/scrippy-template.git
cd scrippy-template.git
python -m pip install -r requirements.txt
make install
With pip
pipinstall scrippy-template
Usage
This module allows document generation from template files using the jinja2 rendering engine.
To be usable, template files must be located in the directory defined by the base_path
parameter passed to the template.Renderer
object.
To handle variable interpolation, the template file MUST accept a dictionary named params
as a parameter.
This dictionary must contain all the variables necessary for the complete rendering of the template file.
The rendering of the template file is obtained from the template.Renderer
object, whose instantiation requires the name of a template file and the base path in which to search for the template file.
Environment-specific parameters for Jinja2 can be modified through the env
attribute of the template.Renderer
object.
By convention, the template files used by the Scrippy framework are stored in the directory defined by the env::templatedirdir
configuration parameter (see the configuration of the Scrippy framework in the relevant documentation).
A template is a simple text file with certain duly marked passages interpolated by the variables passed as parameters.
The Renderer.render()
method returns the rendering of the template file.
The variables must be supplied to the template file as a dictionary named params
.
The dictionary will be passed to the template file which will be responsible for interpolating the variables it contains.
Template files can include:
- control structures
- loops
Simple template file
With the following template file named template_test.mod located in the directory /var/scrippy/templates
:
Hello {{params.user}}
You received this email because you are a member of the functional administrators of {{params.app}}.
The {{params.script}} script execution on {{params.date}} ended with the following error code:
- {{params.error.code}}: {{params.error.msg}}
--
Regards.
{{params.sender}}
The template file can be used as follows:
import datetime
from scrippy_template import template
params = {"user": "Harry Fink",
"app": "Flying Circus",
"script": "dead_parrot.py",
"date": datetime.datetime.now().strftime("%Y/%m/%d"),
"error": {"code": 42,
"msg": "It’s not pinin’! It’s passed on! This parrot is no more!"},
"sender": "Luiggi Vercotti", }
base_path = '/var/lib/scrippy/templates'
template_file = 'template.j2'
renderer = template.Renderer(base_path, template_file)
print(renderer.render(params))
With the default values, the message displayed at the end of the script will contain:
Hello Harry Fink
You received this email because you are a member of the functional administrators of Flying Circus.
The dead_parrot.py script execution on 2019/09/15 ended with the following error code:
- 42: It’s not pinin’! It’s passed on! This parrot is no more!
--
Regards.
Luigi Vercotti
Template file with control structures
params = {"user": "Harry Fink",
"app": "Flying Circus",
"script": "dead_parrot.py",
"date": datetime.datetime.now().strftime("%Y/%m/%d"),
"num_error": 42,
"sender": "Luiggi Vercotti", }
Hello {{params.user}}
You received this email because you are a member of the functional administrators of {{params.app}}.
The {{params.script}} script execution on {{params.date}} ended
{% if params.num_errors == 0 %}
- without error
{% else %}
with {{params.num_errors}} error(s)
{% endif %}
--
Regards.
{{params.sender}}
Template file with loop
params = {"user": "Harry Fink",
"app": "Flying Circus",
"script": "dead_parrot.py",
"date": datetime.datetime.now().strftime("%Y/%m/%d"),
'errors': [{'code': 2, 'msg': "It's not pinin’! It's passed on! This parrot is no more!"},
{'code': 3, 'msg': "Ohh! The cat's eaten it."}],
"sender": "Luiggi Vercotti", }
Hello {{params.user}}
You received this email because you are a member of the functional administrators of {{params.app}}.
The {{params.script}} script execution on {{params.date}} ended with the following errors:
{% for error in params.errors %}
- {{ error.code }}: {{ error.msg}}
{% endfor %}
--
Regards.
{{params.sender}}
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file scrippy-template-1.1.85.tar.gz
.
File metadata
- Download URL: scrippy-template-1.1.85.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e2275517620f2448852015e0c44f6432fed7e4c5fdfe863d0820da347278f640 |
|
MD5 | d4fa50cb8c2c46cf946803af9c63a2ad |
|
BLAKE2b-256 | 9682570442ff1b0afcee8e3c5517f171bab18872b922f42ac58837600fcb1e07 |
File details
Details for the file scrippy_template-1.1.85-py3-none-any.whl
.
File metadata
- Download URL: scrippy_template-1.1.85-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8cecb7a27f50b7db4b48426629887bfa2f3cb0a3e2217c5e73e365c40d615ebb |
|
MD5 | d0b1fc5f7d46f427edc2293f5c0fc991 |
|
BLAKE2b-256 | 6a6c541ba12ce156681d6b2214290e341980f385eecc5b351f714ef54773afdc |