neutromelabs/magento2-headless-components
Composer 安装命令:
composer require neutromelabs/magento2-headless-components
包简介
Simple phtml template rendering without XML layout. Includes caching, theming via native Magento overrides, and first-class AlpineJS support.
README 文档
README
Uncomplicate rich modular layouts based on AlpineJS-components
with Magento 2 Headless Components experience.
Magento 2 Headless Components Module
Provides a simple way to render phtml-based templates, without the need to register them in the xml layout. Includes caching, native Magento theme overrides, and first-class AlpineJS support.
Overview
This is a tool to do a
\Magento\Framework\App\ObjectManager::getInstance() ->get(\Magento\Framework\View\LayoutInterface::class) ->createBlock(\Magento\Framework\View\Element\Template::class) ->setTemplate('Vendor_Module::component/headless/block/template.phtml') ->toHtml();
in a "better" way:
- Native Magento theme overrides
- Block caching
- FPC compatible
- JS-companion blocks for AlpineJS
The Problem
- Magento 2 has no ability to simply render templates like
{{ templates/button }}as almost every other templating engine provides. - AlpineJS-based themes and components become hard to upgrade when overwritten, because of the need to copy-paste the whole JS logic within the template.
Proposed Solution
This module provides an easier way to render templates, with first-class AlpineJS support
via decoupling JS logic from templates into .script.phtml companion files.
The layout system is used for macro-level layouting (header, footer, product-details),
and Headless Components for atom/block level - button, input, accordion.
Installation
composer require neutromelabs/magento2-headless-components bin/magento setup:upgrade
How To Use
Basic Usage
/** view/frontend/templates/some/template.phtml */ <?= $block->headlessComponentRenderer->render('atom/input-text', [ 'type' => 'email', 'name' => 'email', 'label' => __('Email'), 'attributes' => [ 'required' => 1, 'value' => $address->getEmail(), 'x-model' => 'displayEmail', '@change' => 'await save()' ] ], 'address-form.input-email') ?>
Configuration
Configure the component module via DI:
<!-- di.xml --> <type name="NeutromeLabs\HeadlessComponents\Service\Renderer"> <arguments> <argument name="componentModule" xsi:type="string">Vendor_MyComponents</argument> </arguments> </type>
Then create your templates:
Vendor/MyComponents/view/frontend/templates/atom/input-text.phtml
Vendor/MyComponents/view/frontend/templates/atom/input-text.script.phtml <!-- AlpineJS companion -->
Overriding Components
Use standard Magento theme overrides:
app/design/frontend/Vendor/theme/Vendor_MyComponents/templates/atom/input-text.phtml
Script Companions (AlpineJS)
For any template foo/bar.phtml, create foo/bar.script.phtml for the JS logic.
The script companion is automatically detected and rendered once per page in before.body.end.
<!-- atom/button.phtml --> <button x-data="myButton()" @click="handleClick"> <span x-text="label"></span> </button>
<!-- atom/button.script.phtml --> <script> function myButton() { return { label: 'Click me', handleClick() { console.log('clicked'); } } } </script>
neutromelabs/magento2-headless-components 适用场景与选型建议
neutromelabs/magento2-headless-components 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 02 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 neutromelabs/magento2-headless-components 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 neutromelabs/magento2-headless-components 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 14
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2025-02-15