Django模板大纲 [英] Django template outliner

查看:150
本文介绍了Django模板大纲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



主要想法是获取一个模板名称列表作为默认模板加载机械将返回(即尊重 TEMPLATE_DIRS TEMPLATE_LOADERS 等),然后解析模板对于 {%block%} 和{%extends%}标签,以创建树结构。最后使用grapviz进行可视化。



我不知道这是否可行。但是只是为了开始,我如何以我所描述的方式加载模板?



或者也许有类似的事情已经存在?

解决方案

通常,模板通过在线查找,每个可能使用的模板都没有root条目。



因此,如果要在项目中grep所有使用的模板,则需要扫描TEMPLATE_LOADERS中的加载程序可能检查的每个位置,以生成可能的条目以供以后的底部检查。这将是困难的,装载机的一些后端甚至可能不允许按目录操作。

或者您可以解析INSTALLED_APPS中的视图和URL文件以grep模板名称,但这只能用于硬/编码的模板名称更加困难。
另外,可能会从硬编码的字符串中加载模板...



对于给定的模板名称,更容易加载模板并检查节点内部它,就像你的想法一样。您可以检查 django.template.base 以了解其工作原理。



此外,您可以利用django -debug-toolbar显示用于请求的模板。而且,IMO,保持模板结构平坦,简单,因此确定,更容易实现。


I'm trying to obtain a visual representation of the templates of a Django project, as a hierarchy.

The main idea is to get a list of template names as the default template loading machinery would return (ie honoring TEMPLATE_DIRS, TEMPLATE_LOADERS, etc.) and then parsing the templates looking for {% block %} and {% extends %} tags, in order to create a tree structure. Finally use grapviz for the visualization.

I'm not sure if it's a viable approach. But just to start, how can I load the templates the way I described?

Or maybe, does something similar already exist?

解决方案

Normally, templates are looked up by names online, there is no root entry for every template that might be used.

Thus if you want to grep all used templates in a project, you need to scan every place that loaders in TEMPLATE_LOADERS might check, to generate possible entries for later bottom-to-up checking. This would be hard, some backends of loaders might even does not allow fetch-by-directory operation.
Or you could parse views and urls files inside INSTALLED_APPS to grep template names, but this only works w/ hard-coded name of template and even more difficult. Also, there might be templates loaded from hard-coded string...

For a given template name, its easier to load the template and check nodes inside it, just as your idea. You could check django.template.base to know how it works.

Furthermore, you could take advantage of django-debug-toolbar to show the templates used for a request. And, IMO, keeping template structure flat, simple and thus determined, is easier to achieve.

这篇关于Django模板大纲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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