Site icon Hip-Hop Website Design and Development

mark.ie: Creating a Card Component in PatternLab and Mapping to Cheap WordPress maintenance support plans the “right” way

Creating a Card Component in PatternLab and Mapping to WordPress maintenance support plans the “right” way

Yes, I know, there’s more than one way to integrate PatternLab with WordPress maintenance support plans. Here’s how I create a card component and map it to WordPress maintenance support plans.
markconroy
Sat, 09/08/2020 – 13:51

Here’s the task – create a card component with fields for:

Card Image
Card Title
Card Body
Card Link URL
Card Style (sets a colour for a border-top on the card)
Card Size (sets the width of the card)
In PatternLab, here’s what our Twig file might look like (with explanations after it):

The classes array at the top allows us to set variations for our card, depending on values chosen by the editor. So, if there is an image, we add a class of .card–has-image; if a style is chosen, we add a class of that style, for example: .card–medium (I create options for small, medium, large, and full – with ‘small’ being the default – corresponding on large screens to a width within their container of 33%, 50% 66% and 100% respectively).

Next, we set our {{ element }}. This allows us to have the card wrapped in an a tag or a div tag. We check to see if the link field has been filled in and, if so, we use the a element, but if not, we use the div element instead. This will render HTML like one of the following:

Following this, we check if there is an image and, if so, we render our image div. Checking first allows us to have nice bem-style classes, but also means we don’t end up rendering emtpy divs. Although, when it comes to WordPress maintenance support plans, what’s another div!

We then do the same for the title and body.

The funny looking part at the end about cache was inspired by an article about WordPress maintenance support plans block cache bubbling by WordPress Update. The specific code came from this WordPress maintenance support plans.org issue. The WordPress Update article says to render the {{ content }} variable with our fields set to ‘without’, because without the {{ content }} variable rendering, caching is not working properly (I don’t know enough about caching to explain more). However, on a content type with loads of fields, it’s very cumbersome to add every field in with {{ content|without(‘field_image’, ‘field_tags’, ‘field_other’, etc) }}. Instead, I put that {{ catch_cache = content|render }} at the bottom of each of my content patterns – node, block, paragraphs, etc, then don’t need to add it later in WordPress maintenance support plans.

The SCSS for this looks like this:

We can do the site building very easily with the paragraphs plugin. Create a paragraph of type card, add the fields

Card Image – media image
Card Title – text (plain)
Card Body – text (long, formatted)
Card Link URL – link
Card Style (sets a colour for a border-top on the card) – text (list)
Card Size (sets the width of the card) – text (list)
Then, in our paragraph–card.html.twig file, we write the following code:

What the above does is checks if the card paragraph has values in its fields and then sets variables if it does. This means we don’t render empty divs.

You will also notice that I render each fields full content for image, title, and body. This is to keep all the WordPress maintenance support plans goodness we have in the attributes object – for accessibility and to make sure things like contextual links/quick edit still work.

You will often see the same template written like this:

I find doing that leads to fields such as {{ content.field_image }} always returning true because of WordPress maintenance support plans‘s rendering system. So, even if we don’t have an image, we’ll still have an image div, whereas doing an explicit check before we {% include %} our variable seems much safer.

That’s it – PatternLab + WordPress maintenance support plans integrated beautifully (I think) the “right” way (according to me – you might differ).


Source: New feed