Themes

A theme changes the look of your site. In its simplest form a theme generates the surrounding HTML markup for the content output of your extensions.

Note The examples in this tutorial are taken from the Hello theme, which is available in the Marketplace. When installed, the Hello theme is located in /packages/pagekit/theme-hello.

Package definition

A theme is a regular Pagekit package of the type pagekit-theme. Each package needs a description in order to be recognized by Pagekit. This description is located in the composer.json file and looks as follows. For detailed information, take a look the Packages chapter.

  1. {
  2. "name": "pagekit/theme-hello",
  3. "type": "pagekit-theme",
  4. "version": "0.9.0",
  5. "title": "Hello"
  6. }

Module definition

A theme in itself is simply a module. So you may want to read up on modules first. This opens up a lot of possibilities with regard to what a theme can do.

Define the positions and menus of your theme, load additional scripts and much more. Here is a shortened example of the index.php to get you started. Explanations of the theme specific properties follow below.

  1. return [
  2.  
  3. 'name' => 'theme-hello',
  4.  
  5. /**
  6. * Define menu positions.
  7. */
  8. 'menus' => [
  9.  
  10. 'main' => 'Main',
  11.  
  12. ],
  13.  
  14. /**
  15. * Define widget positions.
  16. */
  17. 'positions' => [
  18.  
  19. 'sidebar' => 'Sidebar',
  20.  
  21. ]
  22.  
  23. ];

Your theme defines locations to render menus and widgets. The actual rendering happens in the template.php, as is demonstrated below. However, your theme needs to register these positions before. This happens with the menus and positions property. These contain arrays of the position name and a label, which is displayed in the admin panel.

Menus

In your theme, you can render menus from the Pagekit system in as many positions as you want. To make these positions known to Pagekit, you need to register them using the menus property.

Each menu position is defined by an identifier (e.g. main) and a label to be displayed to the user (e.g. Main).

  1. 'menus' => [
  2.  
  3. 'main' => 'Main',
  4. 'offcanvas' => 'Offcanvas'
  5.  
  6. ],

Positions

Widget positions allow users to publish widgets in several locations of your theme markup. They appear in the Widgets area of the Pagekit admin panel and can be selected by the user when setting up a widget.

Each widget position is defined by an identifier (i.e. sidebar) and a label to be displayed to the user (i.e. Sidebar).

  1. 'positions' => [
  2.  
  3. 'sidebar' => 'Sidebar',
  4.  
  5. ],

Layout file

Apart from the mandatory module files, a theme brings its own views/template.php file. It is the main file for the theme's markup with the following objects available for rendering.

ObjectDescription
$viewView renderer instance
$paramsTheme parameters
$appApplication container instance

Note With PHP templating, the short notation <?= $var ?> prints the value of the the variable $var.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7.  
  8. <!-- Always render head section from the system -->
  9. <?= $view->render('head') ?>
  10.  
  11. <!-- Include theme css -->
  12. <?php $view->style('theme', 'theme:css/theme.css') ?>
  13.  
  14. <!-- Include theme JS, require jQuery which will also be included -->
  15. <?php $view->script('theme', 'theme:js/theme.js', 'jquery') ?>
  16. </head>
  17. <body>
  18.  
  19. <!-- Render logo with site URL -->
  20. <?php if ($logo = $params['logo']) : ?>
  21. <a href="<?= $view->url()->get() ?>">
  22. <img src="<?= $this->escape($logo) ?>" alt="">
  23. </a>
  24. <?php endif ?>
  25.  
  26. <!-- Render menu position -->
  27. <?php if ($view->menu()->exists('main')) : ?>
  28. <?= $view->menu('main') ?>
  29. <?php endif ?>
  30.  
  31. <!-- Render widget position -->
  32. <?php if ($view->position()->exists('sidebar')) : ?>
  33. <?= $view->position('sidebar') ?>
  34. <?php endif; ?>
  35.  
  36. <!-- Render system messages -->
  37. <?= $view->render('messages') ?>
  38.  
  39. <!-- Render content -->
  40. <?= $view->render('content') ?>
  41.  
  42. <!-- Insert code before the closing body tag -->
  43. <?= $view->render('footer') ?>
  44.  
  45. </body>
  46. </html>

Menu and Position renderer

You might want to use a custom menu or position renderer. Below you'll find two examples of how to use them.

  1. <?= $view->menu('main', 'menu-navbar.php') ?>

In this case the main menu will be rendered with the menu-navbar.php layout file.

  1. <ul class="uk-navbar-nav">
  2.  
  3. <?php foreach ($root->getChildren() as $node) : ?>
  4. <li>
  5.  
  6. <!-- ... more markup ... -->
  7.  
  8. </li>
  9. <?php endforeach ?>
  10.  
  11. </ul>

The same works for widget positions.

  1. <?= $view->position('hero', 'position-grid.php') ?>

Here, the widget position hero will be rendered with the position-grid.php layout file.

  1. <?php foreach ($widgets as $widget) : ?>
  2. <div class="uk-width-1-<?= count($widgets) ?>">
  3.  
  4. <div>
  5.  
  6. <h3><?= $widget->title ?></h3>
  7.  
  8. <?= $widget->get('result') ?>
  9.  
  10. </div>
  11.  
  12. </div>
  13. <?php endforeach ?>

Default Pagekit markup

The Pagekit admin panel is built using the UIkit frontend framework. That is why Pagekit core extensions, like static pages and the blog, output markup with CSS classes from UIkit. You are, however, in no way obliged to use UIkit to create your own themes.

To style the Pagekit system output, you can just add the CSS for a few classes instead of including the entire UIkit CSS. The theme.css file that comes with the Hello extension already includes the necessary classes.

If you want to completely change the markup that Pagekit itself generates, you also have the possibility to overwrite system view files.

Overwrite system views

To overwrite system view files, you just need to create corresponding folders inside your theme to mimic the original structure and put the template files there, as shown in the table below.

FileOriginal view fileDescription
views/system/site/page.php/app/system/site/views/page.phpDefault static page view
views/blog/post.php/packages/pagekit/blog/views/post.phpBlog post single view
views/blog/posts.php/packages/pagekit/blog/views/posts.phpBlog posts list view

To understand which variables are available in these views, check out the markup in the original view file.

Add theme options to the Site interface

This is done via JavaScript, most comfortably when you make use of Vue components.

Load your own JS when the Site Tree interface is currently active. In your index.php, you can do this when you listen to the right event.

  1. 'events' => [
  2. 'view.system/site/admin/settings' => function ($event, $view) use ($app) {
  3. $view->script('site-theme', 'theme:js/site-theme.js', 'site-settings');
  4. $view->data('$theme', $this);
  5. },
  6. // ...
  7. ],

The js/site-theme.js contains a Vue component which renders the interface and takes care of storing of the theme settings.

Note Although it's possible to do all of this in a single JS file and have the markup represented in a string, the best practice is to actually create *.vue files with your Vue component. Examples can be found in the app/components folder of the default One theme.

  1. window.Site.components['site-theme'] = {
  2.  
  3. section: {
  4. label: 'Theme',
  5. icon: 'pk-icon-large-brush',
  6. priority: 15
  7. },
  8.  
  9. template: '<div>Your form markup here</div>',
  10.  
  11. data: function () {
  12. return window.$theme;
  13. },
  14.  
  15. events: {
  16.  
  17. save: function() {
  18.  
  19. var config = _.omit(this.config, ['positions', 'menus', 'widget']);
  20.  
  21. this.$http.post('admin/system/settings/config', {name: this.name, config: config}).error(function (data) {
  22. this.$notify(data, 'danger');
  23. });
  24.  
  25. }
  26.  
  27. }
  28.  
  29. };

Add a Theme tab to the Node configuration in the Site Tree

Often you want to attach theme options to a specific Node in the Site tree. For example, you want to allow the user to pick a Hero image, which can be different per page. To do so, we can add a Theme tab to the Site interface.

  1. 'events' => [
  2.  
  3. // ...
  4.  
  5. 'view.system/site/admin/edit' => function ($event, $view) {
  6. $view->script('node-theme', 'theme:js/node-theme.js', 'site-edit');
  7. },
  8.  
  9. // ...
  10. ];

Example for js/node-theme.js:

  1. window.Site.components['node-theme'] = {
  2.  
  3. section: {
  4. label: 'Theme',
  5. priority: 90
  6. },
  7.  
  8. props: ['node'],
  9.  
  10. template: '<div>Your form markup here</div>'
  11.  
  12. };

Note Compare with full Vue components in the app/components folder of the default One theme.

Add theme options to the Widget interface

Register a script to be loaded in the Widget edit view.

  1. 'view.system/widget/edit' => function ($event, $view) {
  2. $view->script('widget-theme', 'theme:app/bundle/widget-theme.js', 'widget-edit');
  3. },

Example for widget-theme.js:

  1. window.Widgets.components['widget-theme'] = {
  2.  
  3. section: {
  4. label: 'Theme',
  5. priority: 90
  6. },
  7.  
  8. props: ['widget', 'config'],
  9.  
  10. template: '<div>Your form markup here</div>'
  11.  
  12. };

Note Compare with full Vue components in the app/components folder of the default One theme.

Manually add a settings screen

If the prepared ways of adding a settings screen do not satisfy your needs, you can also manually create a completely new interface. With the module definition in the index.php file you have full control and can do something like the following:

  • Create a View file for your settings screen.
  • Create a new Controller with an action that renders the view file.
  • Register controller and routes inside your index.php
  • Optional: Set the settings property to the new settings screen route