Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django学习记录(三):templates模板 #39

Open
PyxYuYu opened this issue Sep 15, 2016 · 0 comments
Open

Django学习记录(三):templates模板 #39

PyxYuYu opened this issue Sep 15, 2016 · 0 comments
Labels

Comments

@PyxYuYu
Copy link
Owner

PyxYuYu commented Sep 15, 2016

Knowing yourself is the height of wisdom.

0x01 Django

  • 模板(templates

    • 模板是一个 string 文本,它用来分离一个文档的展现和数据
    • 模板定义了 placeholder 和表示多种逻辑的 tags 来规定文档如何展现
    • 通常模板用来输出 HTMLDjango 模板也能生成其他基于文本的形式
    • 模板举例:
    <!DOCTYPE html>
    <html>
    <head>
       <title>xxxxx</title>
    </head>
    <body>
    <p>Dear {{ person_name }},</p>
    <p>Thanks for placing an order from {{ company }}. It's scheduled to ship on {{ ship_date|date:"F j, Y" }}.</p>
    <p>Here are the items you've ordered:</p>
    <ul>
    {% for item in item_list %}
    <li>{{ item }}</li>
    {% endfor %}
    </ul>
    {% if order_warranty %}
    <p>Your warranty information will be included in the packaging.</p>
    {% endfor %}
    <p>Sincerely, <br />{{ company }}</p>
    </body>
    </html>
    • 模板本质是 HTML,中间夹杂了一些变量和模板标签

      • {{}} 包围的是 变量
        • 过滤器: {{ship_date|date:"F j, Y"}}ship_date 变量传递给过滤器,并给 date 过滤器传递了一个参数 F j, Y, date 过滤器以给定参数的形式格式化日期,类似于 Unix,过滤器使用管道字符 |
      • 扩展 : Django内建标签和过滤器系统
    • {{%%}} 包围的是 块标签

      • 块标签 的含义很丰富,它告诉模板系统做一些事情
      • 上述例子中,if for 逻辑语句,这样的功能都是 块标签
        • 例如:
      {{ string }}
      
      # coding: utf-8
      from django.shortcuts import render
      
      def home(request):
         string = "xxxxx"
         return render(request, 'xxx.html', {'string': string})
@PyxYuYu PyxYuYu added the Django label Sep 15, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant