2.x: dynamically set tpl used by component?

Hi All,

I’m trying to render a Component and dynamically set the template to use at runtime.

I know I can set this in the page template, using:
{% component MyComponent::styleA %}

But what I want to do is use styleA only if logic in the Component PHP file meets certain conditions, otherwise it would just use default.htm.

Is there any way to set the Component template within the Component PHP file?

Thanks!

Hi @Jinjo

Yes, if you return a view from onRender() it will override the default view.

/**
 * onRender is executed when this component is rendered on a page or layout.
 */
public function onRender()
{
    return $this->renderPartial('...');
}