Skip to content

Commit

Permalink
[#45] Add heading to card grid block. Make card heading levels dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuapease committed Oct 3, 2024
1 parent dd63dec commit d46d25a
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ fieldLayouts:
userCondition: null
warning: null
width: 100
-
dateAdded: '2024-10-02T23:56:36+00:00'
elementCondition: null
fieldUid: 4382f55f-01a1-4b67-84c8-ebbc89b238d2 # Heading
handle: null
includeInCards: false
instructions: null
label: null
providesThumbs: false
required: false
tip: null
type: craft\fieldlayoutelements\CustomField
uid: dbdfb37c-efcb-4c7f-a367-c9bf274977ed
userCondition: null
warning: null
width: 100
-
dateAdded: '2024-09-30T23:33:26+00:00'
elementCondition: null
Expand Down
2 changes: 1 addition & 1 deletion config/project/project.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
dateModified: 1727908021
dateModified: 1727913396
email:
fromEmail: $SYSTEM_EMAIL_FROM
fromName: 'Viget Craft Starter'
Expand Down
53 changes: 32 additions & 21 deletions templates/_blocks/_cardGrid.twig
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
{# @var \craft\elements\Entry block #}
{% set block = block ?? null %}

<div class="container grid gap-24 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 lg:gap-48">
{% for card in block.cardGrid.eagerly().all() %}
{% switch card.type.handle %}
{% case 'imageCard' %}
{{ include('_components/card-image.twig', {
image: card.image.eagerly().one(),
href: card.href,
newWindow: card.newWindow,
title: card.title,
description: card.description,
}) }}
{% case 'iconCard' %}
{{ include('_components/card-icon.twig', {
href: card.href,
newWindow: card.newWindow,
icon: card.icon.value,
title: card.title,
description: card.description,
}) }}
{% endswitch %}
{% endfor %}
{% set cardHeadingLevel = block.heading is not empty ? 3 : 2 %}

<div class="container">
{% if block.heading %}
<h2 class="text-3xl font-bold mb-48">
{{ block.heading }}
</h2>
{% endif %}
<div class="grid gap-24 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 lg:gap-48">
{% for card in block.cardGrid.eagerly().all() %}
{% switch card.type.handle %}
{% case 'imageCard' %}
{{ include('_components/card-image.twig', {
image: card.image.eagerly().one(),
href: card.href,
newWindow: card.newWindow,
title: card.title,
description: card.description,
headingLevel: cardHeadingLevel,
}) }}
{% case 'iconCard' %}
{{ include('_components/card-icon.twig', {
href: card.href,
newWindow: card.newWindow,
icon: card.icon.value,
title: card.title,
description: card.description,
headingLevel: cardHeadingLevel,
}) }}
{% endswitch %}
{% endfor %}
</div>
</div>
11 changes: 10 additions & 1 deletion templates/_components/card-icon.twig
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@
{# @var string|null icon #}
{% set icon = icon ?? null %}

{# @var int headingLevel #}
{% set headingLevel = headingLevel ?? 3 %}

{# @var string|null title #}
{% set title = title ?? null %}

{# @var string|null description #}
{% set description = description ?? null %}

{# Computed values #}
{% set element = href ? 'a' : 'article' %}
{% set headingElement = "h#{headingLevel}" %}

{% tag element with {
class: cx(class, 'flex flex-col gap-24', {
Expand All @@ -42,7 +47,11 @@
{% if title or description %}
<div class="flex flex-col gap-8">
{% if title %}
<h2 class="text-xl font-bold">{{ title }}</h2>
{% tag headingElement with {
class: 'text-xl font-bold',
} %}
{{ title }}
{% endtag %}
{% endif %}
{% if description %}
<p class="text-sm">{{ description }}</p>
Expand Down
11 changes: 10 additions & 1 deletion templates/_components/card-image.twig
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@
{# @var string align #}
{% set align = align ?? 'left' %}

{# @var int headingLevel #}
{% set headingLevel = headingLevel ?? 3 %}

{# @var string|null title #}
{% set title = title ?? null %}

{# @var string|null description #}
{% set description = description ?? null %}

{# Computed values #}
{% set element = href ? 'a' : 'article' %}
{% set headingElement = "h#{headingLevel}" %}

{% tag element with {
class: cx(class, 'flex flex-col gap-24', {
Expand All @@ -48,7 +53,11 @@
{% if title or description %}
<div class="flex flex-col gap-8">
{% if title %}
<h2 class="text-xl font-bold">{{ title }}</h2>
{% tag headingElement with {
class: 'text-xl font-bold',
} %}
{{ title }}
{% endtag %}
{% endif %}
{% if description %}
<p class="text-sm">{{ description }}</p>
Expand Down

0 comments on commit d46d25a

Please sign in to comment.