Skip to content

Commit e586245

Browse files
authored
[DOC] Rewrite Fluid introduction chapter
1 parent b4d88ee commit e586245

File tree

1 file changed

+129
-108
lines changed

1 file changed

+129
-108
lines changed

Documentation/Concepts/Fluid/Index.rst

Lines changed: 129 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -15,151 +15,172 @@ Fluid templates
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+
Accessing Variables
39+
-------------------
40+
41+
An integral part of Fluid are variables. This is the data
42+
that is provided by the backend to be used inside your
43+
template. You can access variables like this:
44+
45+
.. code-block:: xml
46+
47+
<h1>{myHeadline}</h1>
48+
49+
50+
If a variable contains subitems, these can be accessed
51+
with the dot syntax:
52+
53+
.. code-block:: xml
54+
55+
<p>{myVariable.mySubItem}</p>
56+
57+
Modifying Variables
58+
-------------------
59+
60+
You can also do some basic math operations:
61+
62+
.. code-block:: xml
63+
64+
{myNumber + 5}
65+
66+
67+
If you need to modify the provided data even more, you
68+
can use so-called ViewHelpers. These are functions that
69+
take some input values, perform operations on those
70+
values and then output the result. The following example
71+
converts a variable to uppercase:
72+
73+
.. code-block:: xml
74+
75+
<f:format.case mode="upper">{myText}</f:format.case>
76+
77+
If you want to perform multiple operations on one variable
78+
or if your templates become more complex, you might also
79+
want to use :ref:`Fluid's inline notation <t3coreapi:fluid-inline-notation>`.
80+
81+
Using Control Structures
82+
------------------------
83+
84+
ViewHelpers are also used for so-called control structures.
85+
If you want to add a condition to your template, you can
86+
use the `<f:if>` ViewHelper:
87+
88+
.. code-block:: xml
89+
90+
<f:if condition="{myVariable} == 'hello'">
91+
The variable is "hello".
92+
</f:if>
93+
94+
You can also use the `<f:for>` ViewHelper to loop over an
95+
array:
96+
97+
.. code-block:: xml
98+
99+
<ul>
100+
<f:for each="{myList}" as="myItem">
101+
<li>This item is {myItem}.</li>
102+
</f:for>
103+
</ul>
41104
42105
43106
.. _ft-directory-structure:
44107

45108
Directory structure
46109
===================
47110

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.
111+
Nowadays, Fluid templates in TYPO3 are always part of an
112+
extension. As they are neither PHP code nor configuration files
113+
and don't need to be accessed by end users, they are placed in the
114+
`Resources/Private/` subfolder.
58115

59116
.. directory-tree::
60-
:level: 4
117+
:level: 6
61118
:show-file-icons: true
62119

63-
* EXT:my_sitepackage
120+
* my_sitepackage
64121

65122
* Resources
66123

67124
* Private
68125

69-
* Language
70-
71126
* Templates
72127

73128
* Layouts
74129

130+
* DefaultLayout.html
131+
75132
* Pages
76133

134+
* MyPage.html
135+
77136
* Partials
78137

79-
* Public
138+
* MyPartial.html
80139

81-
* Css
82140

83-
* Images
141+
There are conventions about the exact location of template files.
142+
However, be aware that these paths might vary slightly between
143+
projects or extensions as they can be configured individually.
84144

85-
* JavaScript
145+
.. _fluid-templates-folders-under-private:
86146

87-
* StaticTemplate
147+
Templates, Layouts and Partials
148+
-------------------------------
88149

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.
150+
.. uml::
151+
:caption: Fluid template structure
94152

95-
The :file:`Private/` directory with its two sub-folders :file:`Language/` and
96-
:file:`Templates/` in contrast, requires some explanation.
153+
frame layout as "Templates/Layouts/DefaultLayout.html" {
154+
frame page as "Templates/Pages/MyPage.html" {
155+
rectangle partial [
156+
<b>Templates/Partials/MyPartial.html</b>
157+
(reusable code snippet)
158+
]
159+
}
160+
}
97161

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

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

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>`.
176+
**Partials** are an easy way to abstract and reuse code snippets in
177+
your templates. They don't have access to all template variables, instead
178+
the required variables need to be provided to the partial when it is used.
158179

159180
.. _fluid-in-depth:
160181

161182
Fluid in depth
162-
================
183+
==============
163184

164185
.. card-grid::
165186
:columns: 1

0 commit comments

Comments
 (0)