定制 visuellverstehen/statamic-content-renderer 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

visuellverstehen/statamic-content-renderer

最新稳定版本:v1.0.5

Composer 安装命令:

composer require visuellverstehen/statamic-content-renderer

包简介

A Statamic search transformer for bard and replicator fields.

README 文档

README

A Statamic addon that renders content from bard and replicator fields to make their content searchable with search transformers.

How to Install

Run the following command from your project root:

composer require visuellverstehen/statamic-content-renderer

How to Use

The content renderer is available through the Renderer() class. To render the content of a replicator or bard field, the class requires a view that provides the info of how to display all the configured sets, e. g.:

{{# resources/views/sets.antlers.php #}}

{{ my_replicator_field }}
    {{ partial src="sets/{type}" }}
{{ /my_replicator_field }}

This view needs to be passed on to the renderer class.

use VV\ContentRenderer\Renderer;

// ...

$renderer = new Renderer();
$renderer->setContent($entry, 'my_replicator_field');
$renderer->setView('sets');

$content = $renderer->render();

The renderer uses the view to render all sets (and all written content of bard fields), sanitizes the content (strips all HTML tags etc.) and returns a string containing every written word from within the field.

This can be used within a search transformer to make the content of bard and replicator fields available for full-text search:

namespace App\SearchTransformers;

use VV\ContentRenderer\Renderer;
 
class MyReplicatorFieldTransformer
{
    public function handle($value, $field, $searchable)
    {
        $renderer = new Renderer();
        $renderer->setContent($searchable, 'my_replicator_field');
        $renderer->setView('sets');
        
        return $renderer->render();
    }
}

Preserving links targets

When using the Content Renderer within a search transformer, it might be useful to preserve link targets in the rendered output. This makes it possible to find entries based on urls linked in the content. You can instruct the renderer to add link targets in parenthesis behind the link text:

$renderer = (new Renderer())->$withLinkTargets();

// read more <a href="https://visuellverstehen.de">about the author</a> of this package
// becomes: read more about the author (https://visuellverstehen.de) of this package

Preserving HTML tags

If you want to keep the HTML tags and/or modify the content in your own way, you can instruct the renderer not to strip them:

$renderer = (new Renderer())->withHtmlTags();

Note: If you choose to preserve HTML tags, the withLinkTargets option (see above) will be ignored.

Custom processor

If you need to modify the content before it is passed on to the render process, you can optionally add a custom processor function:

$renderer = new Renderer();
$renderer->setContent($searchable, 'bard_content');
// …

$renderer->process(function ($content) {
    // modify content
    
    return $content;
});

This allows you to e. g. remove certain sets or modify the content in any other way.

More about us

License

The MIT license (MIT). Please take a look at the license file for more information.

统计信息

  • 总下载量: 5.57k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 6
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-04-24