1

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

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

What part i am missing in first method.

Update

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">
<head>
    <title>Banners</title>
</head> 
<body>
    <referenceContainer name="content">
        <block class="Company\Bannermanager\Block\Index\Index" name="homebanner"  template="Company_Bannermanager::bannermanager.phtml"/>
    </referenceContainer>
</body>

3
  • 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

0

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.

[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="Company\Bannermanager\Block\Index\Index" name="homebanner"  template="Company_Bannermanager::bannermanager.phtml"/>
</referenceContainer>

Or add directly to content:

{{ block class="Company\Bannermanager\Block\Index\Index"  template="Company_Bannermanager::bannermanager.phtml" }}
3
  • 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, privacy policy and cookie policy

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