csoellinger/silverstripe-limit-characters-with-html

一个简单的 Silverstripe 扩展,用于限制 DBHtmlText 和 DBHtmlVarChar 的字符数,而不会丢失 HTML 标签。

安装: 58

依赖: 0

建议者: 0

安全: 0

星标: 1

关注者: 1

分支: 0

开放问题: 1

类型:silverstripe-vendormodule

1.0.0 2023-03-28 08:33 UTC

This package is auto-updated.

Last update: 2024-09-05 14:25:52 UTC


README

此模块向 DBHTMLVarchar 和 DBHTMLText 添加扩展,允许限制 HTML 文本而不丢失 HTML 标签。例如,如果您想使用 WYSIWYG 编写摘要文本。

  1. 需求
  2. 安装
  3. 许可证
  4. 配置
    1. private static bool html_min
    2. private static array html_min_options
  5. 文档
    1. public function LimitCharactersWithHtml( $limit = 20, $add = false, $exact = true ): string
    2. public function LimitCharactersWithHtmlToClosestWord( int $limit = 20, $add = false ): string
    3. public function LongerThan( int $limit, $excludeHtml = true ): bool
  6. 使用示例
  7. 维护者
  8. 错误跟踪器
  9. 开发和贡献

需求

  • Silverstripe ^4 | ^5

安装

只需使用 composer 安装扩展

composer require csoellinger/silverstripe-limit-characters-with-html

通过包含的配置文件,该扩展将自动添加到 SilverStripe\ORM\FieldType\DBHTMLVarcharSilverStripe\ORM\FieldType\DBHTMLText。只需运行

sake dev/build "flush=all"

许可证

查看 许可证

配置

private static bool html_min

启用/禁用 HTML 压缩。

private static array html_min_options

将 HTML 压缩选项设置为关联数组

  • collapse_whitespace => true|false (默认: true)
  • disable_comments => true|false (默认: false)

文档

安装模块后,您将拥有三个新的 db html 字段方法。

public function LimitCharactersWithHtml( $limit = 20, $add = false, $exact = true ): string

/**
 * Limit this field's content by a number of characters. It can consider
 * html and limit exact or at word ending.
 *
 * @param int           $limit        Number of characters to limit by.
 * @param string|false  $add          Ellipsis to add to the end of truncated string.
 * @param bool          $exact        Truncate exactly or at word ending.
 *
 * @return string HTML text with limited characters.
 */

public function LimitCharactersWithHtmlToClosestWord( int $limit = 20, $add = false ): string

/**
 * Limit this field's content by a number of characters and truncate the field
 * to the closest complete word.
 *
 * @param int          $limit        Number of characters to limit by.
 * @param string|false $add          Ellipsis to add to the end of truncated string.
 *
 * @return string HTML text value with limited characters truncated to the closest word.
 */

public function LongerThan( int $limit, $excludeHtml = true ): bool

/**
 * Check if a string is longer than a number of characters. It excludes html
 * by default.
 *
 * @param mixed $excludeHtml Default is true
 *
 * @return bool
 */

使用示例

<?php

$htmlText = '<b>Hello World</b>';

// example 1
DBHTMLText::create('Test')
  ->setValue($htmlText)
  ->LimitCharactersWithHtml(9)
  ->Value() // Output: <b>Hello Wo…</b>

// example 2
DBHTMLText::create('Test')
  ->setValue($htmlText)
  ->LimitCharactersWithHtmlToClosestWord(9)
  ->Value() // Output: <b>Hello…</b>
<p>{$TestField.LimitCharactersWithHtml(9)}</p>
<p>{$TestField.LimitCharactersWithHtmlToClosestWord(9)}</p>

维护者

错误跟踪器

错误在此存储库的问题部分跟踪。在提交问题之前,请阅读现有问题以确保您的独特性。

如果问题看起来像是一个新的错误

  • 创建一个新的问题
  • 描述重现问题的步骤和预期结果。单元测试、屏幕截图和屏幕录像可能在这里有所帮助。
  • 尽可能详细地描述您的环境:Silverstripe 版本、浏览器、PHP 版本、操作系统、安装的任何 Silverstripe 模块。

请直接向模块维护者报告安全问题。请不要在错误跟踪器中提交安全问题。

开发和贡献

如果您想为此模块做出贡献,请确保您提出一个拉取请求,并与模块维护者进行讨论。