Templates
A template: HTML and placeholders; displaying a variable, looping, testing, including a fragment, inheriting a layout, and the automatic escaping of values.
- 25 cards
- 5 topics
- Free
The outline, topic by topic
Each topic is weighted the way the exam weighs it.
- Concepts
- Questions
- Sort and link
- Scenarios
- Understand
A few cards from the deck
Template: what is it?
Show the answer
An HTML file holding placeholders and simple instructions, which an engine fills with data to produce a page.
The HTML is written once, the data changes on every request.
<h1>{{ titre }}</h1>gives<h1>Hello</h1>whentitreisHello.The articles are Pen (stock 3), Notebook (stock 0) and Ruler (stock 5). What HTML does this template produce?
```html
<ul>
{% for a in articles %}
{% if a.stock > 0 %}
<li>{{ a.nom }}</li>
{% endif %}
{% endfor %}
</ul>`- Two
lielements, Pen and Ruler - Three
lielements, theiftest only hiding the Notebook on screen - Three
lielements, the Notebook's one left empty - A single
lielement, Pen, becauseifinterrupts the whole loop
Show the answer
Two
lielements, Pen and RulerThe loop goes over all three articles, and the
ifdecides on every turn whether to write the line. The Notebook does not appear at all in the HTML sent, it is not merely hidden.- Two
A template engine fills the … of an HTML document with the data passed to it by the ….
Show the answer
A template engine fills the placeholders of an HTML document with the data passed to it by the controller.
The engine does not fetch the data itself: it receives a dictionary of variables, for example
articlesandtitre, and renders it as HTML.Template inheritance: what is it?
Show the answer
The mechanism by which a template takes over another's layout and only replaces some of its zones.
The header, menu and footer live in a single base template,
{% extends "base.html" %}is enough for the pages. Changing the menu is done in one place.The footer, with the legal notice link, is copied into forty templates. Changing that link takes forty edits. What should be done?
- Build it as HTML in the controller and pass it as a variable to each page
- Keep the copies and edit them with a global text replacement at each change
- Load it in the browser with JavaScript from a separate file
- Put it in a base template that the pages inherit
Show the answer
Put it in a base template that the pages inherit
A shared layout is handled by inheritance: the footer lives once in the base. HTML built in the controller mixes presentation and logic, and would require turning off escaping to display it.