Add a new block
Open the packages/Acme/etc/blocks.php file, and add a new block:
<?php
$config = [
Core_Block::TYPE => [
/* ... */
'banner' => [ // The block identifier
'class' => Acme_Block_Banner::class, // Optional
'caption' => 'Welcome to MageWork!',
],
/* ... */
],
];
Create a new class: packages/Acme/Block/Banner.php
<?php
declare(strict_types=1);
class Acme_Block_Banner extends Core_Block
{
public function execute(): void
{
$this->setImage('media/banner.png');
}
public function canShow(): bool
{
return true;
}
}
The execute method is called before template rendering. It allows you to implement the code logic and inject data into the template.
Open any template file, example: packages/Acme/template/page.phtml
<?= $this->getBlock('block/banner', ['alt' => 'My Banner'], 'banner') ?>
The first argument is the block template file in "packages/Acme/template" (required).
The second argument is the data to send to the block (optional).
The last argument is the block identifier defined in the configuration file (optional).
Use keep() to forward only part of the current object's data to the block:
<?= $this->getBlock('block/head', $this->keep(['meta_title', 'meta_description']), 'head') ?>
Finally, create the template file: packages/Acme/template/block/banner.phtml
<?php /** @var Acme_Block_Banner $this */ ?>
<?php if ($this->canShow()): ?>
<figure>
<img src="<?= $this->getAssetUrl($this->getImage()) ?>" alt="<?= App::escapeHtmlAttr($this->getAlt()) ?>" />
<figcaption><?= App::escapeHtml($this->getCaption()) ?></figcaption>
</figure>
<?php endif; ?>
- Installation
- Configuration
- Add a new package
- Add a new HTML page
- Templating best practices
- Add a new block
- Assets
- Serve any type of file
- Rewrite a route
- Data assignment
- Objects and class fallback
- Models
- Database
- Forms
- Session messages
- Send emails
- Captcha
- Framework tools
- Console Commands
- Hooks
- Custom shared libraries
- External libraries with composer
- Write content in Markdown
- Static Site Generator