Adding jinja template dynamically

此生再无相见时 提交于 2020-01-04 05:56:18

问题


I have a jinja template that is the only content inside a set of div tags.

<div id="section0">
    {% include 'temppage.html' %}
</div>

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.


回答1:


As glls said, replacing the content can be used with,

document.getElementById("section0").innerHTML = "something";

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' %}`;

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.




回答2:


You could use a JS framework (such as Angular, React...) in order to achieve this...I am assuming you are trying to build a single page app?

Otherwise, you will have to rely more on Javascript in order to change the HTML under you div depending on what you click. For example, if you have button 1, 2, 3. Each rendering a different HTML template upon clicking.

Example (using jQuery):

$(document).on('click', '.some-class', function() {
    document.getElementById("section0").innerHTML = "something";
});

fyi: "something" can be an html structure.



来源:https://stackoverflow.com/questions/39049852/adding-jinja-template-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!