Uncategorized

How to Create a Custom Header and Footer in Drupal 10/11: Understanding {% include %} vs {{ page.header }}

When designing a custom theme in Drupal 10, one of the most fundamental components is creating a custom header and footer. While these elements are essential for site branding and user navigation, understanding the best way to implement them in Drupal can be a bit confusing—especially when deciding between two options: using {{ page.header }} or including a custom Twig template with {% include %}.

In this tutorial, we’ll walk through how to create a custom header and footer, explaining when and why you might use either method to render content in your theme. We’ll also dive into how both approaches—{{ page.header }} and {% include '@acesoftech/layout/header.html.twig' %}—work, and how to decide which one fits your needs.


1. Creating a Custom Header and Footer in Drupal 10/11

Before we dive into the code and templates, let’s take a look at how Drupal organizes the structure of pages and regions.

Step 1: Define Regions in acesoftech.info.yml

In Drupal, regions are areas where you can place content, such as blocks, views, and other dynamic elements. To set up your custom header and footer, you first need to define these regions in your theme’s acesoftech.info.yml file.

Open the acesoftech.info.yml file, and add header and footer regions like this:

yaml
regions:
  header: 'Header'
  content: 'Content'
  footer: 'Footer'

This tells Drupal where the header and footer blocks will be rendered.

Step 2: Custom Header Template

Next, let’s create a custom template for the header. This will allow you to define the layout, content, and styling of your header region.

  1. Create a Custom Header Template:Create a file called header.html.twig in the templates/layout folder of your theme:
    themes/custom/acesoftech/templates/layout/header.html.twig
  2. Add Custom Header Content:Inside the header.html.twig, you can include the site name and any content you wish to appear in the header region.
    twig
    <header class="custom-header">
    <div class="site-branding">
    <h1 class="site-title">{{ site_name }}</h1>
    </div>
    <div class="header-region">
    {{ page.header }}
    </div>
    </header>
    • site_name: This variable outputs the site name, which is typically defined in the site settings.
    • page.header: This renders the content assigned to the header region via Drupal’s Block Layout system.

Step 3: Custom Footer Template

Now let’s create a custom footer template to display the footer region and any additional content like the copyright notice.

  1. Create a Custom Footer Template:Create a file called footer.html.twig in the same templates/layout folder:
    themes/custom/acesoftech/templates/layout/footer.html.twig
  2. Add Custom Footer Content:Inside footer.html.twig, you can add custom markup and display the content from the footer region.
    <footer class="custom-footer">
    <div class="footer-region">
    {{ page.footer }}
    </div>
    <p>&copy; {{ "now"|date("Y") }} Acesoftech</p>
    </footer>
    • page.footer: This renders any blocks or content placed in the footer region in the Block Layout.

2. Include the Custom Header and Footer in page.html.twig

With the header and footer templates ready, we need to include them in the main page.html.twig template to display the content correctly.

  1. Edit page.html.twig:Create or modify the page.html.twig file located at:
    themes/custom/acesoftech/templates/layout/page.html.twig
  2. Include the Custom Header and Footer:Now, let’s include the custom header and footer templates using the {% include %} tag.
    twig
    <div class="page-wrapper">
    {% include '@acesoftech/layout/header.html.twig' %}
    <main role=”main”>
    {{ page.content }}
    </main>

    {% include ‘@acesoftech/layout/footer.html.twig’ %}
    </div>

    • {% include '@acesoftech/layout/header.html.twig' %}: This line includes the custom header template we created earlier.
    • {% include '@acesoftech/layout/footer.html.twig' %}: This line includes the custom footer template.

3. Using {{ page.header }} vs {% include '@acesoftech/layout/header.html.twig' %}

Here comes the main confusion: should you use {{ page.header }} or include a separate header template using {% include %}?

Option 1: Using {{ page.header }}

If you use {{ page.header }}, Drupal will output all the blocks and content placed in the header region without any additional structure. It’s simple and works perfectly if you don’t need to customize the surrounding HTML or CSS.

For example:

twig
<header>
<h1 class="site-title">{{ site_name }}</h1>
{{ page.header }}
</header>
  • Pros:
    • Quick and simple.
    • Renders the content assigned to the header region.
  • Cons:
    • Limited customization.
    • Doesn’t allow for additional wrapping, custom HTML, or extra functionality.

Option 2: Using {% include '@acesoftech/layout/header.html.twig' %}

Using {% include '@acesoftech/layout/header.html.twig' %} gives you more control. You can fully customize the header’s HTML structure, add extra classes, logic, or even static content.

For example, if you want to have a custom header layout, you can create header.html.twig as described earlier and include it in page.html.twig.

  • Pros:
    • Full control over the header structure.
    • Can add static content, custom logic, and custom styling.
    • Easier to maintain in larger projects.
  • Cons:
    • Requires more setup.

Conclusion: Which One Should You Use?

  • Use {{ page.header }} if you’re building a simple site and want a quick and easy way to render the content in the header region.
  • Use {% include '@acesoftech/layout/header.html.twig' %} if you need more control over the layout, structure, or styling of your header (or footer) content.

Both methods work, but {% include %} provides greater flexibility for complex designs. For a simple and quick setup, {{ page.header }} will suffice. However, for a modular, customizable, and scalable solution, including separate header and footer templates with {% include %} is the recommended approach.

By following these steps, you can create custom headers and footers in Drupal 10 that meet your project’s needs, while also understanding when and why to choose one method over the other.

Leave a Reply

Your email address will not be published. Required fields are marked *