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

Improve embedding html and templates #44

Open
radiac opened this issue Oct 27, 2024 · 0 comments
Open

Improve embedding html and templates #44

radiac opened this issue Oct 27, 2024 · 0 comments
Assignees

Comments

@radiac
Copy link
Owner

radiac commented Oct 27, 2024

Two related issues:

  • I'm finding often want to embed html pages in the file alongside the code. Currently the only way is to define it in a constant at the top, or put it in the return value in the view - both of which obscure/bloat the code.
  • I'm finding I often want to render and return template strings, and although the boilerplate for that isn't particularly cumbersome, it is something I usually have to look up.

I think these can be addressed in one solution:

  • Defer html/template definition - set them at the bottom of the file by attaching to the app (an object already known in the view)
  • Do it in a way we can identify convert should write it into an actual html template file
  • View functions can render those templates
  • Ideally other templates can also refer to those in extends and include tags

Suggested syntax:

app = Django()
@app.route("/")
def index(request):
    return app.render("index.html", context=...)

app.templates["index.html"] = "{% extends 'base.html' %}{% block content %}Hello {{ target }}{% endblock %}"

Build this using or on top of django.template.loaders.locmem.Loader, and add this as the priority first loader in settings

Here app.render would be a wrapper for django.shortcuts.render, which saves an import and inserts the current request object - I think that should be available in the app. If not, maybe importing django.shortcuts.render would make more sense.

When we run convert, it can then write these templates out into text files, and rewrite app.render into a straight call to render. It will also need to remove our loader from the settings.

@radiac radiac moved this to In progress in radiac's open source tasks Oct 27, 2024
@radiac radiac self-assigned this Oct 27, 2024
radiac added a commit that referenced this issue Dec 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In progress
Development

No branches or pull requests

1 participant