Change Jinja Child Template in response to HTML ev

2019-08-06 00:39发布

Suppose I have a base template base.html:

<!DOCTYPE html>
<html>
<body>
    <div id="content">
        {% block content %}
        {% endblock %}
    </div>
</body>
</html>

And I have several possible child templates for the content block, say child1.html, child2.html etc.

Is it possible to switch between them in response to an HTML event on the page, say using Javascript? (My motivation is to let the user click on buttons on the page to change the page style.)

I am using Jinja2 with Python on Google App Engine.

Apparently it seems that the template is set by server side scripts so this should not be possible, but I was hoping someone must have wanted to do something similar before.

1条回答
Luminary・发光体
2楼-- · 2019-08-06 01:40

You noted properly, that the jinja2 template is rendered server side while JavaScript runs in your browser.

So JavaScript has no direct chance to affect how is the template rendered.

There could be some solutions, e.g.

Generate both versions of content

Generate them hidden, let your JavaScript show just one of them

AJAX - pull proper content from JavaScript

JavaScript would detect what is to be inside and using AJAX call would pull what is needed.

查看更多
登录 后发表回答