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
5531either keep components together in one theme app, or keep them with where
5632they 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.
6634Ensure 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+
7856Creating 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 ** .
169147In 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
193171the resulting plugin class with the exception of ``get_render_template() ``. Similarly, you cannot add
194172Python 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
197177Conclusion
198178==========
@@ -202,13 +182,6 @@ provide visually appealing components to content editors with minimal coding.
202182
203183By 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.
0 commit comments