Magento 2 – How to Call a PHTML File Within a CMS Page

magento2magento2-dev-betastatic-block

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>

Best Answer

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