Skip to content
This repository was archived by the owner on Jun 22, 2025. It is now read-only.
This repository was archived by the owner on Jun 22, 2025. It is now read-only.

Implementation of Jinja2 Variables #217

Description

@Nootaku

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:

defjinjaVariables(_static):
definnerFunction(path, jinja_variable=None):
# the decorator recieved a Jinja Variableif"jinja_variables"isnotNone:
response=Noneif'jinja_env'in_start_argsand'jinja_templates'in_start_args:
template_prefix=_start_args['jinja_templates'] +'/'ifpath.startswith(template_prefix):
n=len(template_prefix)
template=_start_args['jinja_env'].get_template(path[n:])
response=btl.HTTPResponse(template.render(jinja_variable))
ifresponseisNone:
response=btl.static_file(path, root=root_path)
_set_response_headers(response)
returnresponse_static(path)
returninnerFunction

The 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 ?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions