plato-creative/sections

1.0.1 2019-04-03 23:12 UTC

This package is auto-updated.

Last update: 2024-09-22 22:01:19 UTC


README

安装

Composer 是推荐安装 SilverStripe 模块的途径。

composer require plato-creative/sections

需求

  • silverstripe/cms ^4.0

维护者

文档

向页面添加板块

标准板块区域

向页面类型添加板块区域。

class MyCustomPage extends Page
{
    private static $extensions = array(
        'Sectioned'
    )
}

默认情况下,这将添加一个可通过页面模板中的 {$Area} 访问的板块区域。

多个板块区域

向页面类型添加多个板块。

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'
    )
}

这可以在页面模板中通过以下方式访问

<div>
    {$Area} <%-- standard sections --%>
</div>
<div>
    {$Area('OtherSections')}
</div>

按页面类型限制板块类型

设置允许的板块。

class MyCustomPage extends Page
{
    private static $extensions = array(
        'Sectioned'
    )

    private static $allowed_sections = array(
        'ContentSection',
        'BannerSection'
    )
}

设置排除的板块。

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');
    }
}

模板化

文件名

板块将根据主题模板目录中板块的名称查找模板。例如,MyCustomSection 将查找 MyCustomSection.ss。

此外,板块还将查找带有特定布局的模板。例如,MyCustomSection_left-text.ss。

板块还将查找特定于页面类型的模板。例如,MyCustomSection_homepage.ss。

最后,板块还将查找同时匹配特定布局和页面类型的模板。例如,MyCustomSection_homepage_left-text.ss。

文件名层次结构示例

考虑以下条件:页面类名 = HomePage,板块类名 = MyCustomSection,板块布局 = left-text,并扩展 MyParentSection,我们可以看到搜索的模板及其优先级从先到后。

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.

板块模板变量

板块提供了一些有用的变量来帮助。

{$Class}:返回由板块对象或布局定义的类。

mycustomsection

{$ClassAttr}:返回包含板块类的类属性。

class="mycustomsection"

{$Color}:返回由板块对象定义的颜色。

{$Anchor}:返回基于当前板块标题的 HTML 安全字符串。

check-out-our-features

{$AnchorAttr}{$TargetAttr}:返回基于当前板块标题的 id 属性。

id="check-out-our-features"

{$Pos}:当前区域中的整数位置。从 1 开始。

{$Even}{$Odd}{$First}{$Last}{$Middle}:关于区域位置的布尔值。

{$CurrentPage}:访问当前页面范围。

{$CurrentPage.Title}

<% with CurrentPage %>
    {$Title} - {$Link}
<% end_with %>

板块模板标题

默认情况下,板块中的 $Title 使用 HTMLTag 将标签包装在 CMS 中定义的 HTML 中。因此,您的模板可以简化为以下内容。

{$Title}

相当于

<% if Title %>
    <{$TitleSemantic}>
        {$Title}
    </{$TitleSemantic}>
<% end_if %>

并返回

<h1>
    This sections title
</h1>