更改Jinja子模板以响应HTML事件 [英] Change Jinja Child Template in response to HTML event

查看:66
本文介绍了更改Jinja子模板以响应HTML事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个基本模板 base.html

Suppose I have a base template base.html:

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

我为内容块,例如 child1.html child2.html 等。

是否可以响应页面上的HTML事件(例如使用Javascript)在它们之间切换? (我的动机是让用户单击页面上的按钮来更改页面样式。)

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.)

我正在Google App Engine上使用带有Python的Jinja2。

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.

推荐答案

您正确地指出,jinja2模板是在浏览器中运行JavaScript时呈现在服务器端的。

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

因此JavaScript没有直接机会来影响模板的呈现方式。

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

可能会有一些解决方案,例如

There could be some solutions, e.g.

隐藏它们,让您的JavaScript仅显示其中之一

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

JavaScript会检测到内部内容,而使用AJAX调用会提取所需内容。

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

这篇关于更改Jinja子模板以响应HTML事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆