在OctoberCMS中,如何在局部中呈现一个组件?

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

在OctoberCMS中,我正在创建一个组件。在这个组件的 default.htm 部分文件(/acme/myplugin/components/mycomponent/default.htm)我可以通过这样做包含任何其他注册的组件。

{% component 'someComponentName' %}

然而,如果我有以下代码来处理一个AJAX请求。

public function onAjaxRequest()
{
    return [
        '#ajaxUpdate' => $this->renderPartial('@dates-partial'),
    ];
}

而在我的 dates-partial.htm 文件中的内容,我把与我的 default.htm 文件,该 someComponentName 组件没有被渲染。我还试着在文件的顶部加上以下内容。

[someComponentName]
==

但这是直接作为文本输出到页面。

我怎样才能让 renderPartial() 呈现一个包含组件的局部?

php components octobercms partials
1个回答
0
投票

嗨,我尝试了你的方法,但似乎它为我工作。让我分享我是如何做的事情。

我的页面

title = "compo-in-partial-in-ajax"
url = "/compo-partial-ajax"
layout = "default"
is_hidden = 0

[newCompo]

[newOne]
==
<!-- AJAX enabled form -->
<form data-request="onTest">
    <!-- Action button -->
    <button type="submit">Call Ajax</button>
</form>

<div id="ajaxUpdate">
Default content.
</div>
{% component 'newCompo' %}

我增加了2个组件

  1. newCompo 基本上,我在它的'default.htm'中添加了以下内容。{% component 'newOne' %} 所以我在里面渲染另一个组件。
  2. newOne <- 这是我们要在局部渲染的组件。

这是我的ajax处理程序

public function onTest() {
    return ['#ajaxUpdate' => $this->renderPartial('@partial.htm')];
}

newCompo 组件的-&gt。default.htm

{% component 'newOne' %}

newOne 组件的-> default.htm

<h1>Newone</h1>

部分文件在 components/newcompo -> partial.htm

<h4>partial</h4>
{% component 'newOne' %}

产量


正常

enter image description here

在ajax调用后

enter image description here

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