Reputation: 251
By default, OctoberCMS plugin components render the partial default.htm
. Is it possible to override which partial will be rendered? For example ...
Plugin file structure
├── components
│ ├── example
│ │ ├── default.htm
│ │ ├── other_partial.htm
│ ├── Example.php
├── ...
├── Plugin.php
Example.php
class Example extends ComponentBase {
public function onRun() {
// change the rendered partial, such that other_partial.htm
// will be rendered instead of default.htm
$this->setRenderedPartial('other_partial')
}
}
I know that it is possible to render the other_partial
from inside the default.htm
but in my case I seek to leave the default.htm
untouched and render the other partial as default instead.
Upvotes: 1
Views: 2597
Reputation: 53
I was trying to solve this problem for my website, and I think I have found the answer. On your component PHP file, you can try this function:
public function onRender(){
$partialType= $this->property('partialType');
switch ($partialType) {
case 'one':
$partial = '@partial-1.htm';
break;
case 'two':
$partial = '@partial-2.htm';
break;
case 'three':
$partial = '@partial-3.htm';
break;
case 'four':
$partial = '@partial-4.htm';
break;
default:
$partial = '@default.htm';
break;
}
return $this->renderPartial($partial);
}
Upvotes: 2
Reputation: 13172
You should tell the component to render the Partial on onRender()
that is run after the layout and page are rendered:
public function onRender()
{
return $this->renderPartial('@other_partial');
}
Use the @ to force the component to look for a component partial instead of a theme one!
Upvotes: 1
Reputation: 71
I'm still looking for a solution that does not require twig. I added a setting in my component that list all files under the template folder.
Now I can choose the desired template file, I want that, when I simply call the component like that :
{% component 'mycomponent' %}
it render the component using the correct partial defined in the component settings.
There is probably some code to pu in the onRun() function of my component, but don't find the correct one.
Using $content = $this->renderPartial('grid.htm'); still render the default.htm partial...
Upvotes: 0
Reputation: 231
The logic on you page template could look like the code below to decide which partial should be displayed.
Something like (you need to check the syntax, because I haven't used it recently:
{% if other %} #if variable 'other' exists in the page
{% partial "other" %}
{% else %}
{% partial "default" %}
{% endif %}
Of course, you need to have some logic in your page that either decides the variable 'other' should exist or not. Or you could make the logic check for a specific value of the control variable
Upvotes: 0
Reputation: 128
You can use the renderPartial
method
public function onRun()
{
$content = $this->renderPartial('default.htm');
}
doc: http://octobercms.com/docs/plugin/components#render-partial-method
Upvotes: 2
Reputation: 231
The way I would do this in October is to handle this on the page template with some logic in Twig. This would probably be the most logic place to put it in my opinion.
Upvotes: 2