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

protected function _construct()

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

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


I achieved this by adding the $_template variable

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

namespace Namespace\Module\Block;
use Magento\Framework\View\Element\Template;
class Test extends Template

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


example you can see



|improve this answer|||||
  • 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 '16 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 – Qaisar Satti Aug 17 '16 at 12:41
  • ok Qaisar, where im i supposed to use that $_template? in _construct() or where? – Sushivam Aug 17 '16 at 12:42
  • as i mention in my code you have add protected variable of $_template in class. just as i added the example – Qaisar Satti Aug 17 '16 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 '16 at 8:59

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.