I have a custom theme in magento 2 website.

In home page i called a banner slider(Banner_slider is a custom module)

I called the slider like this in home_content.phtml(design/frontend/company/projectname/Magento_theme/templates/home-content.phtml) like this

<?php include ($block->getTemplateFile('Company_Bannermanager::bannermanager.phtml')) ?>

location of bannermanager.phtml is - (app\code\Company\Bannermanager\view\frontend\templates)

In this file i called the block method(getHomepageBanners()) like this

<?php $home_banners =  $block->getHomepageBanners(); ?>

Block file location is -( app\code\Company\Bannermanager\Block\Index )

I am getting this error

Error filtering template: Warning: Invalid argument supplied for foreach() in D:\xampp\htdocs\mollys_spirits\app\code\Company\Bannermanager\view\frontend\templates\bannermanager.phtml on line 17

I know i can call this block like below

$blockObj= $block->getLayout()->createBlock('Company\Bannermanager\Block\Index\Index');
$home_banners =  $blockObj->getHomepageBanners(); ?>

What part i am missing in first method.


My layout file is homebanner_index_index.xml-(app\code\Eight25media\Bannermanager\view\frontend\layout)

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <referenceContainer name="content">
        <block class="Company\Bannermanager\Block\Index\Index" name="homebanner"  template="Company_Bannermanager::bannermanager.phtml"/>

  • Where do you use homebanner_index_index.xml ? Is there a custom controller? Nov 21, 2017 at 12:48
  • i am not referring anywhere in my index controller.theme page is in the theme folder
    – Mujahidh
    Nov 21, 2017 at 12:52
  • See my updated answer. Nov 22, 2017 at 10:40

1 Answer 1


Your way doesn't follow the best practice. Try to add it via XML layout and call it by using $block->getChildHtml('your.block.alias');.

One more thing about your error. This is because the block class -Company\Bannermanager\Block\Index\Index didn't initialize. So, you couldn't get its function.


In your case, you can simply add your xml to home page via Admin. Open your Cms home page. Add your xml layout via Layout Update XML section.

<referenceContainer name="content">
        <block class="Company\Bannermanager\Block\Index\Index" name="homebanner"  template="Company_Bannermanager::bannermanager.phtml"/>

Or add directly to content:

{{ block class="Company\Bannermanager\Block\Index\Index"  template="Company_Bannermanager::bannermanager.phtml" }}
  • thanks for your answer.I have my layout file also.i will update the question
    – Mujahidh
    Nov 21, 2017 at 12:42
  • @Mujahidh Okay! Update your question with the new errors also. Nov 21, 2017 at 12:42
  • @ Khoa TruongDinh previously also the layout file is there
    – Mujahidh
    Nov 21, 2017 at 12:47

Your Answer

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

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