Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Render template from string #72

Closed
azimovMichael opened this issue Apr 19, 2022 · 7 comments
Closed

[Feature Request] Render template from string #72

azimovMichael opened this issue Apr 19, 2022 · 7 comments
Assignees

Comments

@azimovMichael
Copy link
Contributor

Is your feature request related to a problem? Please describe your use case.
I'm migrating from Flask to Sanic, and currently I'm using Flask's render_template_string to render a jinja template from a string.
If I understand correctly, Sanic only supports rendering from a file.

Describe the solution you'd like
I would like something like this:

TEAMPLATE = '''
<!DOCTYPE html>
<html>
<head>
  <style>
    
  </style>
</head>
<body>
  <script>
    
  </script>
</body>
</html>'''

rendered = render_from_string(TEMPLATE, context)
@azimovMichael
Copy link
Contributor Author

I will add a PR for this

@prryplatypus
Copy link
Member

prryplatypus commented Apr 21, 2022

Looking at this... I think all that's necessary is returning the render_template_string result with Sanic's html method? So basically creating a decorator that does just that?

from sanic import html

TEMPLATE = '''
<html>
<body>
  ...
</body>
</html>'''

rendered = render_from_string(TEMPLATE, context)

return html(rendered)

@azimovMichael
Copy link
Contributor Author

@prryplatypus Not sure I follow. We don't have a render_from_string function yet, do we?
It should be similar to the current render func, but instead of using environment.get_template it should use environment.from_string.

Once we'll have this function, I'm not sure why we need to use the html instead of returning HTTPResponse like the render function does

@ahopkins
Copy link
Member

ahopkins commented Apr 24, 2022

@azimovMichael You are correct, we do not have that function yet. Your strategy is fine. html is just a convenience wrapper. You can just use HTTPResponse here as well.

The question that I have is does it make sense to have render and render_from_string?

Why not something like this:

def render(template_name="", template="", ...):
   if template_name and template:
       raise SomeException

render("my_template_name")  # backwards compat, keep this as first arg
render(template="<html>...")

@azimovMichael
Copy link
Contributor Author

@ahopkins I wanted to provide a similar API to what Flask and Jinja itself provide, but sure we can do it in a single function.

@ahopkins
Copy link
Member

ahopkins commented Apr 24, 2022

That makes sense. I personally don't like polluting the namespace so much 🤷‍♂️. I don't work on jinja much so this is helpful for me to know.

What if we provide both? I'd even go so far as to make it a keyword only argument on render.

@azimovMichael
Copy link
Contributor Author

I'm actually totally fine with providing all in a single function.
Although Jijna does it in 2 functions, I actually prefer it to be in one.
Will do it with a kwarg-only arg.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants