Plain PHP

In case you are missing any feature or tool and you don't want to write an extension for just that particular functionality to keep things simple, you can also use plain PHP within a template file. Using plain PHP code within template files works basically the same like writing extensions in terms of available classes and methods. Tough, it is still recommended to wrap all code snippets into an extension.

Like with any extension, an instance of the Automad class is also available within a template, called $Automad. That object stores all your site's data and pages.
Basically the $Automad objects is a superior collection of many smaller units, the page objetcs, like a whole site constists of many pages as well.

As a little cheat sheet, here are the most common methods to be used:

Getting Pages

Getting the current page object:

$page = $Automad->getCurrentPage(); 

The $page object stores now all data related to the currently viewed page.
The best way to get familiar with page objects is to do a simple:

print_r($page);

You can also get the full page collection, an array consisting of all the site's page objects:

$collection = $Automad->getCollection();

Now, $collection is an associative array of all your pages.
To get a certain page's object out of that array, you can use its URL as key:

$page = $collection["/url/to/page"];

Selections

To cut down the collection of pages according to some criterias, you can create your own selection of page objects like this:

// Create a selection object based on the full collection.
$selection = new \Automad\Core\Selection($Automad->getCollection());
// Use some methods of the Selection class to filter the collection.
// For example, here the collection gets reduced to all children of the home page.
$selection->filterByParentURL("/");
// When filtering is done, you can store the array of the selected pages in any variable.
$pages = $selection->getSelection();

Now your $pages array consists of all page objects, matching the filters (or any other criteria). You can easily loop through the selection like this:

// Do some stufs with each page here.
// As an example you could use the Html class' method below for adding list of links for each page.
echo "<ul>";
foreach ($pages as $page) { 
    echo "<li>" . \Automad\Core\Html::addLink($page) . "</li>";
}
echo "</ul>";

Please note, that the example above is basically imitating the functionality of the Html::generateList() method for clearification. For most things, it is not neccassary at all to write your own loops or functions. Most functionality is already covered by Automad's Html class.


API Reference

There is also a full API reference for a more detailed documentation on all classes and their methods. The most important classes to be used within template are:

  • \Automad\Core\Html
  • \Automad\Core\Image
  • \Automad\Core\Selection
  • \Automad\Core\Listing