前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >flask jinjia模板语法 字典,字符串过滤

flask jinjia模板语法 字典,字符串过滤

作者头像
用户5760343
发布2019-07-08 14:12:44
1.1K0
发布2019-07-08 14:12:44
举报
文章被收录于专栏:sktjsktjsktj
from flask import Flask
 from flask import render_template
app = Flask(name)
 @app.route('/')
 def index():
 return render_template('index.html')
 @app.route('/user/<name>')
 def user(name):
 mydict={'key':'key'}
 return render_template('user.html', name=name,mydict=mydict)
if name == 'main':
 app.run()

<!DOCTYPE html>
 <html lang="en">
 <head>
 <meta charset="UTF-8">
 <title>Title</title>
 </head>
 <body>
 <h1>Hello, {{ name }}!</h1>
 <h2>HELLO,{{ mydict['key']|capitalize }}</h2>
 <h2>HELLO,{{ mydict['key']|upper }}</h2>
 {% if user %}
 Hello, {{ user }}!
 {% else %}
 Hello, Stranger!
 {% endif %}
 <ul>
 {% for comment in comments %}
 <li>{{ comment }}</li>
 {% endfor %}
 </ul>

使用宏

{% macro render_comment(comment) %}
 <li>{{ comment }}</li>
 {% endmacro %}
 <ul>
 {% for comment in comments %}
 {{ render_comment(comment) }}
 {% endfor %}
 </ul>
 </body>
 </html>
{% include 'common.html' %}

block占位符

<html>
 <head>
 {% block head %}
 <title>{% block title %}{% endblock %} - My Application</title>
 {% endblock %}
 </head>
 <body>
 {% block body %}
 {% endblock %}
 </body>
 </html>

extends

{% extends "base.html" %}
 {% block title %}Index{% endblock %}
 {% block head %}
 {{ super() }}
 <style>
 </style>
 {% endblock %}
 {% block body %}
 <h1>Hello, World!</h1>
 {% endblock %}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.06.11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 使用宏
  • block占位符
  • extends
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档