Reusing Code Across Multiple Pages

Last updated on June 26, 2024.

This document details how you can reuse code across multiple pages using partials. Partials are pieces of code extracted to their own separate files to maintain code readability and follow the rule of DRY – Don’t Repeat Yourself.

Important

You can create and manage partials using the Instance Admin. Please visit our Instance Admin documentation on Partials for more details.

Prerequisites

To follow the steps in this tutorial, create a page and a layout:

Steps

Reusing code across multiple pages using partials is a two-step process:

  • Step 1: Create a partial
  • Step 2: Include the partial

Step 1: Create a partial

This tutorial uses the previously created layout:

Create a partial file in the directory. The content of the partial is the extracted meta tags from the layout:

Step 2: Include the partial

In the original layout, include the partial. Partials resolve from , so in the include, you only have to specify the name of the partial in the directory.

Deploy or sync your changes. Pages using the layout are displayed the same. When you check their source code, you can learn that the meta tags have been injected into the layout and, thus, the pages from the partial.

You can use partials with CSS, Javascript, etc. or include , tags, etc.

Next Steps

Congratulations! You have reused code across multiple pages using partials. Now you can learn about using assets: Using Static Assets on Your Pages

Have a suggestion for this page?

Didn't quite find what you are looking for or have feedback on how we can make the content better then we would love to hear from you. Please provide us feedback and we will get back to you shortly.