有没有办法从另一个组件或另一个插件访问部分?
我有一个显示某种消息的模式组件。现在我有另一个组件在模式对话框中显示一个复杂的表单。它们位于 2 个插件中。
有没有办法从另一个组件或另一个插件访问部分?
我有一个显示某种消息的模式组件。现在我有另一个组件在模式对话框中显示一个复杂的表单。它们位于 2 个插件中。
是的,在插件组件内部,您可以从同一插件中的另一个组件(您将设置共享部分)以及来自其他插件的组件和部分访问部分。
要访问同一插件中组件之间的共享部分,请参阅文档的此部分。:
多个组件可以通过将部分文件放在名为
components/partials
. 当找不到通常的组件部分时,在此目录中找到的部分用作后备。例如,位于的共享部分/plugins/acme/blog/components/partials/shared.htm
可以由任何组件显示在页面上,使用:{% partial '@shared' %}
要从组件插件中的另一个插件访问组件或部分,请参阅以下插件Foo
示例Bar
:
plugins/montanabanana/foo/Plugin.php:
<?php namespace MontanaBanana\Foo;
use System\Classes\PluginBase;
class Plugin extends PluginBase
{
public function registerComponents()
{
return [
'MontanaBanana\Foo\Components\Thud' => 'thud'
];
}
public function registerSettings()
{
}
}
plugins/montanabanana/foo/components/Thud.php
<?php
namespace MontanaBanana\Foo\Components;
class Thud extends \Cms\Classes\ComponentBase
{
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
可以使用这个组件和部分Thud
来自的组件Foo
:
plugins/montanabanana/bar/Plugin.php
<?php namespace MontanaBanana\Bar;
use System\Classes\PluginBase;
class Plugin extends PluginBase
{
// We should require the plugin we are pulling from
public $require = ['MontanaBanana.Foo'];
public function registerComponents()
{
return [
'MontanaBanana\Bar\Components\Grunt' => '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
partial。
虽然我们还没有完成,我很确定它必须以这种方式完成(尽管可能有一种更优雅的方式我不知道),但我们已经在我们想要调用的页面上定义了这两个组件这一切都来自:
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>
我不完全理解您在问题的第二部分中要问的内容,但希望以上内容可以帮助您解决问题。