🦋
Butterfly 用户手册
  • Introduction
  • 一 前言
  • 二 开始
    • 安装部署
    • 五分钟体验指南
    • 单机使用手册
    • 应用规范
      • handler specs
      • middleware specs
      • xingqiao_plugin specs
      • yiqiu_program specs
  • 三 客户端功能
    • MySQL 原生协议
    • MySQL ORM
    • Redis 原生协议
      • redis_config
      • redis_tls
    • Redis ORM
    • Redis mcpack
    • Localcache
    • Kazoo
  • 四 应用(通用服务)
    • API JSON 规范
    • 异步任务 BaiChuan(百川)
    • 任务调度 RuQi(如期)
    • 任务编排 XingQiao(星桥)
    • 配置管理 WuXing(五行)
    • 运筹决策 BaiCe(百策)
  • 五 部署运维
    • 单机容器化部署
    • 监控
    • 异常排查
      • CPU Load spike every 7 hours
    • 升级
    • 安全
    • 其他
  • 六 前端
    • butterfly_template
    • butterfly_fe
    • butterfly-admin(json2web)
      • amis
      • sso
      • pangu
    • NoahV
    • PyWebIO
  • 七 潘多拉魔盒
    • 装饰器
      • localcache_decorator
      • retry_decorator
      • custom_decorator
      • command2http_decorator
    • 算法
      • 算法-分位数
      • 算法-变异系数
    • 实用工具
      • host_util
      • shell_util
      • http_util
      • time_util
      • random_util
      • concurrent
      • jsonschema
      • blinker
      • toml
      • command_util
      • config_util
      • picobox
      • 对称加密
        • des
        • aes
      • ascii_art
        • ttable
        • chart
      • business_rules
      • python-mysql-replication
      • dict_util
    • 中间件
      • middleware_status
      • middleware_whitelist
    • test_handler.py
  • 八 最佳实践
    • 分布式架构
    • Code practice
    • Log practice
    • Daemon process
  • 附录
Powered by GitBook
On this page
  • 1 模板语法
  • 1.1 数据替换
  • 1.2 控制流语句
  • 1.3 注释
  • 2 渲染前端
  • 2.1 模板文本
  • 2.2 渲染模板
  • 3 常见问题
  • 3.1 模板中的 key 没有被渲染
  1. 六 前端

butterfly_template

后端模板(butterfly 自带)

1 模板语法

1.1 数据替换

双大括号内包含 Python 表达式,并且可以通过管道符"|"处理生成的文本

<p>Short name: {{story.subject|slugify|lower}}</p>

1.2 控制流语句

if

{% if user.is_logged_in %}
    <p>Welcome, {{ user.name }}!</p>
{% endif %}

for

<ul>
{% for product in product_list %}
    <li>{{ product.name }}: {{ product.price|format_price }}</li>
{% endfor %}
</ul>

1.3 注释

{# This is the best template ever! #}

2 渲染前端

2.1 模板文本

一般放在 <butterfly_project>/templates 下

<p>Welcome, {{user_name}}!</p>
<p>Products:</p>
<ul>
{% for product in product_list %}
    <li>{{ product.name }}:
        {{ product.price|format_price }}</li>
{% endfor %}
</ul>
  • 文本

  • 注释:{# ... #}

  • 数据替换:{{ ... }},如:{{user_name}},{{product.name}}

  • 控制结构:{% ... %}

2.2 渲染模板

from collections import namedtuple

from xlib import template

def  demo(req):
    with open("./templates/host_unit.tpl", "r") as f:
        template_text = f.read()

    Product = namedtuple("Product",["name", "price"])
    product_list = [Product("Apple", 1), Product("Fig", 1.5), Product("Pomegranate", 3.25)]

    def format_price(price):
        return "$%.2f" % price

    # 解析模板
    t = template.Templite(template_text, {"user_name":"Charlie", "product_list":product_list}, {"format_price":format_price})
    # 渲染模板
    print t.render()

if __name__ == "__main__":
    demo()

3 常见问题

3.1 模板中的 key 没有被渲染

此时会抛出异常

Traceback (most recent call last):
  ...
  File "<string>", line 2, in render_function
KeyError: 'xxx'
Previous六 前端Nextbutterfly_fe

Last updated 13 days ago