在 2 个插件之间共享部件/组件



有没有办法从另一个组件或其他插件访问部分?

我有一个显示某种消息的模态组件。现在我有另一个组件在模态对话框中显示一个复杂的表单。它们驻留在 2 个插件中。

是的,在插件组件内部,您可以访问来自同一插件中另一个组件的部件(您将设置共享部件),以及来自其他插件的组件和部件。

有关访问同一插件中组件之间的共享部分,请参阅文档的此部分:

多个组件可以通过将部分文件放入 一个名为components/partials的目录。在此找到的部分 目录用作回退,当通常的组件部分 找不到。例如,位于/plugins/acme/blog/components/partials/shared.htm可以显示在 该页面由任何组件使用:

{% partial '@shared' %}

要从组件插件内的另一个插件访问组件或部件,请参阅以下FooBar插件示例:

plugins/montanabanana/foo/Plugin.php:

<?php namespace MontanaBananaFoo;
use SystemClassesPluginBase;
class Plugin extends PluginBase
{
public function registerComponents()
{
return [
'MontanaBananaFooComponentsThud' => 'thud'
];
}
public function registerSettings()
{
}
}

plugins/montanabanana/foo/components/Thud.php

<?php
namespace MontanaBananaFooComponents;
class Thud extends CmsClassesComponentBase
{
public function componentDetails()
{
return [
'name' => 'Thud Component',
'description' => ''
];
}
}

plugins/montanabanana/foo/components/thud/default.htm

<pre>Thud component, default.htm</pre>

plugins/montanabanana/foo/components/thud/partial.htm

<pre>This is the thud partial</pre>

好的,我们已经设置了注册 Thud 组件的 Foo 插件。该组件中有一些基本的默认标记,组件文件夹中有一个部分标记。现在,让我们设置另一个插件,该插件具有组件Grunt可以使用该组件和来自Foo的部分Thud

plugins/montanabanana/bar/Plugin.php

<?php namespace MontanaBananaBar;
use SystemClassesPluginBase;
class Plugin extends PluginBase
{
// We should require the plugin we are pulling from
public $require = ['MontanaBanana.Foo'];
public function registerComponents()
{
return [
'MontanaBananaBarComponentsGrunt' => 'grunt'
];
}
public function registerSettings()
{
}
}

plugins/montanabanana/bar/components/grunt/default.htm

<pre>Grunt component, default.htm</pre>
{% component 'thud' %}
{% partial 'thud::partial' %}

请注意,在上面的 Bar 中 Grunt 组件的默认标记文件中,我们已经调用了 Thud 组件和 Thud 组件的partial.htm部分。

不过我们还没有完全完成,我很确定它必须以这种方式完成(尽管我不知道可能有一种更优雅的方式),但是我们已经在页面上定义了两个组件,我们想要从中调用它:

themes/your-theme/pages/example.htm

title = "Example"
url = "/example"
[grunt]
[thud]
==
{% component 'grunt' %}

其输出为:

<pre>Grunt component, default.htm</pre>
<pre>Thud component, default.htm</pre>
<pre>This is the thud partial</pre>

我不完全理解你在问题的第二部分问什么,但希望以上内容能帮助你解决它。