dansol / webpack-encore-view-helper
Symfony webpack encore 模板助手
1.0.1
2024-03-17 17:55 UTC
This package is not auto-updated.
Last update: 2024-09-29 20:29:25 UTC
README
webpack-encore-view-helper 是一个 laminas-view 助手,帮助渲染使用 Symfony Webpack Encore(将 Webpack 集成到应用中的一种简单方法 - https://symfony.com.cn/doc/current/frontend.html#webpack-encore)时所需的全部动态脚本和链接标签。
安装
运行以下命令进行安装
$ composer require dansol/webpack-encore-view-helper
文档
将 webpack_encore_view_helper_config.global.php.dist 复制到 config/autoload 并重命名为 webpack_encore_view_helper_config.global.php
根据您的环境调整配置。
示例
<?php return [ ... 'webpack_encore_view_helper_config'=>[ /* webpack encore entry points */ 'entrypoints_map' => json_decode(file_get_contents(__DIR__ . '/../../public/build/entrypoints.json'),true), /* map view to entrypoint ( array can be empty if templates name match entrypoint name when using auto* parameters */ 'template_entrypoint_map'=>[ 'layout::default'=>'app', ] ] ];
在配置加载器中加载 ConfigProvider。
Mezzio 示例
<?php $aggregator = new ConfigAggregator([ // ... other stuff \WebpackEncoreViewHelper\ConfigProvider::class, // Default App module config new PhpFileProvider(realpath(__DIR__) . '/autoload/{{,*.}global,{,*.}local}.php'), // Load development config if it exists new PhpFileProvider(realpath(__DIR__) . '/development.config.php'), ], $cacheConfig['config_cache_path']); ?>
基本用法
... // load asset ( in layout template) $this->webpack_encore_assets('common entrypointname'); // if used a common entry // load asset ( in template that consume an entrypoint) $this->webpack_encore_assets('common entrypointname'); // if used a common entry // in layout template // render link tags echo $this->webpack_encore_assets()->render('css'); ... // render script tags echo $this->webpack_encore_assets()->render('js');
特殊参数
这些参数可以在父模板(布局模板)中使用 - 如果在子模板中使用,将会引发错误
- auto - 自动加载所有资源(父模板和内容中的第一个子模板)
- auto-route - 加载父模板(布局)的所有资源
- auto-child - 加载子模板(布局内容中的第一个模板)的所有资源。这些参数避免了在每个消耗入口点的模板中加载资源的需求,仅在主布局模板中加载资源
auto* 参数根据以下逻辑加载入口:根据 webpack_encore_view_helper_config.global.php 配置文件中 template_entrypoint_map 键配置的 templatename/entrypoint name 配置的 asset 加载(参见 webpack_encore_view_helper_config.global.php.dist)。如果不匹配,助手将尝试匹配模板名称等于入口名称。如果入口未解析,将引发错误。
基本示例
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /> <link rel="shortcut icon" href="https://getlaminas.org/images/favicon/favicon.ico" /> <?=$this->headTitle('mezzio')->setSeparator(' - ')->setAutoEscape(false)?> <?=$this->headMeta()?> <?php // load all asset for $this->webpack_encore_assets('auto'); ?> <?php // render links tags for this template(shared entry) and every asset required for the first child template in content echo $this->webpack_encore_assets()->render('css') ?> </head> <body class="app"> ... <div class="app-content"> <main class="container"> <?=$this->content?> </main> </div> <footer class="app-footer"> ... </footer> <?php // render scripts tags for this template(shared entry) and every asset required for the first child template in content echo $this->webpack_encore_assets()->render('js') ?> </body> </html>