mehrkanal/twig-encore-extension

用于在 Twig 模板中使用 entrypoints.json 的 Twig 扩展

4.0.2 2023-06-27 11:37 UTC

This package is auto-updated.

Last update: 2024-09-14 09:06:21 UTC


README

twig/twigsymfony/webpack-encore-bundle 一起使用

为: Laminas/Mezzio 应用程序创建,这些应用程序没有本地的 Symfony 核心组件。
警告:如果 Symfony 可用,只需直接使用 symfony/webpack-encore-bundle 包。

如何使用和配置 Encore

  1. composer require symfony/webpack-encore-bundle
  2. npm i @symfony/webpack-encore
  3. webpack.config.js
    • setOutputPath() 应该位于公共文件夹中
      • 例如,.setOutputPath('public/assets')
    • setPublicPath() 应该在资产所在的文件夹中
      • 例如,setPublicPath('/assets')
  4. ['twig']['asset_url'] 设置为绝对路径,例如在您的 twig.global.php
    • 最好设置为:/app/public/assets/
    • 注意:如果前面的路径不以反斜杠结尾,则在 asset_url 后面将附加一个正斜杠
      • 例如,asset_path 是 /assets,则 URL 将是 <document_root>/assets/entrypoints.json
  5. Mehrkanal\EncoreTwigExtension\ConfigProvider::class 包含到您的全局配置中
  6. Mehrkanal\EncoreTwigExtension\Extensions\GetCssSourceTwigExtension:class 包含到您的 ['twig']['extensions'] 配置中。
  7. \Symfony\Bridge\Twig\Extension\AssetExtension::class 包含到您的 ['twig']['extensions'] 配置中。
  8. 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

有用的链接

团队

MKLabs