-
Notifications
You must be signed in to change notification settings - Fork 9
Partials
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.
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.
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
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 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.
/partials
└──partial1.html.php
└── partial2.html.php
To import the partials, use:
<?= import('/partials/partial1'); ?>
<?= import('/partials/partial2'); ?>
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.
<?= 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.
Got a question or need help? We have a forum on Github Discussions where you can get in touch with us.