Creating Themes
Themes are loaded through the theme_handler. The definitions.php file defines what theme is loaded at run-time. It looks for the theme in the theme folder and loads the loader.php file in the config directory.
if (!defined('SITE_THEME')) {
define('SITE_THEME', "{theme name}");
} else {
die("definition error");
}
A
theme requires both a loader.php and a routing.php in the config
directory. The examples giving below are missing parts of the code to
shorten the page. It merely serves as an example how to setup these
files.
Loader.php
The loader.php file is used to include files and set options before the actual theme starts to render.
/*
* config loader.php
* load the theme options.
* Copyright 2022 (c) by Patriek Jeuriens.
*/
if (!defined('ROOT')) {
die('Direct access not permitted');
}
global $factory, $parser, $reactor, $routing;
// site options
$reactor->option['api'] = true;
$reactor->option['feed'] = true;
// site defaults
$reactor->defaults->grid['width'] = 240;
$reactor->defaults->grid['height'] = 240;
$reactor->defaults->grid['description'] = 150;
$reactor->defaults->row['width'] = 100;
$reactor->defaults->row['height'] = 100;
$reactor->defaults->avatar['width'] = 200;
$reactor->defaults->avatar['height'] = 200;
Routing.php
The routing.php file is used to route urls to templates. Without this file nothing will happen.
/*
* config routing.php
* define routing structure for theme.
* Copyright 2019 (c) by Patriek Jeuriens.
*/
if (!defined('ROOT')) {
die('Direct access not permitted');
}
global $reactor,$routing;
/**
* Route for the homepage
*
* param string $action name for the route.
* param array $params params from the url.
* return string template to be loaded.
*/
function route_home($action, $params)
{
global $reactor, $routing;
//parse variables
$reactor->page->query_vars['action'] = $action;
$reactor->site->locale = $params[1];
if (!empty($params[2])) {
$routing->additions($params[2]);
}
$object = new core_object();
$result = $object->load("home", "slug");
if ($result) {
$reactor->page->object = $object;
if ($object->property('template') && !isset($reactor->page->query_vars['function'])) {
$template = $object->property('template');
$reactor->page->title = ucfirst($object->name);
// check to route between plugin or template directory.
$reactor->page->template = $reactor->site->templates->page[$template];
if (isset($reactor->site->templates->page[$template]['path']) && $reactor->site->templates->page[$template]['path'] == "plugin") {
$routing->plugin(200, $reactor->site->templates->page[$template]['file']);
} else {
$routing->template(200, $reactor->site->templates->page[$template]['file']);
}
} else {
$reactor->page->title = ucfirst(__("Home"));
$routing->template(200, $reactor->defaults->frontpage);
}
} else {
$reactor->page->object = new core_object();
$reactor->page->title = ucfirst(__("Create your Homepage"));
$routing->template(200, "pages/start.php");
}
}
/**
* Route for download.
*
* param string $action name for the route.
* param array $params params from the url.
* return void;
*/
function route_download($action, $params)
{
global $reactor, $routing;
// parse variables
$reactor->page->query_vars['action'] = $action;
$reactor->site->locale = $params[1];
$reactor->page->query_vars['object'] = $params[2];
$reactor->page->object = new core_object();
$reactor->page->object->load($reactor->page->query_vars['object']);
// increase download count
$reactor->page->object->download();
// route to download
$routing->download(200,$reactor->page->object->url());
reactor_exit();
}
/**
* Register headers in the routing class.
*/
$routing->register_header("csp", "Content-Security-Policy: script-src 'strict-dynamic' 'nonce-" . $reactor->defaults->nonce . "';");
$routing->register_header("xframe", "X-Frame-Options: SAMEORIGIN");
//$routing->register_header("cache", "Cache-Control: max-age=30, public");
$routing->register_header("cache", "Cache-Control: no-cache, public");
$routing->register_header("hsts", "strict-transport-security: max-age=0"); // hsts off
//$routing->register_header("hsts","strict-transport-security: max-age=31536000 "); // hsts on for a year
$routing->register_header("ch", "Accept-CH: Width, Viewport-Width");
/**
* Register routes to routing class.
* All templates name are retrieved from the loader.php file.
*/
$console_templates = implode("|",array_keys($reactor->site->templates->console));
$system_templates = implode("|",array_keys($reactor->site->templates->system));
$routing->register_route('style', "/([\w]{2}_[\w]{2})/style", "route_style");
$routing->register_route('script', "/([\w]{2}_[\w]{2})/script", "route_script");
$routing->register_route("download", "/([\w]{2}_[\w]{2})/download/([\w-]+)", "route_download");
$routing->register_route("console", "/([\w]{2}_[\w]{2})/" . $reactor->site->query_tags['console'] . "/(".$console_templates.")(.*)", "route_console");
$routing->register_route("search", "/([\w]{2}_[\w]{2})/" . $reactor->site->query_tags['search'] . "/(everything|objects|containers)/([^/]+)(.*).*(?=\?)?", "route_search");
$routing->register_route("user-message", "/([\w]{2}_[\w]{2})/" . $reactor->site->query_tags['user'] . "/([\w-]+)/(.*)/([\w-]+)(.*).*(?=\?)?", "route_user");
$routing->register_route("user", "/([\w]{2}_[\w]{2})/" . $reactor->site->query_tags['user'] . "/([\w-]+)(.*).*(?=\?)?", "route_user");
$routing->register_route('system', "/([\w]{2}_[\w]{2})/(login|logout|activate|twofactor|registration|forgot|register)(.*).*(?=\?)?", "route_system");
$routing->register_route("object", "/([\w]{2}_[\w]{2})/" . $reactor->site->query_tags['object'] . "/([\w-]+)(.*).*(?=\?)?", "route_object");
$routing->register_route("instance", "/([\w]{2}_[\w]{2})/" . $reactor->site->query_tags['instance'] . "/([\w-]+)(.*).*(?=\?)?", "route_instance");
$routing->register_route("container", "/([\w]{2}_[\w]{2})/" . $reactor->site->query_tags['container'] . "/([\w-]+)(.*).*(?=\?)?", "route_container");
$routing->register_route("playlist", "/([\w]{2}_[\w]{2})/playlist/([\w-]+)(.*).*(?=\?)?", "route_container");
$routing->register_route("term", "/([\w]{2}_[\w]{2})/" . $reactor->site->query_tags['term'] . "/([\w\-]+)(.*).*(?=\?)?", "route_term");
$routing->register_route("taxonomy", "/([\w]{2}_[\w]{2})/" . $reactor->site->query_tags['taxonomy'] . "/([\w\-]+)(.*).*(?=\?)?", "route_taxonomy");
$routing->register_route('home_language', "/([\w]{2}_[\w]{2})/(.*)", 'route_home');
$routing->register_route('home', "/(.*)", "route_home");