Twig Components¶
Twig Components are widgets (for example, My dashboard blocks from Headless edition) and HTML code (for example, a tag for loading JS or CSS files) that you can inject into the existing templates to customize and extend the user interface. They are combined into groups that are rendered in designated templates.
Built-in Twig Component groups are available for the back office, but you can create your own for use anywhere.
To learn which groups are available in a given view, use the integration with Symfony Profiler.
Create Twig Component¶
You can create Twig Components in one of two ways:
PHP code¶
Create a class implementing the \Ibexa\Contracts\TwigComponents\ComponentInterface
interface and register it as a service by using the ibexa.twig.component
service tag, for example:
1 2 3 |
|
The available attributes are:
group
- indicates the group to which the component belongspriority
- indicates the priority of rendering this component when rendering the whole component group. The higher the value the earlier the component is rendered
This way requires writing custom code, but it allows you to fully control the rendering of the component.
YAML configuration¶
You can create a Twig Component and add it to a group using YAML configuration, as in the example below:
1 2 3 4 5 6 7 8 |
|
The Component priority cannot be specified when using the YAML configuration, but it allows you to use the built-in components to quickly achieve common goals.
You can use an unique group name when creating a Twig Component to create your own group.
Built-in components¶
Name | Description | YAML type |
---|---|---|
Script | Renders a <script> tag |
script |
Stylesheet | Renders a <link> tag |
stylesheet |
Template | Renders a Twig template | template |
Controller | Renders a Symfony controller | controller |
HTML | Renders static HTML | html |
Example¶
The following example shows how you can use each of the built-in components to customize the back office:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
Render Twig Components¶
Render both single Twig Components and whole groups using the dedicated Twig functions. You can modify the Component rendering process by:
- listening to one of the related events
- decorating the
\Ibexa\Contracts\TwigComponents\Renderer\RendererInterface
service
Symfony Profiler integration¶
Use the built-in integration with Symfony Profiler to see which Twig Components have been rendered in a given view. In the Ibexa DXP tab you can find:
- the list of all rendered Twig Component groups by the given view, including empty groups
- the list of rendered Twig Components with information about the group they belong to