美文网首页
模板系统

模板系统

作者: 蝉时雨丶 | 来源:发表于2020-06-10 09:47 被阅读0次

模板

Django项目可以配置一个或多个模板引擎(或者如果不使用模板,甚至为零)。Django后端内置一个自己的模板系统,创造性地称为Django template language(DTL),和一个流行的替代品JICAN2*。后端也可以使用第三方提供其他可用的模板语言。

由于历史原因,模板引擎的通用支持和Django模板语言的实现都存在于django.template 模块的命名空间中。

模板引擎的支持

配置

Templates engines are configured with the TEMPLATES setting. It's a list of configurations, one for each engine. The default value is empty. The settings.py generated by the startproject command defines a more useful value:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            # ... some options here ...
        },
    },
]

BACKEND is a dotted Python path to a template engine class implementing Django's template backend API. The built-in backends are django.template.backends.django.DjangoTemplates and django.template.backends.jinja2.Jinja2.

Since most engines load templates from files, the top-level configuration for each engine contains two common settings:

  • DIRS defines a list of directories where the engine should look for template source files, in search order.
  • APP_DIRS tells whether the engine should look for templates inside installed applications. Each backend defines a conventional name for the subdirectory inside applications where its templates should be stored.

While uncommon, it's possible to configure several instances of the same backend with different options. In that case you should define a unique NAME for each engine.

OPTIONS contains backend-specific settings.

The Django template language

Syntax

A Django template is a text document or a Python string marked-up using the Django template language. Some constructs are recognized and interpreted by the template engine. The main ones are variables and tags.

A template is rendered with a context. Rendering replaces variables with their values, which are looked up in the context, and executes tags. Everything else is output as is.

The syntax of the Django template language involves four constructs.

变量

A variable outputs a value from the context, which is a dict-like object mapping keys to values.

Variables are surrounded by {{ and }} like this:

My first name is {{ first_name }}. My last name is {{ last_name }}.

With a context of {'first_name': 'John', 'last_name': 'Doe'}, this template renders to:

My first name is John. My last name is Doe.

Dictionary lookup, attribute lookup and list-index lookups are implemented with a dot notation:

{{ my_dict.key }}
{{ my_object.attribute }}
{{ my_list.0 }}

If a variable resolves to a callable, the template system will call it with no arguments and use its result instead of the callable.

标签(Tags)

Tags provide arbitrary logic in the rendering process.

This definition is deliberately vague. For example, a tag can output content, serve as a control structure e.g. an "if" statement or a "for" loop, grab content from a database, or even enable access to other template tags.

Tags are surrounded by {% and %} like this:

{% csrf_token %}

Most tags accept arguments:

{% cycle 'odd' 'even' %}

Some tags require beginning and ending tags:

{% if user.is_authenticated %}Hello, {{ user.username }}.{% endif %}

A reference of built-in tags is available as well as instructions for writing custom tags.

过滤器

Filters transform the values of variables and tag arguments.

They look like this:

{{ django|title }}

With a context of {'django': 'the web framework for perfectionists with deadlines'}, this template renders to:

The Web Framework For Perfectionists With Deadlines

Some filters take an argument:

{{ my_date|date:"Y-m-d" }}

A reference of built-in filters is available as well as instructions for writing custom filters.

注释(Comments)

Comments look like this:

{# this won't be rendered #}

A {% comment %} tag provides multi-line comments.

相关文章

  • 圆梦打假

    【圆梦系统】2020核心目标打假模板【圆梦系统】核心目标打假模板——————————————————————【打假...

  • Xcode自定义文件模板

    系统模板文件所在位置 系统模板在/Applications/Xcode.app/Contents/Develope...

  • 各种记忆

    一、定制系统错误页面模板 系统默认的错误模板位于:ThinkPHP/Tpl/think_exception.tpl...

  • 定制错误为404页面

    一、定制系统错误页面模板系统默认的错误模板位于:ThinkPHP/Tpl/think_exception.tpl ...

  • 相关业务描述

    一,审批流 系统描述:审批流是个独立的系统,有个审批模板,模板跟系统的企业id有关,审批流发起的时,模板类型是个枚...

  • 模板系统

    模板 Django项目可以配置一个或多个模板引擎(或者如果不使用模板,甚至为零)。Django后端内置一个自己的模...

  • 模板系统

    转自webpack-handbook 现状 伴随着移动互联的大潮,当今越来越多的网站已经从网页模式进化到了 Web...

  • 铝合金模板十大优势

    铝模板,全称为建筑用铝合金模板系统。是近几年发展势头迅猛的一种新型模板支撑系统。在讲求环保效益的今天,铝模板的优势...

  • ansible-playbook 04 模板when用法

    ansible-playbook模板的使用-when 需求 系统版本不同,根据系统版本使用对应的nginx模板 具...

  • 2018-08-08 模板

    DTL文件 Django文件中的模板系统主要是DTL模板语言,比普通HTML功能更为强大。 模板查找路径配置 模板...

网友评论

      本文标题:模板系统

      本文链接:https://www.haomeiwen.com/subject/mrvjtktx.html