动态添加Jinja模板 [英] Adding jinja template dynamically
问题描述
我有一个Jinja模板,这是一组div标签中唯一的内容.
I have a jinja template that is the only content inside a set of div tags.
<div id="section0">
{% include 'temppage.html' %}
</div>
当我按下按钮时,我想用其他东西替换标签之间的所有内容.我希望将其替换为另一个Jinja模板"{%include'realpage.html'%}",但是首先我不确定如何替换整个部分,而不是仅仅替换一个单词.其次,我什至可以动态添加一个Jinja模板,还是需要直接用一个包含文件内容的字符串替换它.
When I press a button, I want to replace everything between the tags with something else. I was hoping to replace it with another jinja template, "{% include 'realpage.html' %}", but first I am unsure of how to replace the entire section, instead of just replacing a single word. Second, can I even add a jinja template dynamically, or do I need replace it with a string with the contents of the file directly.
推荐答案
正如gls所说,替换内容可以与
As glls said, replacing the content can be used with,
document.getElementById("section0").innerHTML = "something";
要动态添加一个jinja模板,您需要用想要的jinja模板的多行字符串替换innerHTML,并与反引号`"一起使用.看起来像是
As for adding a jinja template dynamically, you need to replace the innerHTML with a multi-line string of the wanted jinja template, with is used with backticks, "`". So it would look like,
document.getElementById("section0").innerHTML = `{% include 'realpage.html' %}`;
模板在页面加载时执行(据我所知这是不可避免的),因此在检查实时页面的html时,多行字符串将包含您要包含的文件中的所有内容.
The template is executed when the page loads (which is unavoidable as far as I'm aware), so when inspecting the html of the live page, the multi-line string will contain whatever is in the file you are including.
这篇关于动态添加Jinja模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!