Renderer variables

Any template block (aka instance of Magento\Framework\View\Element\Template) that has a name that starts with loki automatically gets certain variables inserted to make rendering other blocks or assets easier.

These variables are:

  • $viewModelFactory (instance of Loki\Components\Factory\ViewModelFactory);
  • $blockRenderer (instance of Loki\Components\Util\Block\BlockRenderer);
  • $templateRenderer (instance of Loki\Components\Util\Block\TemplateRenderer);
  • $childRenderer (instance of Loki\Components\Util\Block\ChildRenderer);
  • $imageRenderer (instance of Loki\Components\Util\Block\ImageRenderer);

The renderer variables are discussed here.

Block rendering

The $blockRenderer variable - an instance of the class \LokiCheckout\Util\Block\BlockRenderer - allows you to render a block identified either by its layout name.

For instance, the loader icon can be easily inserted like this:

<?= $blockRenderer->html('loki.checkout.utils.loader-small') ?>

Similarly, a block can be rendered by passing its template:

<?= $blockRenderer->html('form/field/text/script.phtml', $block) ?>

The utitity has two main rendering methods:

  • html(string $blockIdentifier, ?AbstractBlock $parentBlock = null, array $data = []): AbstractBlock
  • get(string $blockIdentifier, ?AbstractBlock $parentBlock = null, array $data = []): AbstractBlock

Both methods boil down to the same thing - instantiating a block. The html() method actually calls upon the get() method to retrieve a block object and then calls upon the toHtml() method of that object (so $block->toHtml()).

Note that the $blockRenderer utility does not take the layout hierarchy into account. It assumes that a block is defined somewhere with a specific block name (in the XML layout: <block name="foobar" />) or that there is a PHTML template somewhere available. This means that you might still want to call upon child blocks regularly as well:

<?= $block->getChildHtml($childAlias) ?>

Adding more block prefixes

The class Loki\Components\Observer\AssignAdditionalBlockVariables its constructor argument blockPrefixes defines which blocks automatically get these template variables inserted. To add your own block, either use the existing prefix loki or add your own block prefix or block name:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Loki\Components\Observer\AssignAdditionalBlockVariables">
        <arguments>
            <argument name="blockPrefixes" xsi:type="array">
                <item name="foobar" xsi:type="string">foobar</item>
            </argument>
        </arguments>
    </type>
</config>
Last modified: September 25, 2025