简体   繁体   English

如何更改在 OctoberCMS 组件中呈现的部分?

[英]How to change which partial is rendered in OctoberCMS component?

By default, OctoberCMS plugin components render the partial default.htm .默认情况下,OctoberCMS 插件组件呈现部分default.htm Is it possible to override which partial will be rendered?是否可以覆盖将呈现的部分? For example ...例如 ...

Plugin file structure插件文件结构

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

Example.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')
   }

}

I know that it is possible to render the other_partial from inside the default.htm but in my case I seek to leave the default.htm untouched and render the other partial as default instead.我知道可以从default.htm内部渲染other_partial ,但在我的情况下,我试图保持default.htm不变,而是将其他部分渲染为默认值。

The way I would do this in October is to handle this on the page template with some logic in Twig.我在 10 月份这样做的方法是在页面模板上使用 Twig 中的一些逻辑来处理这个问题。 This would probably be the most logic place to put it in my opinion.在我看来,这可能是最合乎逻辑的地方。

You can use the renderPartial method您可以使用renderPartial方法

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

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

I was trying to solve this problem for my website, and I think I have found the answer.我试图为我的网站解决这个问题,我想我已经找到了答案。 On your component PHP file, you can try this function:在你的组件 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);
}

You should tell the component to render the Partial on onRender() that is run after the layout and page are rendered:您应该告诉组件在呈现布局和页面后运行的onRender()上呈现 Partial:

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

Use the @ to force the component to look for a component partial instead of a theme one!使用 @ 强制组件查找组件部分而不是主题!

The logic on you page template could look like the code below to decide which partial should be displayed.页面模板上的逻辑可能类似于下面的代码,以决定应显示哪个部分。

Something like (you need to check the syntax, because I haven't used it recently:像(你需要检查语法,因为我最近没有使用它:

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

Of course, you need to have some logic in your page that either decides the variable 'other' should exist or not.当然,您的页面中需要有一些逻辑来决定变量“other”是否应该存在。 Or you could make the logic check for a specific value of the control variable或者您可以对控制变量的特定值进行逻辑检查

I'm still looking for a solution that does not require twig.我仍在寻找不需要树枝的解决方案。 I added a setting in my component that list all files under the template folder.我在组件中添加了一个设置,列出模板文件夹下的所有文件。

Now I can choose the desired template file, I want that, when I simply call the component like that :现在我可以选择所需的模板文件,我想要这样,当我简单地调用组件时:

{% component 'mycomponent' %}

it render the component using the correct partial defined in the component settings.它使用组件设置中定义的正确部分渲染组件。

There is probably some code to pu in the onRun() function of my component, but don't find the correct one.我的组件的 onRun() 函数中可能有一些代码需要 pu,但没有找到正确的代码。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM