Plugin unable to load template

Hello shopware community,

 

When the plugin i made tries to load a custom template on the controller i get the following error message:

Unable to load template snippet ‚frontend/index/index.tpl|frontend/plugins/seo/index.tpl‘ in engine/Library/Smarty/sysplugins/smarty_internal_templatebase.php on line 127

I followed the documentation pretty much to the letter but it somehow doesn’t work at all. And since i’m not using a custom theme for this project i can’t just set injectBeforePlugins = true in a Theme.php file because i’m not using one (as far as i know anyway) and the Bare and Responsive Theme.php file have injectBeforePlugins = true by default.

 

For a little more info on how my actual code looks like, my bootstrap file looks like this:

/**
     * @inheritdoc
     */
    public static function getSubscribedEvents()
    {
        return [
            'Enlight_Controller_Dispatcher_ControllerPath_Frontend_Event' => 'onGetFrontendController'
        ];
    }

     /**
     * @return string
     */
    public function onGetFrontendController()
    {
        $this->container->get('Template')->addTemplateDir($this->getPath() . '/Resources/views/');

        return $this->getPath() . 'Controllers/Frontend/MyController.php';
    }

my template file looks like this:

{extends file="parent:frontend/index/index.tpl"}

{block name="frontend_index_content_left"}{/block}

{block name="frontend_index_content"}
    
        {foreach $result as $item}
            {* some code *}
        {/foreach}
    
{/block}

and my plugin directory structure looks like this:

I’m at a complete loss on how to fix this problem so any help is appreciated :slight_smile:

Hello @dNovoNiels‍,

first of all: Can you provide your MyController.php aswell?

So, basically, you’re overwriting the main template-file in Shopware at the moment:
frontend/index/index.tpl

Nearly your whole Shopware installation should be mostly empty now, since you even replace the main-block  frontend_index_content  in your own template.
Did you just want to provide a custom template for your custom controller? E.g. when you call it like that: myShop.com/MyController

Shopware expects a custom template for each frontend controller action by default, placed in the folder equal to the controller-path.
In that case, you’d have to create a file in the following folder:
MyPlugin/Resources/views/frontend/my_controller/ index.tpl

Just remember: The  index.tpl must have the same name as the action you’ve called in your controller.
E.g. indexAction is called => You’ll need an index.tpl
fooAction is called => You’ll need a foo.tpl

Your controller is placed at Frontend/ MyController , therefore you need to place your template in the folder frontend/ my_controller , starting from your custom views-directory.

Did this help you a little further or did I just confuse you a little more?  Foot-in-Mouth

Patrick  Shopware

@Patrick Stahl schrieb:

Did you just want to provide a custom template for your custom controller? E.g. when you call it like that: myShop.com/MyController

 yes, that is exactly what i’m trying to achieve. I want to use the same template the homepage has on my controller, and  just overwrite the main content block to show what i want it to show. so when i call myshop.com/MyController i will see the same layout as the homepage, but diffirent content.

 

my MyController.php looks like this:

View()->assign('message', $result);
    }
}

might aswell add the complete template code while i’m at it :stuck_out_tongue:

{extends file="parent:frontend/index/index.tpl"}

{block name="frontend_index_content_left"}{/block}

{block name="frontend_index_content"}
    
        {foreach $result as $item}
            {$item}
        {/foreach}
    
{/block}

it’s all pretty simple code so far and as far as i can tell i have it set up in the way that you described it but it still isn’t working :confused:

Hey @dNovoNiels‍,

in that case you’ll have to rename the index-folder in your plugin.
Currently your template is placed in Resources/views/frontend/ index /index.tpl.
Simply rename it to:
Resources/views/frontend/ my_controller /index.tpl

Then it should work just fine already! :slight_smile:

@Patrick Stahl schrieb:

in that case you’ll have to rename the index-folder in your plugin.
Currently your template is placed in Resources/views/frontend/ index /index.tpl.
Simply rename it to:
Resources/views/frontend/ my_controller /index.tpl

Then it should work just fine already! :slight_smile:

 Hello Patrick,

 

Even after renaming the folder, i still get the same error. So unfortunately this solution doesn’t work for me for whatever reason :confused:

Hey @dNovoNiels‍,

that should definitely work.

Can you zip & upload your plugin somewhere and send me the link (here or in a private message) ?
I’d have a quick look into it then, pretty sure I can resolve that issue very fast. :slight_smile:

Patrick  Shopware

I already changed up some of the names of the files and folders but the code is practicly the same as posted above

Hey @dNovoNiels‍,

I got your example to work. :slight_smile:

What did I have to do?
1st: Your registered event in your DnovoEvents.php was wrong. There was an ‘s’ missing at the end of the string, since your controller is written with a ‘s’ at the end.
2nd: The path to your controller was missing a slash ( / ) at the beginning in the method  onGetFrontendController. The path to your custom views directory was correct though and already included a slash.
3rd: Make sure to only use lower-case folder names in your views-folder. Had to change both “Frontend” and “events” to be lowercase.

After those three adjustments, the plugin worked for me and I could successfully call your custom controller with the proper template.

Do those information help you?

Patrick  Shopware

@Patrick Stahl schrieb:

I got your example to work. :slight_smile:

What did I have to do?
1st: Your registered event in your DnovoEvents.php was wrong. There was an ‚s‘ missing at the end of the string, since your controller is written with a ‚s‘ at the end.
2nd: The path to your controller was missing a slash ( / ) at the beginning in the method  onGetFrontendController. The path to your custom views directory was correct though and already included a slash.
3rd: Make sure to only use lower-case folder names in your views-folder. Had to change both „Frontend“ and „events“ to be lowercase.

After those three adjustments, the plugin worked for me and I could successfully call your custom controller with the proper template.

Do those information help you?

Patrick  Shopware

Yes, this worked like a charm :slight_smile: thank you very much