mehrkanal / twig-encore-extension
用于在 Twig 模板中使用 entrypoints.json 的 Twig 扩展
4.0.2
2023-06-27 11:37 UTC
Requires
- php: ^8.1
- symfony/twig-bridge: *
- symfony/webpack-encore-bundle: ^v2.0.0
- twig/twig: ^1.42.5 || ^2.12.5 || ^3.0.3
Requires (Dev)
- laminas/laminas-servicemanager: ^3.20.0
- phpstan/phpstan: ^1.4.8
- rector/rector: ^0.17.1
- symplify/easy-coding-standard: ^11.0.8
This package is auto-updated.
Last update: 2024-09-14 09:06:21 UTC
README
与 twig/twig
和 symfony/webpack-encore-bundle
一起使用
为: Laminas/Mezzio
应用程序创建,这些应用程序没有本地的 Symfony 核心组件。
警告:如果 Symfony 可用,只需直接使用 symfony/webpack-encore-bundle
包。
如何使用和配置 Encore
composer require symfony/webpack-encore-bundle
- npm i @symfony/webpack-encore
- webpack.config.js
setOutputPath()
应该位于公共文件夹中- 例如,
.setOutputPath('public/assets')
- 例如,
setPublicPath()
应该在资产所在的文件夹中- 例如,
setPublicPath('/assets')
- 例如,
- 将
['twig']['asset_url']
设置为绝对路径,例如在您的 twig.global.php- 最好设置为:
/app/public/assets/
- 注意:如果前面的路径不以反斜杠结尾,则在 asset_url 后面将附加一个正斜杠
- 例如,asset_path 是
/assets
,则 URL 将是<document_root>/assets/entrypoints.json
- 例如,asset_path 是
- 最好设置为:
- 将
Mehrkanal\EncoreTwigExtension\ConfigProvider::class
包含到您的全局配置中 - 将
Mehrkanal\EncoreTwigExtension\Extensions\GetCssSourceTwigExtension:class
包含到您的['twig']['extensions']
配置中。 - 将
\Symfony\Bridge\Twig\Extension\AssetExtension::class
包含到您的['twig']['extensions']
配置中。 - 将
Symfony\WebpackEncoreBundle\Twig\EntryFilesTwigExtension:class
包含到您的['twig']['extensions']
配置中。
use Mehrkanal\EncoreTwigExtension\Extensions\GetCssSourceTwigExtension; use Symfony\WebpackEncoreBundle\Twig\EntryFilesTwigExtension; use Twig\Environment; use Twig\Loader\FilesystemLoader; require_once 'vendor/autoload.php'; $container = require __DIR__ . '/container.php'; $loader = new FilesystemLoader('./templates'); $twig = new Environment($loader, [ 'debug' => true ]); $twig->addExtension($container->get(EntryFilesTwigExtension::class)); $twig->addExtension($container->get(GetCssSourceTwigExtension::class));
现在我能做什么?
- 使用 Encore NodeJS 脚本自动生成资产
- 在 Twig 中使用
encore_entry_link_tags(<entrypoint>)
函数来获取所有必需的样式表链接标签 - 在 Twig 中使用
encore_entry_script_tags(<entrypoint>)
函数来获取所有必需的脚本标签 - 在 Twig 中使用
encore_get_css_source(<entrypoint>)
函数来获取所有 CSS 代码
与 Encore Webpack 配置中的 setEntry 一起使用。
Stan
docker run -it -v $PWD:/app -v $SSH_AUTH_SOCK:$SSH_AUTH_SOCK -w /app -e SSH_AUTH_SOCK=$SSH_AUTH_SOCK -e SSH_AGENT_PID=$SSH_AGENT_PID registry.mehrkanal.com/docker/phpx/cli:8.1-build bash composer up composer run stan composer run cf
有用的链接
- 原始代码
- https://symfony.com.cn/doc/current/frontend/encore/installation.html
- https://gitlab.mehrkanal.com/mehrkanal/skeleton.git