使用include来动态指向HTML [英] Using include to dynamically point to HTML

查看:408
本文介绍了使用include来动态指向HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想指出一个基于变量的不同HTML文件。我使用的格式如下:

I want to point to a different HTML files based on a variable. I am using include in the following format:

{% include 'templates/case/{{cid}}/intro.html' %}

这会引发错误:

This throws an error:

TemplateNotFound: templates/case/{{cid}}/intro.html

看着这个我知道Jinja2在运行时不能解析变量。 cid = ABC (ABC是路径中的文件夹名称)的值,所以我预计总路径为:

Looking at this I know Jinja2 does not resolve the variable at runtime. The value of cid = ABC (ABC is a folder's name in the path), so I expected the total path to be:

templates/case/ABC/intro.html

如果我直接在include中使用这个解析路径,它就可以工作。

If I use this resolved path directly in include it works.

我该如何解决这个问题?

How can I resolve this?

推荐答案

至少在Jinja2 2.7.1这样做:

At least in Jinja2 2.7.1 this works:

{% include 'templates/case/%s/intro.html' % cid %}

这篇关于使用include来动态指向HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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