Plain PHP Snippets

In case you want to develop a custom function or an extension, or use inline PHP in your templates, the collection of common code snippets below might help you to get started quickly. Within templates, the $Automad object is always available. Custom functions and extensions receive it as an argument.

Context and Pages

Getting the current page:

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

Getting any page by its URL:

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

Getting the title or any other variable of a page:

$title = $Page->get('title');

Getting the parent page:

$Parent = $Automad->getPage($Page->parentUrl);

Getting all pages of the site as an array with the URLs as keys:

$pages = $Automad->getPages();

Shared Content

Getting the name of the site or any other shared variable:

$sitename = $Automad->Shared->get('sitename');

String Functions

All built-in filters that are not plain PHP functions are methods of the Str class and can be used in PHP as well.

Rendering a Markdown variable:

use Automad\Core\Str;

$output = Str::markdown($Page->get('textTeaser'));

Sanitizing a value:

$output = Str::sanitize($Page->get('title'));

Shortening a text and defining a default:

$output = Str::def(Str::shorten($Page->get('textTeaser'), 100), 'No text');

Query Strings

Getting a query string parameter of the current request:

use Automad\Core\Request;

$filter = Request::query('filter');

Pagelists

Configuring the pagelist and getting the resulting pages:

$Automad->Pagelist->config(array(
  'type' => 'children',
  'sort' => 'date desc'
));

$pages = $Automad->Pagelist->getPages();

Iterating the array of pages:

foreach ($pages as $Page) {
  echo '<p>' . $Page->get('title') . '</p>';
}

Rendering Templates

Rendering a template file within the current context and returning the output, for example inside a custom function:

$html = inc(__DIR__ . '/snippets/card.php', $Automad);
Useful Template Snippets System Guide