如何在组件内渲染主题部分

时间:2018-04-09 01:56:28

标签: themes octobercms renderpartial

我想在几个插件和组件之间重用模态部分。根据主题提供的Bootstrap版本,模态可能具有不同的属性,因此我决定在每个主题中创建一个模式以使用站点范围。当我更改主题时,我只需在主题中添加适当编码的部分。

然后我的组件可以在需要时使用所需的变量渲染模态。

我的解决方案就像:

 public function modalSet($sections) {
        $modal_path = $this->getTheme()->getPath() . '/partials/site/modal.htm';

        return ['#modal' => $this->renderPartial($modal_path,
            [
                'title' => array_get($sections, 'title', ''),
                'subhead' => array_get($sections, 'subhead', ''),
                'body' => array_get($sections, 'body', ''),
                'foot' => array_get($sections, 'foot', ''),
            ]),
        ];
    }


    onShowInfo(){
        return $this->modalSet([
                'title' => 'Information About The Thing',
                'body' => '<p>The thing is really Big</p>',
        ]);
    }

问题是$ this-&gt; renderPartial尝试在组件中找到部分而不是主题,即使我传递了整个主题部分路径。

我收到错误:“找不到页面'/home/kurt/public_html/mysite/themes/mytheme/partials/site/modal.htm'”

那么我需要做什么才能从组件内部渲染一个主题?

1 个答案:

答案 0 :(得分:2)

它们theme partials acting differently from regular partials差别不大。他们有点Halcyon Model //关注主题

好的,这是实现它的方法。

  

只需将名称与目录 [一起使用,请确保为所需的部分文件使用.htm扩展名。和网站文件夹必须位于partials folder ]

    // use this path it will respect current active theme
    // and pick partial from active theme automatically 

    $modal_path = 'site/modal.htm'; 
    return ['#modal' => $this->renderPartial($modal_path,
        [
            'title' => array_get($sections, 'title', ''),
            'subhead' => array_get($sections, 'subhead', ''),
            'body' => array_get($sections, 'body', ''),
            'foot' => array_get($sections, 'foot', ''),
        ]),
    ];

它会欺骗并加载部分目录,并为您提供desired html

如有疑问请发表评论。