Sections
Composer is the recommended way of installing SilverStripe modules., (*1)
composer require plato-creative/sections
Add a sections area to a page type., (*2)
class MyCustomPage extends Page { private static $extensions = array( 'Sectioned' ) }
By default this will add a sections area that can be accessed via page templates with {$Area}
, (*3)
To add multiple sections to a page type., (*4)
class MyCustomPage extends Page { private static $extensions = array( 'Sectioned' ) private static $areas = array( 'Sections' => 'Sections' // make sure you add standard sections area in other it will be deleted 'OtherSections' => 'Other sections cms title' ) }
This can be accessed in the page template with:, (*5)
{$Area}{$Area('OtherSections')}
Set allowed sections., (*6)
class MyCustomPage extends Page { private static $extensions = array( 'Sectioned' ) private static $allowed_sections = array( 'ContentSection', 'BannerSection' ) }
Set excluded sections., (*7)
class MyCustomPage extends Page { private static $extensions = array( 'Sectioned' ) private static $exclude_sections = array( 'FormSection' ) }
class MyCustomSection extends Section { // Defines the name used in the cms such as a new dropdown and gridfield type. private static $singular_name = 'My custom name'; private static $plural_name = 'Sections'; // Define db fields private static $db = array( 'Content' => 'HTMLText' ); // Define a list db fields that can be searched via the frontend private static $site_searchable_fields = array( 'Content' ); // Defines available layouts for this section selectable in the cms private static $layouts = array( 'left-text' => 'Left text', 'right-text' => 'Right text', 'center' => 'Center', ); // Defines available color schemes for this section selectable in the cms private static $colors = array( 'black' => 'White text on black', 'blue' => 'White text on blue background' ); // Defines a custom css class for this section private static $base_class = 'my-custom-css-class'; // Defines if the title of the section will be forced to hide from public display. private static $title_force_hide = true; public function getCMSFields() { $fields = parent::getCMSFields(); // This is required as sections will add its own fields $fields->addFieldsToTab( "Root.Main", array( HTMLEditorField::create( 'Content', 'Content' ) ) ); $this->extend('updateCMSFields', $fields); return $fields; } }
class MyCustomSectionController extends SectionController { private static $allowed_actions = array( 'Form' ); public function Form(){ $fields = FieldList::create(array( TextField::create('Name'), EmailField::create('Email'), TextField::create('Phone'), TextAreaField::create('Message') )); $actions = FieldList::create( FormAction::create('submit', 'Send Enquiry') ); return Form::create($this, 'Form', $fields, $actions); } public function submit($data, $form){ // process form data as usual // ... // redirect return $this->redirect($this->CurrentPage->Link() . '?contacted=1'); } }
Sections will look for the template based on the section section name in you theme template directory. e.g. MyCustomSection will look for MyCustomSection.ss., (*8)
In addition sections will look for templates that have a specific layout appended to it. e.g. MyCustomSection_left-text.ss, (*9)
Sections will also look for templates that are specific to a page type. e.g. MyCustomSection_homepage.ss, (*10)
Finally sections will look for templates that match both specific layout and page type. e.g. MyCustomSection_homepage_left-text.ss, (*11)
Taking the following conditions page classname = HomePage, section classname = MyCustomSection, section layout = left-text and extends MyParentSection we can see the templates that are searched and their priority from first to last., (*12)
MyCustomSection_homepage_left-text.ss // If not found then find next template. MyCustomSection_left-text.ss // If not found then find next template. MyCustomSection_homepage.ss // If not found then find next template. MyCustomSection.ss // If not found then find next template. MyParentSection_homepage_left-text.ss // If not found then find next template. MyParentSection_left-text.ss // If not found then find next template. MyParentSection_homepage.ss // If not found then find next template. MyParentSection.ss // If not found then find next template. Section.ss // Base template. // If not found then error out.
Sections has a few useful variables to help., (*13)
{$Class}
: Returns the class defined by the section object or layout it may have., (*14)
mycustomsection
{$ClassAttr}
: Returns a class attribute with the class of the section., (*15)
class="mycustomsection"
{$Color}
: Returns color defined by the section object., (*16)
{$Anchor}
: Returns a html safe string based on the title of the current section., (*17)
check-out-our-features
{$AnchorAttr}
or {$TargetAttr}
: Returns a id attribute based on the title of the current section., (*18)
id="check-out-our-features"
{$Pos}
The current integer position in the area. Will start at 1., (*19)
{$Even}
, {$Odd}
, {$First}
, {$Last}
or {$Middle}
: Booleans about the position in the area., (*20)
{$CurrentPage}
: Access the current page scope., (*21)
{$CurrentPage.Title} <% with CurrentPage %> {$Title} - {$Link} <% end_with %>
By default $Title
in sections uses HTMLTag to wrap a tag with html defined in the cms.
So your template can simplified to this., (*22)
{$Title}
Is the equivalent of, (*23)
<% if Title %> <{$TitleSemantic}> {$Title} </{$TitleSemantic}> <% end_if %>
And returns, (*24)
<h1> This sections title </h1>