How to call a PHTML file within a CMS page Magento 2?

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

    you need to uncoment SetEnv MAGE_MODE developer from .htaccess file to eloborate error.

    Thank you Taral. I have done it. Now error message has been change. I have edited my question now.

    Please show me your block class and module.xml code.

    I had added block class and module.xml code to post.

    @TaralPatoliya Thank you for your suggestion... It really helped for me!

  • Keyur Shah

    Keyur Shah Correct answer

    6 years ago

    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"}} 
    

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM