Inline PHP

Since templates are regular .php files, Automad also supports plain PHP within templates. However, variables that are only used in PHP code don't show up automatically in the dashboard and mixing PHP with the template language should be done carefully. Therefore it is not recommended to use large amounts of plain PHP in templates. Missing functionality should rather be implemented as a custom function or an extension, where the PHP code is nicely wrapped into a statement that integrates well with the rest of a template.

Process Flow

When a template file is loaded, its PHP code is executed first. Only the output of that PHP code is then processed by the template engine. That has some important consequences:

  • PHP code can generate template language code that is then processed by the template engine.
  • PHP code is executed only once per file and doesn't know anything about the context of loops or with statements that wrap it. Inside of a foreach loop, PHP code is therefore not executed for each page.
  • Variables that are defined during rendering using the set function are not available yet when the PHP code is executed.

PHP is therefore a good fit for values that don't depend on the context, like constants or cookies. The Standard Lite theme, for example, uses a small piece of PHP in order to read the color theme that was selected by the visitor from a cookie:

<@ set { :colorTheme: '<?php echo $_COOKIE['std-user-selected-theme'] ?? ''; ?>' } @>

The Automad Object

An instance of the Automad class is always available within templates as $Automad. That object provides access to all pages and data of your site.

Getting Pages

Getting the page that represents the current context:

$Page = $Automad->Context->get();

Other pages can be accessed by their URL. A URL is always the main reference to a page in Automad. Note that URLs are always relative to the homepage, which is represented by a single slash /.

$Page = $Automad->getPage('/url');

To get any data from a page, you can use the get() method. In case a field is not defined for a page, the value of the shared data is returned instead:

<h1><?php echo $Page->get('title'); ?></h1>

Pagelists

The pagelist can be accessed and configured as follows:

<?php

use Automad\Core\Request;
use Automad\Core\Str;

$Automad->Pagelist->config(array(
  'type' => 'children',
  'sort' => Str::def(Request::query('sort'), 'date desc')
));

foreach ($Automad->Pagelist->getPages() as $Page) {
  echo '<a href="' . $Page->url . '">' . $Page->get('title') . '</a>';
}

The available options are the same as for the newPagelist function.

Useful Classes

The most important classes to start with are:

Automad\Core\Automad
The main object that holds all pages, the current context, the pagelist and the filelist.
Automad\Models\Page
A single page and its data.
Automad\Models\Pagelist
The pagelist collection.
Automad\Core\Str
String helpers that are also used as filters, like Str::def(), Str::shorten() or Str::dateFormat().
Automad\Core\Request
Helpers to access request data, like Request::query().
Automad\Core\Image
Image processing for resizing and cropping.
Custom Block Styles Extensions