【发布时间】:2018-08-23 02:11:42
【问题描述】:
有没有办法从另一个组件或另一个插件访问部分?
我有一个显示某种消息的模态组件。现在我有另一个组件在模式对话框中显示一个复杂的表单。它们驻留在 2 个插件中。
【问题讨论】:
标签: octobercms octobercms-plugins october-partial
有没有办法从另一个组件或另一个插件访问部分?
我有一个显示某种消息的模态组件。现在我有另一个组件在模式对话框中显示一个复杂的表单。它们驻留在 2 个插件中。
【问题讨论】:
标签: octobercms octobercms-plugins october-partial
是的,在插件组件内部,您可以从同一个插件中的另一个组件(您需要设置共享部分)以及来自其他插件的组件和部分访问部分。
用于访问同一插件中组件之间的共享部分,see this section of the docs.:
多个组件可以通过将部分文件放在 一个名为
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,它可以使用这个组件和来自Foo 的部分Thud:
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 部分。
虽然我们还没有完成,但我很确定必须以这种方式完成(尽管可能有一种更优雅的方式我不知道),但我们已经在我们的页面上定义了这两个组件想从以下位置调用这一切:
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>
我不完全理解您在问题的第二部分中提出的问题,但希望以上内容可以帮助您解决问题。
【讨论】: