VOOZH about

URL: https://deepwiki.com/MahoCommerce/maho/14.3-block-api

⇱ Block API | MahoCommerce/maho | DeepWiki


Loading...
Last indexed: 15 May 2026 (ea8ab8)
Menu

Block API

Purpose and Scope

This document provides a technical reference for the Block API in Maho, which handles presentation logic and HTML rendering. Blocks serve as the view layer in Maho's MVC architecture, bridging business logic from models with HTML output through templates. The base class Mage_Core_Block_Abstract defines the rendering lifecycle, child management, and caching mechanisms used throughout the platform.

For information about Layout XML and block tree construction, see Layout and Template API For frontend theme architecture, see Theme Architecture


Block Class Hierarchy

All blocks in Maho extend from Mage_Core_Block_Abstract, which provides core functionality for the block system. This base class itself extends \Maho\DataObject to provide flexible data storage and accessors.

Core Block Inheritance Structure:


Sources: app/code/core/Mage/Core/Block/Abstract.php33-34 app/code/core/Mage/Adminhtml/Block/Template.php13 app/code/core/Mage/Adminhtml/Block/Widget.php19-20 app/code/core/Mage/Adminhtml/Block/Sales/Order/View/History.php13 app/code/core/Mage/Adminhtml/Block/Sales/Order/Comments/View.php13


Core Block Properties

The Mage_Core_Block_Abstract class maintains several critical properties for identifying and organizing blocks within the layout tree:

PropertyTypeDescription
$_nameInLayoutstringUnique identifier for block in layout tree. app/code/core/Mage/Core/Block/Abstract.php54
$_layoutMage_Core_Model_LayoutReference to parent layout instance. app/code/core/Mage/Core/Block/Abstract.php61
$_parentMage_Core_Block_AbstractParent block in hierarchy. app/code/core/Mage/Core/Block/Abstract.php68
$_parentBlockMage_Core_Block_AbstractAlternative parent reference used for internal traversal. app/code/core/Mage/Core/Block/Abstract.php138
$_aliasstringShort alias used by parent to reference this block. app/code/core/Mage/Core/Block/Abstract.php75
$_childrenarrayChild blocks indexed by alias. app/code/core/Mage/Core/Block/Abstract.php89
$_sortedChildrenarrayOrdered list of child block names. app/code/core/Mage/Core/Block/Abstract.php96
$_isAnonymousboolWhether block was auto-named. app/code/core/Mage/Core/Block/Abstract.php131

Sources: app/code/core/Mage/Core/Block/Abstract.php50-170


Block Lifecycle

The rendering process follows a strict sequence of preparation, rendering, and post-processing.


Key Lifecycle Methods:

MethodPurposeOverride Recommendation
__construct()Object initialization and dependency injection for _factory and _app. app/code/core/Mage/Core/Block/Abstract.php195-205Rarely
_construct()Internal constructor for initialization logic. app/code/core/Mage/Core/Block/Abstract.php212-217Common
setLayout()Attaches block to layout; triggers _prepareLayout() and layout events. app/code/core/Mage/Core/Block/Abstract.php292-298No
_prepareLayout()Hook to configure block or add children after layout is set. app/code/core/Mage/Core/Block/Abstract.php296Common
toHtml()Main entry point for rendering. Handles caching logic. app/code/core/Mage/Core/Block/Abstract.php313-334No
_beforeToHtml()Pre-render hook to fetch data or validate state. app/code/core/Mage/Core/Block/Abstract.php341-344Common
_toHtml()Actual generation of HTML output. app/code/core/Mage/Core/Block/Abstract.php351-354Common

Sources: app/code/core/Mage/Core/Block/Abstract.php195-354


Child Block Management

Blocks are organized into a tree structure where parents manage the rendering of their children.


Core Management Methods:

Sources: app/code/core/Mage/Core/Block/Abstract.php430-727


Block Caching

Maho provides a granular fragment caching system for blocks to improve performance.

Cache Constants:

ConstantValuePurpose
CACHE_KEY_PREFIX'BLOCK_'Prefix for block cache keys. app/code/core/Mage/Core/Block/Abstract.php38
CACHE_GROUP'block_html'The cache tag used for HTML blocks. app/code/core/Mage/Core/Block/Abstract.php42

Caching Logic: The toHtml() method checks if getCacheLifetime() is set. If active, it attempts to load HTML from the cache using a key generated via getCacheKey(). If a miss occurs, the block renders and saves the result to the cache using getCacheTags(). app/code/core/Mage/Core/Block/Abstract.php313-334

Sources: app/code/core/Mage/Core/Block/Abstract.php36-47 app/code/core/Mage/Core/Block/Abstract.php313-334


Security and Data Escaping

Maho provides robust mechanisms for escaping data within blocks to prevent XSS.

Escaping Methods:

Adminhtml Sales Helpers: The Mage_Adminhtml_Helper_Sales class provides specialized escaping that preserves safe links (e.g., in order history comments) while sanitizing the rest of the content via escapeHtmlWithLinks. app/code/core/Mage/Adminhtml/Helper/Sales.php108-143

Sources: app/code/core/Mage/Core/Helper/Abstract.php180-203 app/code/core/Mage/Adminhtml/Block/Template.php53-56 app/code/core/Mage/Core/Model/Security/HtmlEscapedString.php13-41 app/code/core/Mage/Adminhtml/Helper/Sales.php108-143


Helper Integration

Blocks have built-in access to helper classes for data manipulation and translation.

Sources: app/code/core/Mage/Core/Block/Abstract.php245-285 app/code/core/Mage/Core/Block/Abstract.php1002-1026