Create a new Magento 2 Module, called ‘Jeff_Office’
registration.php file
#app/code/Jeff/Office/registration.php
module.xml file
#app/code/Jeff/Office/etc/module.xml
frontend routes.xml
#app/code/Jeff/Office/etc/frontend
Index.php controller action file
#app/code/Jeff/Office/Controller/Index/Index.php resultPageFactory = $resultPageFactory; parent::__construct($context); } public function execute() { $resultPage = $this->resultPageFactory->create(); //Text Block Examples $block = $resultPage->getLayout()->createBlock( 'Magento\Framework\View\Element\Text', 'example_1' )->setText( ' Text_1 This is a text this is a text this is a text.
' ); $resultPage->getLayout()->setChild( 'content', $block->getNameInLayout(), 'exmaple_1_alias' ); //ListText Block examples $blockT = $resultPage->getLayout()->createBlock( 'Magento\Framework\View\Element\Text\ListText', 'example_2' ); $resultPage->getLayout()->setChild( 'content', $blockT->getNameInLayout(), 'example_2_alias' ); $block2A = $resultPage->getLayout()->createBlock( 'Magento\Framework\View\Element\Text', 'example_2a' )->setText( 'Text_2A: First Text block
' ); $resultPage->getLayout()->setChild( 'example_2', $block2A->getNameInLayout(), 'example_2a_alias' ); $block2B = $resultPage->getLayout()->createBlock( 'Magento\Framework\View\Element\Text', 'example_2b' )->setText( 'Text_2B: Second Text Block
' ); $resultPage->getLayout()->setChild( 'example_2', //the parent block name $block2B->getNameInLayout(), //the child block name 'example_2b_alias' ); //Message Block Examples $messageBlock = $resultPage->getLayout()->createBlock( 'Magento\Framework\View\Element\Messages', 'example_3' ); $messageBlock->addSuccess('Text_3A: Success'); $messageBlock->addNotice('Text_3B: Notice'); $messageBlock->addWarning('Text_3C: Warning'); $messageBlock->addError('Text_3D: Error'); $resultPage->getLayout()->setChild( 'content', $messageBlock->getNameInLayout(), 'example_3_alias' ); //Template Block examples $templateBlock = $resultPage->getLayout()->createBlock( 'Magento\Framework\View\Element\Template', 'example_templateblock' )->setTemplate( 'Jeff_Office::office/no4/example.phtml' ); $resultPage->getLayout()->setChild( 'content', $templateBlock->getNameInLayout(), 'example_4_alias' ); //Custom Block Examples $customBlock = $resultPage->getLayout()->createBlock( 'Jeff\Office\Block\Main', 'main_block' )->setTemplate( 'Jeff_Office::office/no4/hello.phtml' ); $resultPage->getLayout()->setChild( 'content', $customBlock->getNameInLayout(), 'customblock_alias' ); return $resultPage; } }
Mian.php Block file
#app/code/Jeff/Office/Block/Main.php
jeff_office_index_index.xml layout file
#app/code/Jeff/Office/view/frontend/layout/jeff_office_index_index.xmal
Three Template files for the Block usage
template.phtml file
#app/code/Jeff/Office/view/frontend/templates/office/no4/template.phtml Template Title
You should see this
You should see another this.
example.phtml file
#app/code/Jeff/Office/view/frontend/templates/office/no4/example.phtml Example Page
Example Solution, Example, Example
Solution Example, Solution, Solution
hello.phtml file
#app/code/Jeff/Office/view/frontend/templates/office/no4/hello.phtml My Own Block Template
Hello Tempatle
helloPublic(); ?>
Install our new created module
Typing in following commands
php bin/magento module:enable Jeff_Office php bin/magento setup:upgrade php bin/magento cache:clean php bin/magento cache:flush
Opening your browser, and type in http://yourdomain.com/jeff_office/index/index, you will see the result: