← All posts

Crafting Lean, Performant Cards: A FlyntWP Twig Component Walkthrough

Crafting Lean, Performant Cards: A FlyntWP Twig Component Walkthrough

I’ve had enough of slow, bloated websites. For too long, the promise of "easy" low-code page builders has delivered a nightmare of inconsistent design, inline styles, and HTML structures so deep and convoluted they make Core Web Vitals scores weep. That’s why I absolutely love building with FlyntWP. It’s a breath of fresh air, providing a lightweight, programmatic approach that puts developers firmly in control, while empowering content editors with just the right amount of flexibility.

FlyntWP embraces a component-driven architecture built on modular Twig templates and powered by the robust content management of ACF Pro. This combination allows us to create highly reusable, perfectly optimized content blocks that render pristine HTML. Forget the endless `div` soup; with FlyntWP, we build components that are inherently clean, fast, and designed for stellar performance right out of the gate. We’re talking about sub-second build times thanks to Vite, and a DOM structure so lean, Google’s Lighthouse scores will sing.

Today, I want to walk you through a common pattern: building a reusable "Card" component. This is something every website needs – a flexible block to display an image, title, description, and a call-to-action link. The goal is simple: make it easy for editors to create beautiful cards without ever touching code, ensure the HTML is perfect, and make it reusable across any page where cards are needed.

First, let's set up the content structure in ACF Pro. I create a new Field Group named "Card Block." Within this group, I’ll add four fields: 1. **Image**: An "Image" field, returning the Image Array. This ensures we have access to all image data, like alt text and various sizes. 2. **Title**: A "Text" field for the card’s main heading. 3. **Description**: A "WYSIWYG Editor" field. Crucially, I’ll limit the toolbar options here to prevent editors from injecting unwanted styles or complex formatting. A simple paragraph or two is all we need. 4. **Link**: A "Link" field. This gives editors full control over the URL, link text, and target (new window/tab).

This ACF setup gives editors everything they need while strictly controlling the output to maintain design consistency and a clean DOM.

Now for the Twig component itself. In FlyntWP, our components live in `src/Components`. I'd create a folder `src/Components/Card` and inside it, an `index.twig` file. This file will receive the data directly from our ACF fields. Here’s a simplified version of what `index.twig` might look like:

```twig <div class="card {{ options.className }}"> {% if data.image %} <figure class="card__figure"> <img src="{{ Image(data.image).src }}" alt="{{ Image(data.image).alt }}" loading="lazy" class="card__image"> </figure> {% endif %} <div class="card__content"> {% if data.title %} <h3 class="card__title">{{ data.title }}</h3> {% endif %} {% if data.description %} <div class="card__description">{{ data.description }}</div> {% endif %} {% if data.link %} <a href="{{ data.link.url }}" target="{{ data.link.target }}" class="card__link">{{ data.link.title }}</a> {% endif %} </div> </div> ```

In this Twig snippet, `data` contains all the values from our ACF fields. `options.className` is a powerful FlyntWP feature that allows developers to pass additional CSS classes dynamically to the component’s wrapper, enhancing flexibility without needing more ACF fields. Notice the use of `loading="lazy"` for images – a small but critical detail for performance. The `Image()` function is a custom Twig function FlyntWP provides, making image handling super efficient. We wrap the image in a `figure` for semantic correctness and only render elements if the data exists, preventing empty tags in the DOM.

Once this component is registered with FlyntWP (typically as a block within a flexible content field), editors can simply drag and drop the "Card" block, fill in the fields, and instantly have a perfectly formed, performant card appear on their page. No drag-and-drop elements for positioning, no messy rich text editor output for layout – just structured content rendered by optimized Twig templates.

This approach isn’t just about making developers happy; it directly translates into superior user experience. A lightweight DOM means faster page loads, smoother interactions, and ultimately, flawless Core Web Vitals scores. When you’re building with FlyntWP, you're not just writing code; you’re crafting a faster, cleaner web, one modular, performant component at a time. This programmatic control and emphasis on clean structure is exactly why FlyntWP is my go-to for building high-performance WordPress sites that truly stand out.