How to integrate Component Architecture into Symfony?

webmamba

Matheo Daninos

Posted on March 15, 2024

How to integrate Component Architecture into Symfony?

Miscommunication in our projects is costly. A single misunderstood User Story can result in 3 days of wasted development time. Additionally, when developers do not use the same programming language, it may be necessary to construct APIs to facilitate communication, which can also be expensive. It is important to consider why front-end developers may be hesitant to work with Twig and how this can lead to a disconnect between front-end and back-end development.

Front-end developers, like back-end developers, have their own set of habits, principles, and rules that make them feel safe, efficient, and at home.

One of the most important principles is Component Architecture, which serves as the foundation for major JavaScript frameworks such as Svelte, React, and Vue.

What is Component Architecture?

4 main rules define Component Architecture.

Composition

A page is no longer just a page, but rather a collection of small, reusable components. These components can be assembled to form a page. For example, there could be a component for the title and another for the training list. The training list component could even be composed of smaller components, such as a training card component. The goal is to create the most atomic components possible.

Independence

For a component to be infinitely reusable, it should not be aware of its context. It should function identically when placed on another page.

Properties

Our component must remain independent, but we can customize it based on the context. For instance, we can change the label of a button while keeping its appearance the same by assigning it properties. Properties are attributes that the component will adopt at creation and maintain throughout its life.

States

A component can have multiple states. For instance, a button can transition from an active state to a loading state and then to a disabled state. Typically, a state principle is used to achieve this. The difference between a state and a property is that a property remains constant throughout the lifespan of the component, while a state changes.

Does it work with Symfony?

The answer is yes!

Start by installing

composer require ux-twig-component
Enter fullscreen mode Exit fullscreen mode

Then in your template folder, create a component folder. Inside the component folder, add a file named Button.html.twig.

<button class="btn btn-primary">Click me</button>
Enter fullscreen mode Exit fullscreen mode

And just like that… you have created your first component! Congrats

Now, how will you use it? Let's assume you want to use it on your homepage. Open your home.html.twig template.

// the content of your template
<twig:Button/>
Enter fullscreen mode Exit fullscreen mode

This way, you have successfully imported a component for the first time.

Can I include properties for this component?

Sure, you can! Simply go back to your button template and add a label property like this:

{% props label = 'Click me' %}

<button class="btn btn-primary">{{ label }}</button>
Enter fullscreen mode Exit fullscreen mode

Now, our component has a label property, with a default value 'Click me'. Without this property, using the component would result in an error.

To assign a property to a component, follow these steps:

<twig:Button label="Submit"/>
Enter fullscreen mode Exit fullscreen mode

How do we do composition?

If we want our component to contain an icon before our label, let's modify the template of our button.

{% props label = 'Click me' %}

<button class="btn btn-primary">
        {% block content %}
        {% endblock %}
        {{ label }}
</button>
Enter fullscreen mode Exit fullscreen mode

So, I have added a block that I have named content. The term "content" is important because it corresponds to the default block.

You can now use your button in this way:

<twig:Button label="Submit">
    <twig:Icon/>
</twig:Button>
Enter fullscreen mode Exit fullscreen mode

We added an Icon component to the Button component by placing it between the tags. This transfers the Icon component to the content block.

This block is a powerful tool for for precise composition. You can even create your own blocks.

{% props label = 'Click me' %}

<button class="btn btn-primary">
        {% block content %}
        {% endblock %}
        {{ label }}
        {% block after_label %}
        {% endblock %}
</button>
Enter fullscreen mode Exit fullscreen mode
<twig:Button label="Submit">
    <twig:Icon/>
    <twig:block name="after_label">
            +
    </twig:block>
</twig:Button>
Enter fullscreen mode Exit fullscreen mode

Note that we use the <twig:block tag to easily refer to the block of our component.

And what about the states of our component?

You have two options to manage the states of your component:

  • For the first option, if you want your component's CSS classes to change based on user actions, you can use Stimulus, a small JavaScript library that integrates perfectly with Symfony through the StimulusBundle. With just a few lines of JavaScript code, you can achieve your goal.
  • But imagine having a component with complex states that depend on the back-end. In this case, you have the option of using LiveComponents. I won't go into details, but here is what you need to remember: with very little code, you can perform complex actions on your back-end without reloading the page.

The present article is meant to give you an overview of how to use Component Architecture with Symfony. I know this article will be read mainly by back-end developers, but spread the word. It is suitable for both back-end and front-end developers. Even front-end developers can feel at home with Symfony. And you, back-end developers, you can continue to have fun while developing a quality front-end.

Thank you for reading!

💖 💪 🙅 🚩
webmamba
Matheo Daninos

Posted on March 15, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related