This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

chris10207
chris10207

How can we use a component from another component Lets say we have the following code for the component A

componenta/default.htm

inside the markup i'd like to use

{% component "componentB" %}

but it generates an exception

LookAgency
LookAgency

You can render components with

$controllerObject->renderComponent($componentName, $componentProperties)

view source here but this doesn't bind the second component to the controller or run its init methods as far as I'm aware so you may experience issues. For a practical example in your situation, you would have something like {{ __SELF__.renderComponentB() }} in your componenta/default.htm and then you would have

public function renderComponentB() {
    $componentbProps = [];
    return $this->controller->renderComponent('componentb', $componentbProps);
}
chris10207
chris10207

Thanks for the idea it behaves the same way as if i insert directly {% component 'componentB' %} directly the html markup, meaning an exception is raised

actually, it is the rendering of the next partial in the markup that raises the exception, i have the following in componentA/default.htm

{% component 'componentB' %}

<div id="data-details">
{% partial '@view/view-profile' %}
</div>

and the exception

[2016-08-27 17:35:03] dev.ERROR: exception 'Cms\Classes\CmsException' with message 'The partial '::view/view-profile' is not found.' in /Users//Sites/xyz/site/modules/cms/Classes/Controller.php:833

Last updated

ideepblue
ideepblue

chris10207 said:

Thanks for the idea it behaves the same way as if i insert directly {% component 'componentB' %} directly the html markup, meaning an exception is raised

actually, it is the rendering of the next partial in the markup that raises the exception, i have the following in componentA/default.htm

{% component 'componentB' %}

<div id="data-details">
{% partial '@view/view-profile' %}
</div>

and the exception

[2016-08-27 17:35:03] dev.ERROR: exception 'Cms\Classes\CmsException' with message 'The partial '::view/view-profile' is not found.' in /Users//Sites/xyz/site/modules/cms/Classes/Controller.php:833

Here's a solution that I successful make this works.

In your component A class file (e.g. componenta.php), put the following code into the method onInit() that would solve the problem:

public function onInit()
{
    $this->controller->addComponent('\Your\Namespace\Components\ComponentB', 'componentB', []);
}

You may need to see these two links as reference:

  1. https://octobercms.com/forum/post/using-form-builder-component-with-another-plugin
  2. https://octobercms.com/docs/api/cms/classes/controller#addcomponent

1-4 of 4

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.