Django------The Django template language

  1. Templates------A template is simply a text file. It can generate any text-based format, A template contains variables, which get replaced with values when the template is evaluated. and tags, which control the logic of the template. Below is a minimal template that illustrates a few basics. Each element will be explained later in this document.  

     1 {% extends "base_generic.html" %}
     2 
     3 {% block title %}{{ section.title }}{% endblock %}
     4 
     5 {% block content %}
     6 <h1>{{ section.title }}</h1>
     7 
     8 {% for story in story_list %}
     9 <h2>
    10   <a href="{{ story.get_absolute_url }}">
    11     {{ story.headline|upper }}
    12   </a>
    13 </h2>
    14 <p>{{ story.tease|truncatewords:"100" }}</p>
    15 {% endfor %}
    16 {% endblock %}                                                 
  2. Variables------Variable look like this:{{ variable }}.When the template engine encounters a variable,(模板引擎遇到一个变量) it evaluates that variable and replaces it with the result. Variable names consistof any combination of alphanumeric characters(字母字符) and the underscore(下划线). The dot also appears in variable sections, you cannot have spaces or punctuation characters in variable names(标点符号). Use a dot access attributes of a variable. In the above example,{{section.title}}will be replaced with title attribute of the section object. If use a variable that doesn't exist, the templates system will insert the value of the TEMPLATE_STRING_IF_INVALID setting, which is by default.

  3. Filters------You can modify variables for display by using filters.Filters look like this:{{ name|lower }}.This displays the value of the {{ name }} variable after being filterde through the lower filter, which converts text to lowercase, Use pipe to apply a filter.      Django provides about thirty built-in template filters. e.g.      defalut--If a variable is false or empty, use given default. Otherwise,use the value of the variable  {{ value|default:"nothing"}}  length-- Return the length of the value.This works for both string and lists; {{value|length}}  if value is ['a','b','c','d'],the output will be 4.  striptags--strips all [X]html tags. {{value|striptags}} 

  4. Tags------ Tags look like this {% tag %}.Tags are more complex than variables: Some create text in the output, some control flow by performing loops or logic,and some load external infomation inth the template to be used by later variable. Some tags require beginning and ending tags,here are some of the more commonly used tags:   for-- Loop over each item in an array, to display a list of athletes provided in athlete_list: <ul>{% for athlete in athlete_list %}<li>{{ athlete.name }}</li>{% endfor %}</ul>  if and else-- Evaluates a variable ,and if that variable is "true" the contents of the block are displayed: {% if athlete_list % } number of athletes: {{ athlete_list|length}} {% else %} no athletes.{% endif %} 

posted @ 2012-08-20 15:10  事件轮询,回不到过去  阅读(393)  评论(0编辑  收藏  举报