Composable Layouts with a predictable structure
Last updated
Last updated
Before we dive into how to create composable layouts with Twig Hooks
, let's first understand what the composable
part means.
composable (not )
Capable of being (as from multiple or elements).
source:
So, to achieve composability we need building blocks from which we will build our layouts. With Twig Hooks we can use the following building blocks (in Twig Hooks we call them hookables):
A regular Twig template
Twig Component
Live Component
As mentioned in previous chapters, hookables can also define their own hooks, so we are able to create more complex building blocks like the header section which consisted of a title and some action buttons.
To fully utilize this functionality, make sure:
you have turned on the Autoprefixing feature and you are familiar with it
you are familiar with and the concept of
The idea behind the Predictable structure
is to organize your hookables (your Twig templates) to make it easier to guess the hooks names with which they are rendered, and to be able to find them based on a given hook name. This approach aims to reduce the need to browse through multiple folders when trying to locate a template we want to edit or check.
The main rule for predictable structure is:
Given a template defines a hook, all its hookables should live in a directory with the same name as the template.
So, if templates/course/create.html.twig
defines a hook, all its direct hookables should live in the templates/course/create/
directory (e.g., templates/course/create/form.html.twig
).
To better understand this rule, let's consider the following directory structure (and let's assume create.html.twig
, form.html.twig
and header.html.twig
define new hooks inside):
we can tell that:
create.html.twig
has two direct hookables (form
, header
)
header.html.twig
has one direct hookable (title
)
form.html.twig
has five direct hookables (create
, max_number_of_students
, name
, price
, start_date
)
Moreover, assuming create.html.twig
defines an app.course.create
primal hook, we can tell that the form.html.twig
defines an app.course.create.form
subsequent hook and header.html.twig
defines a app.course.create.header
subsequent hook.