如何从树枝过滤器渲染组件?

问题描述 投票:1回答:1

我想制作一个过滤器,用渲染的组件替换一些文本。由于组件需要控制器自我渲染,而插件类无法访问它,我不知道如何正确实现。

// ...

// Within my plugin
public function registerMarkupTags()
{
    return [
        'filters' => [
            'myfilter' => function($content){
                $render = RenderMyComponentPlease('my-component') // <- Rendering the component
                return str_replace('[SLUG]', $render, $content);  // Replace within the text
            }
        ]
    ];
}

// ...
octobercms
1个回答
0
投票

得到它了!

我没有注意到控制器是Singleton,控制器可以使用静态方法getController()进行检索。

这是一个可能的解决方案:


// ...

// Within my plugin
public function registerMarkupTags()
{
    return [
        'filters' => [
            'myfilter' => function($content){
                $controller = Controller::getController(); // Retrieve the controller
                $controller->addComponent('MyPluginPath\Components\MyComponent', 'my-component', [], false); // Add my component to the page
                $render =  $controller->renderComponent('my-component', []); // <- Rendering the component
                return str_replace('[SLUG]', $render, $content);  // Replace within the text
            }
        ]
    ];
}

// ...
© www.soinside.com 2019 - 2024. All rights reserved.