Block

Creates just a Block

Create a Simple Block for the Frontend or Adminhtml. It is also possible to insert the block based on a layout handle and a reference.

Example

Create a Notice Block

Input for the block form

  • classname: Notices
  • methodname: getCustomNotice
  • scope: Frontend
  • layout handle: default
  • reference type: Container
  • reference name: content

Use the snippet in the Magento 2 module creator.

Files

view/frontend/templates/test.phtml

<?php
/**
 * @var $block \Mage2Gen\Module\Block\Test
 */
?>
<div>
	<?= $block->Test() ?>
	<?= __('Hello Mage2Gen_Module::test.phtml') ?>
</div>

Block/Test.php

<?php
declare(strict_types=1);

namespace Mage2Gen\Module\Block;

class Test extends \Magento\Framework\View\Element\Template
{

    /**
     * Constructor
     *
     * @param \Magento\Framework\View\Element\Template\Context  $context
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        array $data = []
    ) {
        parent::__construct($context, $data);
    }

    /**
     * @return string
     */
    public function Test()
    {
        //Your block code
        return __('Hello Developer! This how to get the storename: %1 and this is the way to build a url: %2', $this->_storeManager->getStore()->getName(), $this->getUrl('contacts'));
    }
}