Skip to content
Gary Barclay edited this page Apr 28, 2021 · 19 revisions

Partials are generally small and reusable chunks of code. You can build sections of a page and reuse them on different pages without the need to repeat yourself. In other words, partials help you keep your templates clean and DRY.

Partials can live anywhere but for the sake of consistency it is best to place them in the /partials directory.

Table of contents

The import() function

The import($path, $data = array()) template function allows for the importing of a partial into a page or into another partial. For the sake of consistency, it is best to place them in the /partials directory.

Locating partials

However, you can import partials relative to the root pages folder /pages. The path to resolve the location of the partial can be both relative to the folder the partial is imported in, or absolute to the root pages folder.

The following partials are resolved against the root pages folder:

  • file.html
  • folder/file.html

The following partials are resolved relative to the folder the partials are imported into.

  • ../file.html
  • ../folder/file.html

Passing variables

You can also pass variables to a partial. For example, suppose you have a partial called note.html in your /partials directory that contains this formatting:

<h3><?= $title ?></h3>
<div>
   <?= $content ?>
</div>

The <?= $note ?> is a variable that gets populated when you import the partial and specify a value for that parameter, like this:

<?= import('/partials/note.html, [
    'title' => 'My Note Title',  
    'content' => "This is my sample note."
]) ?>

The value of content (which is "This is my sample note") will be inserted into the $content variable.

Passing variables to includes is especially helpful when you want to hide complex formatting away from your Markdown content.

Importing in a partial

Importing partials into other partials is as simple as:

<?= import('/partials/file'); ?>

If you want to have multiple partials in your /pages folder, you should have an individual folder for each partial.

Example:

/partials
  └──partial1.html.php
  └── partial2.html.php

To import the partials, use:

<?= import('/partials/partial1'); ?>
<?= import('/partials/partial2'); ?>

Importing a partial from a Joomla Template

Joomlatools Pages allows you to import a partial from within the active Joomla theme. This can be done by using theme:// which is equivalent to the actual path of your active Joomla template.

Example:

<?= import('theme://includes/folder/file'); ?>

is equivalent to:

<?= import('/root/templates/[active]/includes/folder/file'); ?>

This will include the following file /root/templates/[active]/includes/folder/file.php where [active] is the active theme in Joomla.