Flask-Admin 管理系统模板 - 菜单和图标生成
这是一个使用 Flask-Admin 构建管理系统的模板文件。模板文件使用 Jinja2 语言编写,包含了多个宏和块,用于生成管理系统界面的各个部分。其中:
menu宏用于生成菜单栏,包括菜单项和子菜单,可以根据传入的参数指定菜单栏的根节点;menu_icon宏用于生成菜单项的图标,根据菜单项的类型和图标值生成不同类型的图标,包括glyph、fa、image和image-url;menu_links宏用于生成菜单栏下方的链接,可以根据传入的参数指定链接列表;messages宏用于生成页面上方的消息提示框,包括不同类型的消息,如成功、错误、警告等;body块用于生成页面的主体部分,可以根据需要添加自定义内容。
在这个特定的模板文件中,body 块包含了一个简单的欢迎页面,用于展示管理系统的主界面。
以下是模板文件的代码片段:
{% macro menu_icon(item) -%}
{% set icon_type = item.get_icon_type() %}
{%- if icon_type %}
{% set icon_value = item.get_icon_value() %}
{% if icon_type == 'glyph' %}
<i class="glyphicon {{ icon_value }}"></i>
{% elif icon_type == 'fa' %}
<i class="fa {{ icon_value }}"></i>
{% elif icon_type == 'image' %}
<img src="{{ url_for('static', filename=icon_value) }}" alt="menu image">
{% elif icon_type == 'image-url' %}
<img src="{{ icon_value }}" alt="menu image">
{% endif %}
{% endif %}
{%- endmacro %}
{% macro menu(menu_root=None) %}
{% if menu_root is none %}{% set menu_root = admin_view.admin.menu() %}{% endif %}
{%- for item in menu_root %}
{%- if item.is_category() -%}
{% set children = item.get_children() %}
{%- if children %}
{% set class_name = item.get_class_name() or '' %}
{%- if item.is_active(admin_view) %}
<li class="active dropdown{% if class_name %} {{class_name}}{% endif %}">
{% else -%}
<li class="dropdown{% if class_name %} {{class_name}}{% endif %}">
{%- endif %}
<a class="dropdown-toggle" data-toggle="dropdown" href="javascript:void(0)">
{% if item.class_name %}<span class="{{ item.class_name }}"></span> {% endif %}
{{ menu_icon(item) }}{{ item.name }}
{%- if 'dropdown-submenu' in class_name -%}
<i class="glyphicon glyphicon-chevron-right small"></i>
{%- else -%}
<i class="glyphicon glyphicon-chevron-down small"></i>
{%- endif -%}
</a>
<ul class="dropdown-menu">
{%- for child in children -%}
{%- if child.is_category() -%}
{{ menu(menu_root=[child]) }}
{% else %}
{% set class_name = child.get_class_name() %}
{%- if child.is_active(admin_view) %}
<li class="active{% if class_name %} {{class_name}}{% endif %}">
{% else %}
<li{% if class_name %} class="{{class_name}}"{% endif %}>
{%- endif %}
<a href="{{ child.get_url() }}"{% if child.target %} target="{{ child.target }}"{% endif %}>
{{ menu_icon(child) }}{{ child.name }}</a>
</li>
{%- endif %}
{%- endfor %}
</ul>
</li>
{% endif %}
{%- else %}
{%- if item.is_accessible() and item.is_visible() -%}
{% set class_name = item.get_class_name() %}
{%- if item.is_active(admin_view) %}
<li class="active{% if class_name %} {{class_name}}{% endif %}">
{%- else %}
<li{% if class_name %} class="{{class_name}}"{% endif %}>
{%- endif %}
<a href="{{ item.get_url() }}"{% if item.target %} target="{{ item.target }}"{% endif %}>{{ menu_icon(item) }}{{ item.name }}</a>
</li>
{%- endif -%}
{% endif -%}
{% endfor %}
{% endmacro %}
{% macro menu_links(links=None) %}
{% if links is none %}{% set links = admin_view.admin.menu_links() %}{% endif %}
{% for item in links %}
{% set class_name = item.get_class_name() %}
{% if item.is_accessible() and item.is_visible() %}
<li{% if class_name %} class="{{ class_name }}"{% endif %}>
<a href="{{ item.get_url() }}">{{ menu_icon(item) }}{{ item.name }}</a>
</li>
{% endif %}
{% endfor %}
{% endmacro %}
{% macro messages() %}
{% with messages = get_flashed_messages(with_categories=True) %}
{% if messages %}
{% for category, m in messages %}
{% if category %}
{# alert-error changed to alert-danger in bootstrap 3, mapping is for backwards compatibility #}
{% set mapping = {'message': 'info', 'error': 'danger'} %}
<div class="alert alert-{{ mapping.get(category, category) }} alert-dismissable">
{% else %}
<div class="alert alert-dismissable">
{% endif %}
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
{{ m }}
</div>
{% endfor %}
{% endif %}
{% endwith %}
{% endmacro %}
该模板文件定义了多个宏,用于生成管理系统界面的不同部分:
menu_icon宏用于生成菜单项的图标,根据菜单项的类型和图标值生成不同类型的图标,包括glyph、fa、image和image-url。menu宏用于生成菜单栏,包括菜单项和子菜单,可以根据传入的参数指定菜单栏的根节点。menu_links宏用于生成菜单栏下方的链接,可以根据传入的参数指定链接列表。messages宏用于生成页面上方的消息提示框,包括不同类型的消息,如成功、错误、警告等。
模板文件还定义了一个 body 块,用于生成页面的主体部分,可以根据需要添加自定义内容。
这个特定的模板文件中,body 块包含了一个简单的欢迎页面,用于展示管理系统的主界面。
{% extends 'admin/master.html' %}
{% block body %}
<div>
<h2>欢迎进入管理系统</h2>
<a href="/index">返回主页</a>
</div>
{% endblock %}
这个模板文件可以帮助开发者快速构建一个管理系统,并提供灵活的定制选项。
原文地址: https://www.cveoy.top/t/topic/jzcY 著作权归作者所有。请勿转载和采集!