.. _components-with-slots:
####################################
Creating Components with Slots
####################################
.. index::
single: Components with slots
single: Slots
.. versionadded:: 2.0
Slots allow you to define specific regions within a component where content editors can add child plugins. This is useful when you want to control the structure of a component while allowing flexibility in certain areas.
What are slots?
===============
Slots are predefined placeholders in a component that:
- Automatically create child plugin types for each slot
- Restrict where child plugins can be added
- Provide fallback content when a slot is empty
- Are automatically created when a component instance is saved
Defining slots
==============
To define slots in your component, add a ``slots`` attribute to the component's ``Meta`` class:
.. code-block:: python
from django import forms
from djangocms_frontend.component_base import CMSFrontendComponent, Slot
from djangocms_frontend.component_pool import components
from djangocms_frontend.contrib.image.fields import ImageFormField
@components.register
class MyHero(CMSFrontendComponent):
class Meta:
name = "My Hero Component"
render_template = "hero.html"
slots = (
("title", "Title"), # (slot_name, verbose_name)
("slot", "Slot"),
)
title = forms.CharField(required=True, initial="my title")
slogan = forms.CharField(
required=True,
initial="django CMS' plugins are great components",
widget=forms.Textarea
)
image = ImageFormField(required=True)
def get_short_description(self):
return self.title
**Slot definition formats:**
Simple tuple format::
slots = (
("slot_name", "Verbose Name"),
("buttons", "Buttons"),
)
Using the Slot class for advanced options::
from djangocms_frontend.component_base import Slot
slots = (
Slot("slot_name", "Verbose Name", allow_children=False),
Slot("buttons", "Buttons", **additional_plugin_kwargs),
)
Using slots in templates
=========================
In your render template, use the ``{% childplugins %}`` template tag to render slot content:
.. code-block:: django
{% load cms_tags frontend sekizai_tags %}
{% childplugins instance "slogan" %}
{{ instance.slogan }}
{% endchildplugins %}
Default subtitle text
{% childplugins instance "title" %}
{{ instance.title }}
{% endchildplugins %}
Default Title
{% endchildplugins %}
{% childplugins instance "subtitle" %}