You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Problem
Passing variable named name to jinja template using the api.template function's keyword argument returns unexpected behaviors. i.e. The below implementation does not work:
@api.route("/{var}")asyncdeffunc(req, resp, *, var):
resp.content=api.template("page.html", name=var) # using `name` for the keyword to pass variable `name` in jinja template
+
<p>Hello {{ name }}!</p>
I think variable names such as name is very often used, so it may cause errors to others in the future as well.
Cause
Keyword argument for name was a reserved keyword in the api.template function.
By the way, I'm a big fan of the work, and thank you @kennethreitz for encouraging submission of issues for Hacktoberfest! I'm still learning how to code, but it pushed me to make a first ever submission of an issue to an open source project.
The text was updated successfully, but these errors were encountered:
Good job, just a python thing, don't use mutable objects as default arguments (unless you know what you're doing) because python evaluate them once (google it)
So instead of def template(self, name, auto_escape=True, values={}): use
def template(self, name, auto_escape=True, values=None):
if values is None:
values = {}
# check values is a dict instance
Problem
Passing variable named
name
to jinja template using the api.template function's keyword argument returns unexpected behaviors. i.e. The below implementation does not work:+
I think variable names such as
name
is very often used, so it may cause errors to others in the future as well.Cause
Keyword argument for
name
was a reserved keyword in the api.template function.responder/responder/api.py
Line 375 in cee5ca8
Possible solutions
There either could be a disclaimer in the docs about the reserved words, or maybe changing the implementation like
and unpacking the dictionary variables to update the Jinja template so any variable can pass? So the endpoint for html rendering looks like
How do you think?
Full codes and processes to recreate the issue are here
Server file:
Template html file:
Running the server:
Testing the endpoint with the
name
variable returnsnull
:When I use different name such as
greeting
for the variable it works.By the way, I'm a big fan of the work, and thank you @kennethreitz for encouraging submission of issues for Hacktoberfest! I'm still learning how to code, but it pushed me to make a first ever submission of an issue to an open source project.
The text was updated successfully, but these errors were encountered: