Chapitre 4

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Framework Django

Chapitre 4: Les templates


A.U 2022-2023 2
Template Engine

A.U 2022-2023 3
Templates

u Les templates ou les gabarits définit la mise en page et le formatage final


envoyé à l’utilisateur
u Syntaxe
u {{output_variable}} : affichage des variables
u {% tag %} : utilization des tags pour un formatage complex
u variable|filter : les filtres permettent de formater des variables individuellement

A.U 2022-2023 5
Django Template Configuration
By default, Django templates are enabled on all Django projects due to the TEMPLATES variable in settings.
py. Listing 3-1 illustrates the default TEMPLATES value in Django projects.
Configuration des templates: settings.py
Listing 3-1. Default Django template configuration in settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

A.U 2022-2023 6
Tags

u {% now %}
u {% csrf_token %} : utiliser pour éviter les scripts cross site, utilisé seulement
dans la balise <form>
u {% if %} {% elif %} {% else %} {% endif %}
u {% for %} {% endfor %}
u {% ifchanged %} {% endifchanged %}
u {% cycle %} {% resetcycle %} {% regroup %}
u {% filter %} {% endfilter %}

A.U 2022-2023 7
filters

u Dates: date, time, timesince, timeuntil


u Strings, Lists et Numbers: add, default, default_if_none, length, length_is,
make_list, yesno
u Numbers: divisibleby, filesizeformat, floatformat, get_digit, phone2numeric
u Strings: capfirst, cut, linenumbers, pluralize, slugify, title, truncatechars,
truncatechars_html, truncatewords, truncatewords_html, upper, wordcount
u Lists et Dictionaries: dictsort, dictsortreversed, join, first, last, random, slice,
unordered_list
u Spacing: addslashes, center, ljust, rjust, escape, escapejs, force_escape

A.U 2022-2023 8
a single template update takes effect on all templates.
Reusable Django templates also allow you to define page blocks to override content on a page-by-
page basis. This process makes a project's templates more modular because you define top-level blocks to
Exemple
establish the overall layout and define content on a page-by-page basis.
Lets take the first step toward building reusable Django templates exploring the Django built-in
{% block %} tag. Listing 3-10 illustrates the first lines of a template called base.html with several
{% block %} tags.
u le tag « block »
Listing 3-10. Django template with {% block %} tags
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>{% block title%}Default title{% endblock title %}</title>
<meta name="description" content="{% block metadescription%}{% endblock metadescription %}">
<meta name="keywords" content="{% block metakeywords%}{% endblock metakeywords %}">

A.U 2022-2023 9
85
Templates réutilisables
CHAPTER 3 ■ DJANGO TEMPLATES

u Il est possible de définir une relation d’héritage entre les templates


Listing 3-12. Django templates use of {{block.super}} with three reusable templates
# base.html template
<p>{% block breadcrumb %}Home{% endblock breadcrumb %}</p>

# index.html template
{% extends "base.html" %}
{% block breadcrumb %}Main{% endblock breadcrumb %}

# detail.html template
{% extends "index.html" %}
{% block breadcrumb %} {{block.super}} : Detail {% endblock breadcrumb %}

The base.html template in Listing 3-12 defines the breadcrumb block with a default value of Home. Next,
the index.html template reuses the base.html template and overrides the breadcrumb block with a value of
A.U 2022-2023 10
Main. Finally, the detail.html template reuses the index.html template and overrides the breadcrumb block
value. However, notice the {{block.super}} statement in the final block override. Since {{block.super}}

You might also like