Anatomy of a theme - RelationalExtend/RelationalExtend GitHub Wiki
##The fuel/themes folder:
- /theme_name/classes/theme_name/Info.php (Theme Info)
- /theme_name/css/ (Theme CSS files)
- /theme_name/images/ (Theme Images)
- /theme_name/js/ (JS Files)
- /theme_name/layouts/ (Layout files for lex parser - .html)
- /theme_name/partials/ (Partial layout files for lex parser - .html)
All these files and folders have to be present for the theme to be recognized in the CMS.
##Info.php
<?php
/**
* Sample theme info file
*
* @author Ahmed Maawy
* @copyright 2011 - 2012 Ahmed Maawy
*/
namespace sample;
class Sample_Info extends \ThemeInfo implements \IInfo {
/**
* Get extension meta data
*
* @return void
*/
public function info()
{
$this->build_theme_data("Bootstrap Theme", "Built on Twitter Bootstrap", "", time(), "1.0");
$this->build_theme_layout_data("layout.html", "Main layout file", "", 0);
$this->build_theme_partial_data("partial.html", "Main partial file", "", 0);
$this->build_theme_javascript_data("bootstrap.js", "Sample JS file", "");
$this->build_theme_style_data("bootstrap.css", "Sample CSS file", "");
return array(\CMSTheme::THEME_PREFIX => $this->theme_data,
\CMSTheme::LAYOUT_PREFIX => $this->layout_data,
\CMSTheme::PARTIAL_PREFIX => $this->partial_data,
\CMSTheme::JS_PREFIX => $this->js_data,
\CMSTheme::CSS_PREFIX => $this->css_data
);
}
}
Building Theme data items
protected function build_theme_data($name, $description, $install_folder, $install_time, $active = 0, $version = "1.0")
protected function build_theme_layout_data($layout_file, $name, $content, $theme_id, $active = 1, $default = 0)
protected function build_theme_partial_data($layout_file, $name, $content, $theme_id, $active = 0)
protected function build_theme_javascript_data($javascript_file, $name, $content)
protected function build_theme_style_data($style_file, $name, $content)
build_theme_data can only be called once. It contains data about the theme itself. layout_data, partial_data, javascript_data and style_data can be called more than once and build up an array. It will pick up only files present in this array and install these bits into a database table.
##Theme file sample
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
{{ theme:js slug="sample_js_file" }}
{{ theme:css slug="sample_css_file" }}
<title></title>
</head>
<body>
<ul>
{{ theme:partial slug="partial_theme" }}
{{ page:pages }}
<li>{{ page_title }}</li>
{{ /page:pages }}
</ul>
</body>
</html>
The above is a sample theme.
- {{ theme:js }} calls a Javascript segment
- {{ theme:js }} calls a Stylesheet segment
- {{ theme:partial }} calls a partial template
Additionally, the above page makes use of the page extension's plugin and calls the pages function. This plugin in located in the Plugin.php file for the extension. This is how the plugin is structured:
<?php
/**
* Plugin file for the Pages extension
*
* @author Ahmed Maawy
* @copyright 2011 - 2012 Ahmed Maawy
*/
namespace page;
class Page_Plugin implements \IPlugin {
public function pages($attributes, $content)
{
$pages = array();
if(isset($attributes["slug"])) {
$slug = $attributes["slug"];
$pages = Page::get_page_by_slug($slug);
}
else {
$pages = Page::get_pages();
}
return $pages;
}
}
In the example above, we can pass a parameter to the plugin's page function (called slug). So we may do the following:
{{ page:pages slug="abcd" / }}
##Creating a public controller to access a theme file
Below is an example of how you can call the theme layout from a public controller
<?php
/**
* Main Public Controller for the Page Module
*
* @author Ahmed Maawy and Nick Hargreaves
* @copyright 2011 - 2012 Ahmed Maawy
*/
namespace page;
class Controller_Page extends \Controller_Public {
public function action_index($page_slug)
{
return $this->render_layout("main_layout_file", array("page_slug" => $page_slug));
}
}
The second parameter contains parameters passed to the layouts processors. You may use these parameters within an extension's Extension.php file as follows:
<?php
/**
* Extension class for the Pages extension
*
* @author Ahmed Maawy
* @copyright 2011 - 2012 Ahmed Maawy
*/
namespace page;
class Page_Extension implements \IExtension {
/**
* Execute the extension
* @param array $parameters
* @return void
*/
public function execute_extension($parameters = array())
{
if(isset($parameters["page_slug"])) {
// Process and return the page's content for that page slug
return array("content" => $page_content);
}
}
}
This will return the following variable that can be accessed by the template: {{ page_content }}