20

Magento 2

I have installed a sample Bannerslider module downloaded from Magestore.

When I call bannerslider.phtml into CMS Home Page, it's showing unreliable error on the home page.

{{block class="Magento\Bannerslider\Block\Slideshow" template="bannerslider.phtml"}}

The error message is We're sorry, an error has occurred while generating this email.

I changed the block class as well, still, it shows the same error. Can you please suggest me a proper way?

If you need furthermore code detail, Please mentioned me in a comment. I am really stuck here.

After uncomment SetEnv MAGE_MODE developer in .htaccess file error message being the change and showing

Error filtering template: Invalid block type: Magento\Bannerslider\Block\Slideshow

By the way, the admin section of this module is working fine.

Block class

namespace Magento\Bannerslider\Block;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\Filesystem;
use Magento\Framework\App\Filesystem\DirectoryList;
class Slideshow extends Template
{
    public function __construct(
        Context $context,
        \Magento\Bannerslider\Model\BannerFactory $bannerFactory,
        \Magento\Framework\Filesystem $fileSystem,
        \Magento\Framework\StoreManagerInterface $storeManager,
        array $data = array()
    ) {
        $this->_fileSystem = $fileSystem;
        $this->_bannerFactory = $bannerFactory;
        $this->_storeManager = $storeManager;
        parent::__construct($context, $data);
    }
    public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }
    public function getBaseJs($fileName){

        return $this->_storeManager->getStore()->getBaseUrl(
                \Magento\Framework\UrlInterface::URL_TYPE_MEDIA
            ).'bannerslider/js/'.$fileName;
    }
    public function getBannerData(){
        $banners = $this->_bannerFactory->create()
                        ->getCollection()
                        ->addFieldToFilter('status',1);
        foreach ($banners as $banner){
                $result['banners'][] = $banner->getData();
        }
        return $result;
    }
    public function getBannerImage($imageName) {

        $mediaDirectory = $this->_storeManager->getStore()->getBaseUrl(
                \Magento\Framework\UrlInterface::URL_TYPE_MEDIA
            );
        return $mediaDirectory.'bannerslider/images'.$imageName;
    }
}

Module.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Magento_Bannerslider" setup_version="1.0.0"/>
</config>
5
  • 3
    you need to uncoment SetEnv MAGE_MODE developer from .htaccess file to eloborate error. Commented Oct 7, 2015 at 10:37
  • Thank you Taral. I have done it. Now error message has been change. I have edited my question now. Commented Oct 7, 2015 at 10:40
  • Please show me your block class and module.xml code.
    – Amit Bera
    Commented Oct 7, 2015 at 12:57
  • I had added block class and module.xml code to post. Commented Oct 7, 2015 at 13:48
  • @TaralPatoliya Thank you for your suggestion... It really helped for me!
    – DRAJI
    Commented Jul 15, 2016 at 5:57

2 Answers 2

43

Try below code in cms page, If you want to call phtml with in cms page.

{{block class="Magento\Bannerslider\Block\Slideshow" template="Magento_Bannerslider::bannerslider.phtml"}} 
25

In magento,if you want to add block class in cms page, then you can tre below code

{{block class="Magento\Bannerslider\Block\Slideshow" template="Magento_Bannerslider::bannerslider.phtml"}} 

Formula is :

{{block class="[BlockClassFullName]" template="[VendorName]_[ModuleName]::[YourTemplateFileLocation].phtml"}} 

Default block:

{{block class="Magento\Framework\View\Element\Template" template="[VendorName]_[ModuleName]::[YourTemplateFileLocation].phtml"}} 

This is equal to Magento 1.X version code

{{ block type="core/template" template="YourtemplateFileLocation.phtml"}}
3
  • hi @Amit, can we use cacheable="false" here? when used in cms Commented Mar 21, 2019 at 13:38
  • Can use but it makes you make non Cacheable
    – Amit Bera
    Commented Mar 21, 2019 at 14:13
  • Yes i need that one, what is the syntax in using cms, i used it but it didn't work Commented Mar 21, 2019 at 14:20

Your Answer

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

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