Magento Asked by Mujahidh on December 24, 2021
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
<?php
$blockObj= $block->getLayout()->createBlock('CompanyBannermanagerBlockIndexIndex');
$home_banners = $blockObj->getHomepageBanners(); ?>
What part i am missing in first method.
Update
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">
<head>
<title>Banners</title>
</head>
<body>
<referenceContainer name="content">
<block class="CompanyBannermanagerBlockIndexIndex" name="homebanner" template="Company_Bannermanager::bannermanager.phtml"/>
</referenceContainer>
</body>
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 -CompanyBannermanagerBlockIndexIndex
didn't initialize. So, you couldn't get its function.
[EDIT]
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="CompanyBannermanagerBlockIndexIndex" name="homebanner" template="Company_Bannermanager::bannermanager.phtml"/>
</referenceContainer>
Or add directly to content:
{{ block class="CompanyBannermanagerBlockIndexIndex" template="Company_Bannermanager::bannermanager.phtml" }}
Answered by Khoa TruongDinh on December 24, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP