Skip to content

Commit e6cfacf

Browse files
s2bAlagtslinawolf
authored
[DOC] Rewrite Fluid introduction chapter (#587)
* [DOC] Rewrite Fluid introduction chapter * Update Documentation/Concepts/Fluid/Index.rst Co-authored-by: Sandra Erbel <[email protected]> * Add anchors to headlines --------- Co-authored-by: Sandra Erbel <[email protected]> Co-authored-by: Lina Wolf <[email protected]>
1 parent 78c5dee commit e6cfacf

File tree

1 file changed

+139
-111
lines changed

1 file changed

+139
-111
lines changed

Documentation/Concepts/Fluid/Index.rst

Lines changed: 139 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -7,159 +7,187 @@
77
Fluid templates
88
===============
99

10-
.. todo: This chapter was moved from the site package tutorial:
11-
Overhaul and improve
12-
10+
.. contents::
11+
:caption: Content on this page
12+
1313
.. _quick-introduction-to-fluid:
1414

1515
Quick Introduction to Fluid
1616
===========================
1717

18-
Like many other templating engines, Fluid reads *template files*,
19-
processes them and replaces certain variables and specific tags with dynamic
20-
content. The result is a fully working website with a clean and valid HTML
21-
output. Dynamic elements are automatically updated as required. Navigation
22-
menus are a typical example for this type of content. A menu exists on all
23-
pages across the entire website. Whenever pages are added, deleted or renamed,
24-
the menu items change.
25-
26-
Fluid takes modern templating a step further. By using *ViewHelpers*,
27-
developers can implement complex functionality and therefore extend the
28-
original functionality of Fluid to their heart's content. ViewHelpers are built
29-
in the programming language PHP. Having said that, website integrators or
30-
editors are not required to learn or understand these (this is the
31-
responsibility of a software developer). Integrators only need to **apply**
32-
them -- and this is as easy as adding an HTML tag such as :html:`<image.../>` to an
33-
HTML file.
34-
35-
More than 80 ViewHelpers are shipped with the TYPO3 core already. They enable
36-
integrators and web developers to use translations of variables, generate forms
37-
and dynamic links, resize images, embed other HTML files and even implement
38-
logical functions such as :html:`if ... then ... else ...`. An overview of the
39-
available ViewHelpers and how to apply them can be found in the
40-
:doc:`Fluid ViewHelper reference <t3viewhelper:Index>`.
18+
TYPO3 uses a template engine to generate the necessary HTML
19+
markup for a website. The template engine provides data from
20+
the backend, such as content or menu structures, which can
21+
then be formatted with HTML.
22+
23+
TYPO3's template engine of choice is called Fluid. Fluid's
24+
syntax is inspired by the syntax of HTML or XML documents,
25+
so it should already be familiar for users that already know
26+
HTML.
27+
28+
While Fluid is extendable by PHP developers, knowing any
29+
PHP is not necessary to write templates for TYPO3. However,
30+
Fluid comes with its own syntax rules, which need to be
31+
obeyed when writing templates.
32+
33+
.. _fluid-basics:
34+
35+
Fluid Basics
36+
============
37+
38+
.. _fluid-basics-accessing-variables:
39+
40+
Accessing Variables
41+
-------------------
42+
43+
An integral part of Fluid are variables. This is the data
44+
that is provided by the backend to be used inside your
45+
template. You can access variables like this:
46+
47+
.. code-block:: xml
48+
49+
<h1>{myHeadline}</h1>
50+
51+
52+
If a variable contains subitems, these can be accessed
53+
with the dot syntax:
54+
55+
.. code-block:: xml
56+
57+
<p>{myVariable.mySubItem}</p>
58+
59+
.. _fluid-basics-modifying-variables:
60+
61+
Modifying Variables
62+
-------------------
63+
64+
You can also do some basic math operations:
65+
66+
.. code-block:: xml
67+
68+
{myNumber + 5}
69+
70+
71+
If you need to modify the provided data even more, you
72+
can use so-called ViewHelpers. These are functions that
73+
take some input values, perform operations on those
74+
values and then output the result. The following example
75+
converts a variable to uppercase:
76+
77+
.. code-block:: xml
78+
79+
<f:format.case mode="upper">{myText}</f:format.case>
80+
81+
If you want to perform multiple operations on one variable
82+
or if your templates become more complex, you might also
83+
want to use :ref:`Fluid's inline notation <t3coreapi:fluid-inline-notation>`.
84+
85+
.. _fluid-basics-control-structures:
86+
87+
Using Control Structures
88+
------------------------
89+
90+
ViewHelpers are also used for so-called control structures.
91+
If you want to add a condition to your template, you can
92+
use the :ref:`If ViewHelper <f:if> <t3viewhelper:typo3fluid-fluid-if>`:
93+
94+
.. code-block:: xml
95+
96+
<f:if condition="{myVariable} == 'hello'">
97+
The variable is "hello".
98+
</f:if>
99+
100+
You can also use the :ref:`For ViewHelper <f:for> <t3viewhelper:typo3fluid-fluid-for>` to loop over an
101+
array:
102+
103+
.. code-block:: xml
104+
105+
<ul>
106+
<f:for each="{myList}" as="myItem">
107+
<li>This item is {myItem}.</li>
108+
</f:for>
109+
</ul>
41110
42111
43112
.. _ft-directory-structure:
44113

45114
Directory structure
46115
===================
47116

48-
Fluid requires a specific directory structure to store the template files. If
49-
you are working through this tutorial now, this is a perfect time to create the
50-
first set of folders of the site package extension. The initial directory can
51-
be named :file:`site_package/`, which we assume is located on your local
52-
machine. You can also choose a different name such as "site_example" or
53-
"site_clientname", but this tutorial uses "site_package".
54-
55-
The aforementioned folders for Fluid are all located as sub-directories of a
56-
folder called :file:`Resources/`. Therefore, create the directory structure as
57-
listed below.
117+
Nowadays, Fluid templates in TYPO3 are always part of an
118+
extension. As they are neither PHP code nor configuration files
119+
and don't need to be accessed by end users, they are placed in the
120+
`Resources/Private/` subfolder.
58121

59122
.. directory-tree::
60-
:level: 4
123+
:level: 6
61124
:show-file-icons: true
62125

63-
* EXT:my_sitepackage
126+
* my_sitepackage
64127

65128
* Resources
66129

67130
* Private
68131

69-
* Language
70-
71132
* Templates
72133

73134
* Layouts
74135

136+
* DefaultLayout.html
137+
75138
* Pages
76139

140+
* MyPage.html
141+
77142
* Partials
78143

79-
* Public
144+
* MyPartial.html
80145

81-
* Css
82146

83-
* Images
147+
The displayed folder structure is the convention for the location
148+
of template files in a sitepackage extension.
149+
However, be aware that these paths might vary slightly between
150+
projects or extensions as they can be configured individually.
84151

85-
* JavaScript
152+
.. _fluid-templates-folders-under-private:
86153

87-
* StaticTemplate
154+
Templates, Layouts and Partials
155+
-------------------------------
88156

89-
The :file:`Public/` directory branch is self-explanatory: it contains folders
90-
such as :file:`Css/`, :file:`Images/`, :file:`JavaScript/` and
91-
:file:`StaticTemplate/`. All files in these folders will be delivered to the
92-
user (website visitors) *as they are*. These are **static** files which are not
93-
modified by TYPO3 at all before they are sent to the user.
157+
.. uml::
158+
:caption: Fluid template structure
94159

95-
The :file:`Private/` directory with its two sub-folders :file:`Language/` and
96-
:file:`Templates/` in contrast, requires some explanation.
160+
frame layout as "Templates/Layouts/DefaultLayout.html" {
161+
frame page as "Templates/Pages/MyPage.html" {
162+
rectangle partial [
163+
<b>Templates/Partials/MyPartial.html</b>
164+
(reusable code snippet)
165+
]
166+
}
167+
}
97168

98-
.. _fluid-templates-folders-under-private:
99169

100-
Folders under 'Private/'
101-
------------------------
170+
Fluid knows three different types of template files: **Templates**,
171+
**Layouts** and **Partials**. Templates are always the starting point
172+
and thus are always required, while both Partials and Layouts are optional.
173+
However, these can be very helpful to improve the structure of
174+
your templates and can avoid duplicate code, which makes
175+
maintenance much easier.
176+
177+
**Layouts** are a flexible method to reuse HTML markup that should wrap
178+
around multiple template files. You could for example extract your
179+
header and footer markup to a layout file and only keep the content
180+
in-between in your template. Layouts automatically have access to all
181+
variables defined within the template.
102182

103-
.. _fluid-templates-folders-under-private-templates-layouts:
104-
105-
Templates/Layouts
106-
~~~~~~~~~~~~~~~~~
107-
HTML files, which build the overall *layout* of the website, are stored in the
108-
:file:`Layouts/` folder. Typically this is only **one** construct for all
109-
pages across the entire website. Pages can have different layouts of course,
110-
but *page layouts* do not belong into the :file:`Layouts/` directory. They are
111-
stored in the :file:`Templates/Pages/` directory (see below). In other words, the
112-
:file:`Layouts/` directory should contain the global layout for the entire website
113-
with elements which appear on all pages (e.g. the company logo, navigation
114-
menu, footer area, etc.). This is the skeleton of your website.
115-
116-
.. _fluid-templates-folders-under-private-templates-pages:
117-
118-
Templates/Pages
119-
~~~~~~~~~~~~~~~
120-
The most important fact about HTML files in the :file:`Templates/Pages` directory
121-
has been described above already: this folder contains layouts, which are page-
122-
specific. Due to the fact that a website usually consists of a number of pages
123-
and some pages possibly show a different layout than others (e.g. number of
124-
columns), the :file:`Templates/Pages/` directory may contain one or multiple HTML files.
125-
126-
.. _fluid-templates-folders-under-private-templates-partials:
127-
128-
Templates/Partials
129-
~~~~~~~~~~~~~~~~~~
130-
The directory called :file:`Partials/` may contain small
131-
snippets of HTML template files. "Partials" are similar to templates, but their
132-
purpose is to represent small units, which are perfect to fulfil recurring
133-
tasks. A good example of a partial is a specially styled box with content that
134-
may appear on several pages. If this box would be part of a page layout, it
135-
would be implemented in one or more HTML files inside the :file:`Templates/Pages/`
136-
directory. If an adjustment of the box is required at one point in the future,
137-
this would mean that several template files need to be updated. However, if we
138-
store the HTML code of the box as a small HTML snippet into the :file:`Partials/`
139-
directory, we can include this snippet at several places. An adjustment only
140-
requires an update of the partial and therefore in one file only.
141-
142-
The use of partials is optional, whereas files in the :file:`Layouts/` and
143-
:file:`Templates/Pages` directories are mandatory for a typical sitepackage
144-
extension.
145-
146-
The sitepackage extension described in this tutorial focuses on the
147-
implementation of pages, rather than specific content elements.
148-
149-
.. _fluid-templates-folders-under-private-language:
150-
151-
Language
152-
~~~~~~~~
153-
The directory :file:`Language/` may contain :file:`.xlf` files that are used for
154-
the localization of labels and text strings (frontend as well as backend) by
155-
TYPO3. This topic is not strictly related to the Fluid template engine and is
156-
documented in section
157-
:ref:`Internationalization and Localization <t3coreapi:internationalization>`.
183+
**Partials** are an easy way to abstract and reuse code snippets in
184+
your templates. They don't have access to all template variables, instead
185+
the required variables need to be provided to the partial when it is used.
158186

159187
.. _fluid-in-depth:
160188

161189
Fluid in depth
162-
================
190+
==============
163191

164192
.. card-grid::
165193
:columns: 1

0 commit comments

Comments
 (0)