在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()
呈现一个包含组件的局部?
嗨,我尝试了你的方法,但似乎它为我工作。让我分享我是如何做的事情。
我的页面
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个组件
newCompo
基本上,我在它的'default.htm'中添加了以下内容。{% component 'newOne' %}
所以我在里面渲染另一个组件。newOne
<- 这是我们要在局部渲染的组件。这是我的ajax处理程序
public function onTest() {
return ['#ajaxUpdate' => $this->renderPartial('@partial.htm')];
}
newCompo
组件的->。default.htm
{% component 'newOne' %}
newOne
组件的->default.htm
<h1>Newone</h1>
部分文件在
components/newcompo
->partial.htm
<h4>partial</h4>
{% component 'newOne' %}
正常
在ajax调用后