|
| 1 | +.. _template_components: |
| 2 | + |
| 3 | +############################ |
| 4 | +Building Template Components |
| 5 | +############################ |
| 6 | + |
| 7 | +.. index:: |
| 8 | + single: Template Components |
| 9 | + single: Auto Components |
| 10 | + |
| 11 | +.. versionadded:: 2.1 |
| 12 | + |
| 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. |
| 16 | + |
| 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>`_. |
| 22 | + |
| 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. |
| 26 | + |
| 27 | +Custom components are part of the djangocms-frontend root package and do not |
| 28 | +require additional listing in the ``INSTALLED_APPS`` setting. |
| 29 | + |
| 30 | +djangocms-frontend allows developers to extend its functionality by creating |
| 31 | +template components**. In this tutorial, we will create an **Hero component** |
| 32 | +with the following fields: |
| 33 | + |
| 34 | +- ``title``: A required text field. |
| 35 | +- ``slogan``: A required text area field. |
| 36 | +- ``hero_image``: A required image field. |
| 37 | + |
| 38 | +This component will be stored in a template directory named ``<app_name>/cms_components``, |
| 39 | +as required for djangocms-frontend template components. |
| 40 | + |
| 41 | +Directory Structure |
| 42 | +------------------- |
| 43 | + |
| 44 | +The templte component lives in the template directory of any of your apps. |
| 45 | +Ensure your DjangoCMS app has the following structure:: |
| 46 | + |
| 47 | + theme_app/ |
| 48 | + migrations/ |
| 49 | + models.py |
| 50 | + templates/ |
| 51 | + theme_app/ |
| 52 | + cms_components/ |
| 53 | + hero.html |
| 54 | + views.py |
| 55 | + admin.py |
| 56 | + |
| 57 | +Creating the Template Component |
| 58 | +-------------------------------- |
| 59 | + |
| 60 | +The **template component** must be stored in the ``cms_components`` directory |
| 61 | +inside your app. djangocms-frontend expects you to follow Django's template |
| 62 | +namespace convention. Create a new file at:: |
| 63 | + |
| 64 | + theme_app/templates/theme_app/cms_components/hero.html |
| 65 | + |
| 66 | +.. note:: |
| 67 | + No python code is required to create the component. The component is |
| 68 | + defined in the template itself. |
| 69 | + |
| 70 | +Then, add the following code:: |
| 71 | + |
| 72 | + <!-- theme_app/templates/theme_app/cms_components/hero.html --> |
| 73 | + {% load frontend cms_component %} |
| 74 | + |
| 75 | + {# Declare component - template tags are evaluated at project startup and will render empty #} |
| 76 | + {% cms_component "Hero" name=_("My Hero Component") %} |
| 77 | + {% field "title" forms.CharField required=True name=_("Title") %} |
| 78 | + {% field "slogan" forms.CharField required=True name=_("Slogan") widget=forms.Textarea %} |
| 79 | + {% field "hero_image" ImageFormField required=True name=_("Image") help_text=_("At least 1024px wide image") %} |
| 80 | + |
| 81 | + {# Actual template - when rendering, declared fields are available in the context #} |
| 82 | + <section class="bg-white dark:bg-gray-900"> |
| 83 | + <div class="grid max-w-screen-xl px-4 py-8 mx-auto lg:gap-8 xl:gap-0 lg:py-16 lg:grid-cols-12"> |
| 84 | + <div class="mr-auto place-self-center lg:col-span-7"> |
| 85 | + <h1 class="max-w-2xl mb-4 text-4xl font-extrabold tracking-tight leading-none md:text-5xl xl:text-6xl dark:text-white"> |
| 86 | + {{ title }} |
| 87 | + </h1> |
| 88 | + <p class="max-w-2xl mb-6 font-light text-gray-500 lg:mb-8 md:text-lg lg:text-xl dark:text-gray-400"> |
| 89 | + {{ slogan }} |
| 90 | + </p> |
| 91 | + {% childplugins %}{% endchildplugins %} |
| 92 | + </div> |
| 93 | + <div class="hidden lg:mt-0 lg:col-span-5 lg:flex"> |
| 94 | + <img src="{{ hero_image.url }}"> |
| 95 | + </div> |
| 96 | + </div> |
| 97 | + </section> |
| 98 | + |
| 99 | +Understanding the Code |
| 100 | +---------------------- |
| 101 | + |
| 102 | +Component Declaration |
| 103 | +^^^^^^^^^^^^^^^^^^^^^ |
| 104 | + |
| 105 | +:: |
| 106 | + |
| 107 | + {% cms_component "Hero" name=_("My Hero Component") %} |
| 108 | + |
| 109 | +This tag **declares** the component and assigns it a name (``Hero``). This is used internally |
| 110 | +by django CMS to identify the plguin later. The ``name`` parameter is used to display the |
| 111 | +component in the CMS admin interface. Internally the command declares a ``CMSFrontendComponent`` |
| 112 | +class. All named arguments are added to the component's Meta class. |
| 113 | + |
| 114 | +Defining Fields |
| 115 | +^^^^^^^^^^^^^^^ |
| 116 | + |
| 117 | +:: |
| 118 | + |
| 119 | + {% field "title" forms.CharField required=True name=_("Title") %} |
| 120 | + {% field "slogan" forms.CharField required=True name=_("Slogan") widget=forms.Textarea %} |
| 121 | + {% field "hero_image" ImageFormField required=True name=_("Image") help_text=_("At least 1024px wide image") %} |
| 122 | + |
| 123 | +Each ``{% field %}`` tag defines a form field that content editors can use when configuring the component in the CMS. |
| 124 | +The first parameter is the field name which is then available in the rest of the template. The second parameter is the |
| 125 | +form field class to use. The remaining parameters are passed to the form field constructor. |
| 126 | + |
| 127 | +By default, Django's ``django.forms`` module is available as ``forms`` in the template context. If the relevant apps are |
| 128 | +installed, additional fields available are ``HTMLFormField`` for rich text, ``LinkFormField`` for links, and ``ImageFormField`` |
| 129 | +for images. |
| 130 | + |
| 131 | +Rendering the Component |
| 132 | +^^^^^^^^^^^^^^^^^^^^^^^ |
| 133 | + |
| 134 | +After the fields are declared, the remaining part of the template is dedicated to rendering the component. |
| 135 | +The fields declared earlier (``title``, ``slogan``, and ``hero_image``) are now available as template variables:: |
| 136 | + |
| 137 | + <h1>{{ title }}</h1> |
| 138 | + <p>{{ slogan }}</p> |
| 139 | + <img src="{{ hero_image.url }}"> |
| 140 | + |
| 141 | +The ``{% childplugins %}`` block allows additional CMS plugins (like buttons) to be added inside the component |
| 142 | +in the structure editor. |
| 143 | + |
| 144 | +Make the component avialabvle in django CMS |
| 145 | +------------------------------------------- |
| 146 | + |
| 147 | +Template components are discovered automatically - no more coding is required. If you change the declarative |
| 148 | +content, i.e. add/remove ``{% field %}`` tags, or change the ``{% cms_component %}`` tag, you need to restart |
| 149 | +the Django server to apply the changes. |
| 150 | + |
| 151 | +1. Restart your Django server. |
| 152 | +2. Create a new page end edit it. |
| 153 | +3. Add a new **Hero component** to a page from the plugin picker. |
| 154 | +4. Fill in the **title**, **slogan**, and **hero image** fields. |
| 155 | +5. Save and publish the page. |
| 156 | + |
| 157 | +Conclusion |
| 158 | +---------- |
| 159 | + |
| 160 | +You have successfully created a **djangocms-frontend template component** using ``cms_component``! |
| 161 | +This structure allows editors to easily customize hero sections while maintaining a reusable |
| 162 | +and structured design. |
| 163 | + |
0 commit comments