2

I have a module that's meant to add a section to the CP.

class Reports extends Module
{
    // Public Methods
    // =========================================================================

    /**
     * @inheritdoc
     */
    public function init()
    {
        parent::init();

        Event::on(
            Cp::class,
            Cp::EVENT_REGISTER_CP_NAV_ITEMS,
            function (RegisterCpNavItemsEvent $event) {
                $event->navItems[] = [
                    'url' => 'reports',
                    'label' => 'Reports',
                    //'icon' => '@mynamespace/path/to/icon.svg',
                    'subnav' => [
                        'vat' => ['label' => 'VAT', 'url' => 'reports/vat']
                    ],
                ];
            }
        );

        Event::on(
            View::class,
            View::EVENT_REGISTER_CP_TEMPLATE_ROOTS,
            function (RegisterTemplateRootsEvent $event) {
                $event->roots['reports'] = __DIR__ . '/templates';
            }
        );

    }
}

Inside of my module is a templates folder, and the templates are being reached, however the {% extends "_layouts/cp.twig" %} is erroring from my template files. This is index.twig:

{% extends "_layouts/cp.twig" %}
{% set title = "Reports" %}

{% block content %}
    <p>Reports</p>
{% endblock %}

and this is the error: enter image description here

The code is straight from Craft 4.x and 3.x Docs (same for both), so I'm not sure what's wrong.

1 Answer 1

2

There is ONE tiny difference between Craft 3.x and Craft 4.x!

{% extends "_layouts/cp.html" %} for Craft 3

{% extends "_layouts/cp.twig" %} for Craft 4

and to avoid a future breakage on update, I did find that {% extends "_layouts/cp" %} works for both.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.