touchcast/modulator

该软件包最新版本(0.14)没有可用的许可证信息。

SilverStripe 模块,用于从较小组件动态创建页面

安装次数: 4,902

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 3

分支: 3

开放问题: 0

类型:silverstripe-module

0.14 2017-04-10 22:25 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:26:15 UTC


README

Travis Packagist Packagist

Modulator 是一个可扩展的系统,用于创建子页面组件。它通过将 DataObjects附加到页面而不是传统的内容体来实现这一点。

特性

  • 每个模块级别的草稿和发布控制
  • 模块拖放重新排序
  • 创建您自己的模块的可扩展系统
  • CMS 预览
  • 按页面类型筛选可用模块

安装

Modulator 可以通过 Composer 安装;

composer require touchcast/modulator

安装后,运行 /dev/build 以生成数据库表。

使用方法

在您的网站树中创建一个 ModularPage 页面。将新模块添加到页面并根据需要填充它们。

创建模块

每个模块由一个 PHP 类和一个模板文件组成。

首先扩展 PageModule

class HeroModule extends PageModule {

  // Give the module a name for use within the CMS
  public static $label = "Hero module";

  // Give it a description
  public static $description = "A large title section at the top of the page";

  // Optionally group your modules by type, for easy reference in the CMS
  public static $category = "Headers";

  // Add any fields required for the module
  private static $db = array(
    "Heading" => "Varchar(128)"
  );

  // Provide custom summary content for the gridfield
  public function getSummaryContent() {
    return $this->Heading;
  }

  // Provide text content from the module to be included in the pages's search index
  public function getSearchBody() {
    return $this->Heading;
  }
}

然后创建一个模板文件。它应该与您的模块类同名。

<header>
  <h1>$Heading</h1>
</header>

如果您想扩展 ModularPage 模板,可以使用 $ActiveModules 循环手动渲染模块。

<% loop $ActiveModules %>
<section class="$ClassName.Lowercase <% if $Odd %>odd<% else %>even<% end_if %> order-$Order">
  $Content
</section>
<% end_loop %>

筛选可用模块

对于您从 ModularPage 扩展的每个页面类,您可以指定一个允许附加到该页面的模块列表。例如;

class CustomPage extends ModularPage {

  public static $allowed_modules = array(
    'HeroModule',
    'TextModule'
  );
}

自定义基础模块

如果您想为页面模块创建一个自定义基础类以包含任何公共字段,您可以在配置文件中指定此内容;

ModularPage:
  base_class: CustomModuleBase

您可以更进一步,为每种页面类型指定不同的基础类(作为使用 $allowed_modules 的替代方法);

ModularPage:
  base_class: CustomModuleBase

BlogPage:
  base_class: BlogModuleBase

搜索体填充

默认行为是将所有页面的模块内容填充到 SiteTree 的 Content 字段中,以便它可以由 Silverstripe 内置的搜索功能进行索引。如果您不希望这种行为,您可以在每个类的基础上禁用它;

ModularPage:
  write_content: true

BlogPage:
  write_content: false

测试

从模块文件夹中运行 phpunit,或从浏览器中运行 /dev/tests