How to use partial tag inside a plugin in OctoberCMS?

Inside my plugin in /plugins/acme/blog/views/mytemplate.htm

I have this code

{% partial 'test' vars=values}

this returns Unknown "partial" tag

and it’s called by an internal code with

return View::make('acme.blog::mytemplate', $values);

since it’s not called by the CMS it’s missing some functionality I guess. Is there a way to include a partial inside a view and still pass data to the partial?
I also tried the include function from twig include - Documentation - Twig - The flexible, fast, and secure PHP template engine but I’m not sure what location path to use since I’m getting “file not found” for every combination

If I’m not mistaken, the back end partials don’t accept any twig values, but they have PHP functionality so you can write PHP functions inside of the view file.

1 Like

Partials depend heavily on the CMS controller lifecycle and aren’t supported by Laravel’s view engine because of this. Using {% include %} is the closest you’ll get.

You can render partials programatically by creating an instance of the CMS Controller and calling renderPartial()

$controller = Controller::getController() ?: new Controller;
$controller->renderPartial('...');
1 Like

Hi,
how do I put this code inside the view file (.htm)? The “PHP functionality” @artistro08 was referring to.

Try adding this to your plugin registration file

public function registerMarkupTags()
{
    return [
        'functions' => [
            'include_partial' => function($name, $params = []) {
                $controller = \Cms\Classes\Controller::getController() ?: new \Cms\Classes\Controller;
                return $controller->renderPartial($name, $params, false);
            },
        ]
    ];
}

This should give you access via Twig, like so:

{{ include_partial('somepartial', { foo: 'bar' }) }}
1 Like

Hi,
now I’m facing the path problem. What is the root of renderPartail when it looks for partials? I tried every single combination and I’m getting an empty output. Is there a way to debug this?

renderPartial is called from here
/plugins/acme/blog/views/test1/main.htm

and the partial is here
/plugins/acme/blog/views/test1/partials/test_partial.htm

{{ include_partial(‘test_partial’, { foo: ‘bar’ }) }}

and

{{ include_partial(‘partials/test_partial’, { foo: ‘bar’ }) }}

return an empty result

Could anyone please help me? At least a way to debug it?

It looks for partials inside the active theme. Consider using the {% include %} tag

{% include 'acme.blog::test1.partials.test_partial' %}
1 Like

Thank you that syntax worked for me.