Skip to content

Commit 7c88aa2

Browse files
marbrufsbraun
andauthored
docs: Deduplication and clarity in the tutorials (#317)
Co-authored-by: Fabian Braun <fsbraun@gmx.de>
1 parent ddb5255 commit 7c88aa2

File tree

4 files changed

+100
-148
lines changed

4 files changed

+100
-148
lines changed

docs/source/tutorial/builtin_components.rst

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.. _built_in_components:
22

33
######################################
4-
Using Built-In Bootstrap 5 Components
4+
Installation and Usage with Built-In Components
55
######################################
66

77
.. index::
@@ -63,34 +63,49 @@ Configuration
6363
Open your Django project's ``settings.py`` and add the following applications to ``INSTALLED_APPS`` -
6464
you only need (and should) add the components you want content editors to use:
6565

66-
.. code-block:: python
67-
68-
INSTALLED_APPS = [
69-
# Optional dependencies
70-
'djangocms_icon',
71-
'easy_thumbnails',
72-
'djangocms_link', # Required if djangocms_frontend.contrib.link is used
73-
# Main frontend components
74-
'djangocms_frontend',
75-
'djangocms_frontend.contrib.accordion',
76-
'djangocms_frontend.contrib.alert',
77-
'djangocms_frontend.contrib.badge',
78-
'djangocms_frontend.contrib.card',
79-
'djangocms_frontend.contrib.carousel',
80-
'djangocms_frontend.contrib.collapse',
81-
'djangocms_frontend.contrib.component',
82-
'djangocms_frontend.contrib.content',
83-
'djangocms_frontend.contrib.grid',
84-
'djangocms_frontend.contrib.icon',
85-
'djangocms_frontend.contrib.image',
86-
'djangocms_frontend.contrib.jumbotron',
87-
'djangocms_frontend.contrib.link',
88-
'djangocms_frontend.contrib.listgroup',
89-
'djangocms_frontend.contrib.media',
90-
'djangocms_frontend.contrib.tabs',
91-
'djangocms_frontend.contrib.utilities',
66+
.. code-block:: python
67+
68+
INSTALLED_APPS = [
69+
# Optional dependencies
70+
'djangocms_icon',
71+
'easy_thumbnails',
72+
'djangocms_link', # Required if djangocms_frontend.contrib.link is used
73+
# Main frontend components
74+
'djangocms_frontend',
75+
'djangocms_frontend.contrib.accordion',
76+
'djangocms_frontend.contrib.alert',
77+
'djangocms_frontend.contrib.badge',
78+
'djangocms_frontend.contrib.card',
79+
'djangocms_frontend.contrib.carousel',
80+
'djangocms_frontend.contrib.collapse',
81+
'djangocms_frontend.contrib.component',
82+
'djangocms_frontend.contrib.content',
83+
'djangocms_frontend.contrib.grid',
84+
'djangocms_frontend.contrib.icon',
85+
'djangocms_frontend.contrib.image',
86+
'djangocms_frontend.contrib.jumbotron',
87+
'djangocms_frontend.contrib.link',
88+
'djangocms_frontend.contrib.listgroup',
89+
'djangocms_frontend.contrib.media',
90+
'djangocms_frontend.contrib.tabs',
91+
'djangocms_frontend.contrib.utilities',
9292
]
9393
94+
For example, if you don't want to use any built-in components because you plan on
95+
:ref:`building your own <custom_components>`, a minimal setup of ``INSTALLED_APPS``
96+
would look like this:
97+
98+
.. code-block:: python
99+
100+
INSTALLED_APPS = [
101+
'easy_thumbnails',
102+
'djangocms_link', # Required if djangocms_frontend.contrib.link is used
103+
# Main frontend app - pre-built components not needed
104+
'djangocms_frontend',
105+
]
106+
107+
108+
94109
2. **Apply Migrations**
95110

96111
Run the following command to create the necessary database tables:

docs/source/tutorial/custom_components.rst

Lines changed: 31 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,39 @@
11
.. _custom_components:
22

3-
####################################
4-
Building Custom Frontend Components
5-
####################################
3+
#######################################
4+
Building Custom Components With Form Classes
5+
#######################################
66

77
.. index::
88
single: Custom frontend components
99

1010
.. versionadded:: 2.0
1111

12-
Custom frontend components are a powerful tool for content editors, allowing them to build pages without
13-
needing in-depth knowledge of design, HTML, or nested structures. Editors can simply add content to pre-defined
14-
components, creating visually cohesive pages with ease.
12+
In this tutorial, we will explore how to create custom **Frontend Components**. These are more
13+
versatile than :ref:`template_components`, but require some minimal Python coding.
1514

16-
When working with `Tailwind CSS <https://tailwindcss.com>`_, for example, you either create your custom
17-
frontend components or customize components from providers, e.g. `Tailwind UI <https://tailwindui.com>`_,
18-
`Flowbite <https://flowbite.com>`_, or the community `Tailwind Components <https://tailwindcomponents.com>`_.
15+
You create a custom frontend component by subclassing the ``CMSFrontendComponent`` class to
16+
declare the form, plus providing a rendering template, and `djangocms-frontend` will take care
17+
integrating it automatically.
1918

20-
With django CMS you make your components available to the content editors to simply add them to a page by a
21-
click **and** frontend developers for use in templates from a single source.
2219

23-
Custom frontend components are more versatile than template components, but require some minimal Python coding.
24-
Technically, you create a custom frontend component by declaring its change form and rendering template.
20+
Hero CMS component example
21+
==========================
2522

26-
Installation
27-
============
23+
In this tutorial we will create again a **Hero component** similar to the one in the
24+
:ref:`previous chapter <template_components>`.
2825

29-
Install ``djangocms-frontend`` and add it to your project as described here: :ref:`built_in_components`.
30-
31-
If you do not use the built-in components, you do not need to add them to your ``INSTALLED_APPS``.
32-
33-
.. code-block:: python
34-
35-
INSTALLED_APPS = [
36-
'easy_thumbnails',
37-
'djangocms_link', # Required if djangocms_frontend.contrib.link is used
38-
# Main frontend app - pre-built components not needed
39-
'djangocms_frontend',
40-
]
41-
42-
43-
Adding Styles and JavaScript
44-
============================
45-
46-
When building template components, you will need to provide your custom CSS files
47-
either by adding them to the base templates to load on every page, or by adding a
48-
django-sekizai block to each component.
49-
50-
Hero component example
51-
======================
26+
Directory Structure
27+
-------------------
5228

5329
``djangocms-frontend`` will look for custom frontend components in the
54-
**``cms_components`` module in any of your apps**. This way you can
30+
**``cms_components`` module in any of your installed apps**. This way you can
5531
either keep components together in one theme app, or keep them with where
5632
they are used in different custom apps.
5733

58-
Directory Structure
59-
-------------------
60-
61-
Let's go through this by creating a theme app::
62-
63-
python manage.py startapp theme
64-
65-
Custom frontend components live in the ``cms_components`` module of any of your apps.
6634
Ensure your app has the following structure::
6735

68-
theme/
36+
theme_app/
6937
cms_components.py
7038
migrations/
7139
models.py
@@ -75,6 +43,16 @@ Ensure your app has the following structure::
7543
views.py
7644
admin.py
7745

46+
In this example, `cms_components.py` will contain the component definition, and `hero.html`
47+
the presentation template.
48+
49+
.. note::
50+
51+
Components will create migrations since they use proxy models of ``djangocms-frontend``'s
52+
``FrontendUIItem`` model which are necessary, for example, to manage permissions.
53+
Those migrations will be added to the app containing the ``cms_component.py`` file.
54+
55+
7856
Creating two Custom Frontend Components
7957
---------------------------------------
8058

@@ -88,7 +66,7 @@ Add a ``cms_components.py`` file to the ``theme`` app (see structure above):
8866
from djangocms_frontend.component_base import CMSFrontendComponent
8967
from djangocms_frontend.component_pool import components
9068
from djangocms_frontend.contrib.image.fields import ImageFormField
91-
69+
from django import forms
9270
9371
@components.register
9472
class MyHeroComponent(CMSFrontendComponent):
@@ -165,7 +143,7 @@ The templates could be, for example:
165143
<a class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 me-2 mb-2 dark:bg-blue-600 dark:hover:bg-blue-700 focus:outline-none dark:focus:ring-blue-800"
166144
href="{{ instance.link|to_url }}">{{ instance.text }}</a>
167145
168-
As always, django CMS manages styling and JavaScript dependencies with django-sekizai.
146+
As always, django CMS manages styling and JavaScript dependencies with **django-sekizai**.
169147
In this example, we add the Tailwind CSS CDN to the ``js`` block.
170148

171149

@@ -193,6 +171,8 @@ and can contain Python code to modify the behavior of a form. You cannot directl
193171
the resulting plugin class with the exception of ``get_render_template()``. Similarly, you cannot add
194172
Python code the model class, in this case with the exception of ``get_short_description()``.
195173

174+
For maximun flexibility in your customized components, you can build a :ref:`custom Plugin<how-to-add-frontend-plugins>`.
175+
196176

197177
Conclusion
198178
==========
@@ -202,13 +182,6 @@ provide visually appealing components to content editors with minimal coding.
202182

203183
By following the steps outlined above, you can:
204184

205-
- Create a theme app to house your custom components.
206185
- Define components using the `CMSFrontendComponent` class.
207186
- Leverage templates to control the visual presentation of your components.
208-
- Register and manage your components seamlessly within django CMS.
209-
210-
.. note::
211-
212-
Components will create migrations since they use proxy models of ``djangocms-frontend``'s
213-
``FrontendUIItem`` model which are necessary, for example, to manage permissions.
214-
Those migrations will be added to the app containing the ``cms_component.py`` file.
187+
- Register and manage your components seamlessly within django CMS.

docs/source/tutorial/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ that gets the job done for your project.
1818
added to your project's ``INSTALLED_APPS``.
1919

2020
2. **Template Components** – The easiest approach creating or porting your own
21-
**custom frontend components**, allowing you define components by their HTML templates. Special
21+
custom components, allowing you to define them by their **HTML templates, without any code**. Special
2222
``djangocms-frontend`` tags are used to provide the additional declarative information
2323
needed. This is the fastest approach to create ``djangocms-frontend`` components.
2424
Template-based (or auto) components are auto-detected.

docs/source/tutorial/template_components.rst

Lines changed: 26 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.. _template_components:
22

33
############################
4-
Building Template Components
4+
Simplified Component Creation with Templates
55
############################
66

77
.. index::
@@ -10,68 +10,29 @@ Building Template Components
1010

1111
.. versionadded:: 2.1
1212

13-
Custom components are a powerful tool for content editors, allowing them to build pages without needing
14-
in-depth knowledge of design, HTML, or nested structures. Editors can simply add content to pre-defined
15-
components, creating visually cohesive pages with ease.
13+
**Template components** are the easiest approach to creating or porting your own custom
14+
frontend components, allowing you to define custom components **using django templates,
15+
without needing to write any Python code**.
1616

17-
When working with `Tailwind CSS <https://tailwindcss.com>`_, for example, you
18-
either create your custom components or customize components from providers,
19-
e.g. `Tailwind UI <https://tailwindui.com>`_,
20-
`Flowbite <https://flowbite.com>`_, or the community
21-
`Tailwind Components <https://tailwindcomponents.com>`_.
2217

23-
With django CMS you make your components available to the content editors to
24-
simply add them to a page by a click **and** frontend developers for use in templates from a single
25-
source.
18+
Example Hero Template Component
19+
===============================
2620

27-
Installation
28-
============
29-
30-
Install ``djangocms-frontend`` and add it to your project as described here: :ref:`built_in_components`.
31-
32-
If you do not use the built-in components, you do not need to add them to your ``INSTALLED_APPS``.
33-
34-
.. code-block:: python
35-
36-
INSTALLED_APPS = [
37-
'easy_thumbnails',
38-
'djangocms_link', # Required if djangocms_frontend.contrib.link is used
39-
# Main frontend app - built-in components from contrib not needed
40-
'djangocms_frontend',
41-
]
42-
43-
44-
Adding Styles and JavaScript
45-
============================
46-
47-
When building template components, you will need to provide your custom CSS files
48-
either by adding them to the base templates to load on every page, or by adding a
49-
django-sekizai block to each component.
50-
51-
Hero component
52-
==============
53-
54-
``djangocms-frontend`` allows developers to extend its functionality by creating
55-
**template components**. In this tutorial, we will create an **Hero component**
56-
with the following fields:
21+
In this tutorial, we will create a **Hero template component** with the following fields:
5722

5823
- ``title``: A required text field.
5924
- ``slogan``: A required text area field.
6025
- ``hero_image``: A required image field.
6126

62-
This component will be stored in a template directory named ``<app_name>/cms_components``
63-
(or any subdirectory thereof).
64-
65-
.. note::
66-
You can change the location of your template components inside your template directory
67-
by setting the :attr:`DJANGOCMS_FRONTEND_COMPONENT_FOLDER` setting. The default is
68-
``cms_components``. If you change it, you need to adjust the directory structure accordingly.
69-
27+
This component will be declared by using special djangocms tags in a template file,
28+
with no python code required.
7029

7130
Directory Structure
7231
-------------------
7332

74-
The template component lives in the template directory of any of your apps.
33+
`djangocms-frontend` will **automatically locate and register template components** by looking for them
34+
in the **``templates/<app_name>/cms_components/``** directory of your installed apps.
35+
7536
Ensure your DjangoCMS app has the following structure::
7637

7738
theme_app/
@@ -84,20 +45,19 @@ Ensure your DjangoCMS app has the following structure::
8445
views.py
8546
admin.py
8647

87-
Creating the Template Component
88-
--------------------------------
48+
In this example, ``theme_app/templates/theme_app/cms_components/hero.html`` will be the template
49+
defining our custom hero component.
8950

90-
The **template component** must be stored in the ``cms_components`` directory
91-
inside your app. ``djangocms-frontend`` expects you to follow Django's template
92-
namespace convention. Create a new file at::
51+
.. note::
52+
You can change the location of your template components inside your template directory
53+
by setting the :attr:`DJANGOCMS_FRONTEND_COMPONENT_FOLDER` setting. The default is
54+
``cms_components``. If you change it, you need to adjust the directory structure accordingly.
9355

94-
theme_app/templates/theme_app/cms_components/hero.html
9556

96-
.. note::
97-
No python code is required to create the component. The component is
98-
defined in the template itself.
57+
Creating the Template Component
58+
--------------------------------
9959

100-
Then, add the following code::
60+
Add the following code to the `hero.html` template::
10161

10262
<!-- theme_app/templates/theme_app/cms_components/hero.html -->
10363
{% load frontend cms_component %}
@@ -291,7 +251,7 @@ The verbose choice label is appended by the actual value of the field between an
291251

292252

293253
Limitations of template components
294-
----------------------------------
254+
==================================
295255

296256
Template components are a powerful tool for developers, but they have some limitations:
297257

@@ -305,6 +265,10 @@ Template components are a powerful tool for developers, but they have some limit
305265
Classes are instantiated by default, for example. This is ok for ``widget=forms.Textarea``, but potentially not
306266
for more complex cases.
307267

268+
For more powerful customization options, consider building a :ref:`custom Frontend Component <custom_components>`
269+
or a :ref:`custom Plugin<how-to-add-frontend-plugins>`
270+
271+
308272
Examples
309273
========
310274

0 commit comments

Comments
 (0)