跳转到主要内容

Django模板分析器 - 从Django模板中提取模板节点

项目描述

django-template-analyzer

https://github.com/edoburu/django-template-analyzer/actions/workflows/tests.yaml/badge.svg?branch=master https://img.shields.io/pypi/v/django-template-analyzer.svg https://img.shields.io/badge/wheel-yes-green.svg https://img.shields.io/codecov/c/github/edoburu/django-template-analyzer/master.svg

template_analyzer 包提供了一个API来分析Django模板结构。它可以用来查找特定类型的节点,例如自动检测占位符标签。

支持的功能

扫描器可以在各种情况下找到标签,包括

  • 扩展节点

  • 包含节点

  • 用新定义覆盖的块

  • 带有或不带有 {{ block.super }} 的块

  • 重新组织块

  • 在扩展模板中忽略块外的节点

  • 处理多级超包含

返回的节点按自然顺序提供,就像它们在输出页面上出现一样。

虽然Django提供了template.nodelist.get_nodes_of_type()函数,但此函数不会产生相同的结果。

API示例

from django.template.loader import get_template
from mycms.templatetags.placeholdertags import Placeholder
from template_analyzer.djangoanalyzer import get_node_instances

# Load a Django template
template = get_template("mycms/default-page.html")

# Find all tags in the template:
placeholders = get_node_instances(template, Placeholder)

# Read information from the template tag themselves:
# (this is an example, accessing a custom method on the Placeholder object)
placeholder_names = [p.get_name() for p in placeholders]

安装

首先安装模块,最好是在虚拟环境中。可以从PyPI安装

pip install django-template-analyzer

或者直接在当前文件夹中安装

pip install .

鸣谢

  • 此包基于Django CMS的工作Django CMS

  • 非常感谢Django CMS中cms/utils/placeholder.py / cms/utils/plugins.py的贡献者!

下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

django-template-analyzer-2.1.tar.gz (9.5 kB 查看哈希值)

上传时间 源代码

构建分布

django_template_analyzer-2.1-py3-none-any.whl (10.9 kB 查看哈希值)

上传时间 Python 3

由以下组织支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面