-
Notifications
You must be signed in to change notification settings - Fork 609
Implementation of Jinja2 Variables #217
Description
First of all : Eel is amazing ! This is a project I'd love to participate with.
One of the features I find lacking is the implementation of Jinja Variables. While the documentation and the options allow for inserting and using templates (with the jinja_templates argument in the start() function), I did not see any possibility to transfer variables within the templates.
It would be nice if this could be included either as a decorator or in a more "flask-like" fashion in the return of a function.
One solution that I tried to fiddle with but to no avail, was to create a decorator for the _static function (in the __init__.py) that seems to handle Jinja's template.render() function.
Here's what I got so far:
def jinjaVariables(_static):
def innerFunction(path, jinja_variable=None):
# the decorator recieved a Jinja Variable
if "jinja_variables" is not None:
response = None
if 'jinja_env' in _start_args and 'jinja_templates' in _start_args:
template_prefix = _start_args['jinja_templates'] + '/'
if path.startswith(template_prefix):
n = len(template_prefix)
template = _start_args['jinja_env'].get_template(path[n:])
response = btl.HTTPResponse(template.render(jinja_variable))
if response is None:
response = btl.static_file(path, root=root_path)
_set_response_headers(response)
return response
_static(path)
return innerFunctionThe idea was to create a global dictionary variable called jinja_variable and to update that variable depending on the page you want to visit.
If you were to include the jinja_variable in the kwargs of the start() function, wouldn't it be possible to dynamically change the output of the html page by accessing the required keys ?