hryvinskyi/magento2-theme-assets
Composer 安装命令:
composer require hryvinskyi/magento2-theme-assets
包简介
Provides asset rendering functionality for scripts, stylesheets and asset path resolution
README 文档
README
A Magento 2 module that provides asset rendering functionality for scripts, stylesheets, and asset path resolution.
Features
- Asset Rendering: Render JavaScript and CSS assets with multiple loading strategies
- Inline & External: Support for both inline and external asset rendering
- Loading Strategies: Sync, async, and defer loading for scripts
- Preloading: Resource preload hints with fetch priority support
- Content Strategies: Multiple fallback strategies for asset content retrieval
- Caching: Built-in asset caching for improved performance
Installation
Via Composer
composer require hryvinskyi/magento2-theme-assets
Manual Installation
- Create the directory
app/code/Hryvinskyi/ThemeAssets - Copy the module files to the directory
- Run the following commands:
bin/magento module:enable Hryvinskyi_ThemeAssets bin/magento setup:upgrade bin/magento cache:flush
Requirements
- PHP ^8.1
- Magento 2.4.x
- hryvinskyi/magento2-head-tag-manager
Usage
In PHTML Templates
<?php use Hryvinskyi\ThemeAssets\Api\AssetRendererInterface; /** @var \Hryvinskyi\Base\Model\ViewModelRegistry $viewModels */ $assetRenderer = $viewModels->require(AssetRendererInterface::class); // Render inline script echo $assetRenderer->renderScript('Module_Name::js/script.js'); // Render external script with async loading echo $assetRenderer->renderScript( 'Module_Name::js/script.js', AssetRendererInterface::RENDER_EXTERNAL, AssetRendererInterface::LOAD_ASYNC ); // Render external script with defer and preload echo $assetRenderer->renderScript( 'Module_Name::js/script.js', AssetRendererInterface::RENDER_EXTERNAL, AssetRendererInterface::LOAD_DEFER, AssetRendererInterface::PRELOAD_ENABLED, [], ['priority' => AssetRendererInterface::PRIORITY_HIGH] ); // Render inline stylesheet echo $assetRenderer->renderStyle('Module_Name::css/styles.css'); // Render external stylesheet with preload echo $assetRenderer->renderStyle( 'Module_Name::css/styles.css', AssetRendererInterface::RENDER_EXTERNAL, AssetRendererInterface::PRELOAD_ENABLED ); // Get raw file content $content = $assetRenderer->getContent('Module_Name::js/config.json');
Constants
Render Types
AssetRendererInterface::RENDER_INLINE- Inline the asset contentAssetRendererInterface::RENDER_EXTERNAL- Link to external file
Load Strategies (for scripts)
AssetRendererInterface::LOAD_SYNC- Synchronous loading (blocking)AssetRendererInterface::LOAD_ASYNC- Asynchronous loadingAssetRendererInterface::LOAD_DEFER- Deferred loading
Preload Options
AssetRendererInterface::PRELOAD_ENABLED- Enable resource preloadingAssetRendererInterface::PRELOAD_DISABLED- Disable resource preloading
Fetch Priority
AssetRendererInterface::PRIORITY_HIGH- High fetch priorityAssetRendererInterface::PRIORITY_LOW- Low fetch priorityAssetRendererInterface::PRIORITY_AUTO- Browser default priority
Options Array
The $options parameter supports:
[
'priority' => AssetRendererInterface::PRIORITY_HIGH, // Fetch priority for preload
'attributes' => [ // Additional HTML attributes
'data-custom' => 'value',
'id' => 'my-script'
],
'content_before' => '/* prefix */', // Content to prepend (inline only)
'content_after' => '/* suffix */' // Content to append (inline only)
]
Asset Content Strategies
The module uses a strategy pattern for retrieving asset content with multiple fallbacks:
- MagentoAssetSystemStrategy - Uses Magento's built-in asset system
- DirectStaticFileStrategy - Direct access to static file directory
- FilesystemServiceStrategy - Filesystem service with absolute path
Strategies are tried in order until content is successfully retrieved.
Logging
Asset retrieval failures are logged to var/log/asset_renderer.log.
API Reference
AssetRendererInterface
| Method | Description |
|---|---|
createAsset(string $fileId, array $params = []): File |
Create and cache an asset |
renderScript(...) |
Render a JavaScript asset |
renderStyle(...) |
Render a CSS asset |
getContent(string $fileId, array $params = []): string |
Get raw file content |
AssetPathResolverInterface
| Method | Description |
|---|---|
getViewFilePath(string $fileId, array $params = []): string |
Get asset file path |
getViewFileContent(string $fileId, array $params = []): string|false |
Get asset file content |
createAsset(string $fileId, array $params = []): File |
Create and cache an asset |
License
MIT License
Author
Volodymyr Hryvinskyi volodymyr@hryvinskyi.com
GitHub: https://github.com/hryvinskyi
hryvinskyi/magento2-theme-assets 适用场景与选型建议
hryvinskyi/magento2-theme-assets 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 147 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 hryvinskyi/magento2-theme-assets 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hryvinskyi/magento2-theme-assets 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 147
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-04