[Solved] Block is not calling properly in magento 2 custom module

Mujahidh Asks: Block is not calling properly in magento 2 custom module
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 – (appcodeCompanyBannermanagerviewfrontendtemplates)

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

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

Block file location is -( appcodeCompanyBannermanagerBlockIndex )

I am getting this error

Error filtering template: Warning: Invalid argument supplied for foreach() in D:xampphtdocsmollys_spiritsappcodeCompanyBannermanagerviewfrontendtemplatesbannermanager.phtml on line 17

I know i can call this block like below

$blockObj= $block->getLayout()->createBlock('CompanyBannermanagerBlockIndexIndex');
$home_banners =  $blockObj->getHomepageBanners(); ?>

What part i am missing in first method.


My layout file is homebanner_index_index.xml-(appcodeEight25mediaBannermanagerviewfrontendlayout)

<?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="CompanyBannermanagerBlockIndexIndex" name="homebanner"  template="Company_Bannermanager::bannermanager.phtml"/>

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.