Skip to content

Instantly share code, notes, and snippets.

@LukeTowers
Created August 14, 2017 19:27
Show Gist options
  • Save LukeTowers/d712d8a0085b7b51d968d5b62e99ca65 to your computer and use it in GitHub Desktop.
Save LukeTowers/d712d8a0085b7b51d968d5b62e99ca65 to your computer and use it in GitHub Desktop.
Static Page display component
{{ __SELF__.content | raw }}
<?php namespace LukeTowers\Examples\Components;
use Config;
use Cms\Classes\Theme;
use Cms\Classes\ComponentBase;
use RainLab\Pages\Classes\Page as StaticPage;
class StaticPage extends ComponentBase
{
/**
* HTML to render
* @var string
*/
public $content;
/**
* Register the component details
*/
public function componentDetails()
{
return [
'name' => 'Static Page',
'description' => 'Displays the content of the selected Static Page',
];
}
/**
* Define the component properties
*/
public function defineProperties()
{
return [
'static_page' => [
'title' => 'Static Page',
'description' => 'The static page to display with this component.',
'default' => 'default_static_page.htm',
'type' => 'dropdown',
],
];
}
/**
* Get the options for the fallback_page property
*
* @return array $options ['filename' => 'pageTitle']
*/
public function getFallback_PageOptions()
{
// Load all staticPages
$staticPages = StaticPage::listInTheme($this->getCurrentTheme(), true)->all();
// Return an array of options in the form of ['filename' => 'pageTitle']
return array_combine(array_keys($staticPages), collect($staticPages)->pluck('title')->all());
}
/**
* Initialize the component for requests
*/
public function init()
{
$this->prepareVars();
}
/**
* Prepare the variables and properties for this request
*/
public function prepareVars()
{
$staticPage = $this->getPage($this->property('static_page'));
$this->content = $staticPage ? $staticPage->getProcessedMarkup() : null;
}
/**
* Gets the current theme to use for finding pages
*
* @return mixed $currentTheme
*/
protected function getCurrentTheme()
{
return Theme::load(Config::get('cms.activeTheme'));
}
/**
* Gets a page object in the current theme from a filename
*
* @param string $fileName
* @return StaticPage
*/
public function getPage($fileName)
{
return StaticPage::load($this->getCurrentTheme(), $fileName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment