2

How to achieve the setTemplate() in the custom modules constructor?

protected function _construct()
{
    $this->setTemplate('Myfolder/filter.phtml');
}

protected function _toHtml() {
    return parent::_toHtml();
}

Are we achieving the same in the layout file or how?

1 Answer 1

6

I achieved this by adding the $_template variable

protected $_template = 'Myfolder/filter.phtml';

<?php
namespace Namespace\Module\Block;
use Magento\Framework\View\Element\Template;
class Test extends Template
{

    protected $_template = 'Myfolder/filter.phtml';

}

example you can see

vendor\magento\module-layered-navigation\Block\Navigation\State.php

Reference

10
  • Should we give Vendor_Module::Myfolder/filter.phtml? and how come you are using $_template = 'test.phtml'; inside class? no need to use that $_template anywhere in the constructor?
    – Sushivam
    Aug 17, 2016 at 12:39
  • i think it already referring the you module also in magento core i didn't see any module name with it. so my opinion Myfol‌​der/filter.phtml Aug 17, 2016 at 12:41
  • ok Qaisar, where im i supposed to use that $_template? in _construct() or where?
    – Sushivam
    Aug 17, 2016 at 12:42
  • as i mention in my code you have add protected variable of $_template in class. just as i added the example Aug 17, 2016 at 12:45
  • i tried directly in the phtml as <block class="Magento\Catalog\Block\Product\ListProduct" name="product_list" template="Magento_Catalog::product/list.phtml">
    – Sushivam
    Aug 18, 2016 at 8:59

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.