1

默认情况下,OctoberCMS 插件组件会渲染部分default.htm. 是否可以覆盖将呈现的部分?例如 ...

插件文件结构

├── components
│   ├── example
│   │   ├── default.htm
│   │   ├── other_partial.htm
│   ├── Example.php
├── ...
├── Plugin.php

例子.php

class Example extends ComponentBase  {

   public function onRun() {
       // change the rendered partial, such that other_partial.htm
       // will be rendered instead of default.htm
       $this->setRenderedPartial('other_partial')
   }

}

我知道可以other_partial从内部渲染,default.htm但在我的情况下,我试图default.htm保持不变并将其他部分渲染为默认值。

4

6 回答 6

2

我在 10 月份这样做的方法是在 Twig 中使用一些逻辑在页面模板上处理这个问题。在我看来,这可能是最合乎逻辑的地方。

于 2017-05-18T18:15:23.320 回答
2

您可以使用该renderPartial方法

public function onRun()
{
    $content = $this->renderPartial('default.htm');
}

文档: http: //octobercms.com/docs/plugin/components#render-partial-method

于 2017-05-19T14:30:43.857 回答
2

我试图为我的网站解决这个问题,我想我已经找到了答案。在您的组件 PHP 文件中,您可以尝试此功能:

public function onRender(){
    $partialType= $this->property('partialType');
    switch ($partialType) {
        case 'one':
            $partial = '@partial-1.htm';
            break;
        
        case 'two':
            $partial = '@partial-2.htm';
            break;

        case 'three':
            $partial = '@partial-3.htm';
            break;
              
        case 'four':
            $partial = '@partial-4.htm';
            break;
              
        default:
            $partial = '@default.htm';
            break;
    }

   return $this->renderPartial($partial);
}
于 2020-07-01T13:20:58.517 回答
1

您应该告诉组件渲染onRender()布局和页面后运行的 Partial:

public function onRender()
{
    return $this->renderPartial('@other_partial');
}

使用 @ 强制组件查找组件部分而不是主题!

于 2018-08-31T14:18:49.247 回答
0

页面模板上的逻辑可能类似于下面的代码,以决定应该显示哪个部分。

类似的东西(您需要检查语法,因为我最近没有使用它:

{% if other %} #if variable 'other' exists in the page
    {% partial "other" %}
{% else %}
    {% partial "default" %}
{% endif %}

当然,您需要在页面中有一些逻辑来决定变量“其他”是否应该存在。或者您可以对控制变量的特定值进行逻辑检查

于 2017-05-20T15:40:54.903 回答
0

我仍在寻找不需要树枝的解决方案。我在组件中添加了一个设置,列出了模板文件夹下的所有文件。

现在我可以选择所需的模板文件,我想要的,当我像这样简单地调用组件时:

{% component 'mycomponent' %}

它使用组件设置中定义的正确部分渲染组件。

我的组件的 onRun() 函数中可能有一些代码要输入,但找不到正确的代码。

使用 $content = $this->renderPartial('grid.htm'); 仍然呈现 default.htm 部分...

于 2017-10-03T08:34:55.153 回答