sympress/twig-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sympress/twig-bundle

Composer 安装命令:

composer require sympress/twig-bundle

包简介

Twig integration bundle for SymPress with Symfony TwigBundle feature parity.

README 文档

README

Checks Release PHP Downloads License: GPL-2.0-or-later Security Policy

sympress/twig-bundle integrates Twig into the SymPress kernel while delegating the core feature set to Symfony's symfony/twig-bundle.

Installation

composer require sympress/twig-bundle

The bundle is discovered by the SymPress kernel through the package metadata in composer.json. It requires PHP 8.5, SymPress Kernel, SymPress FrameworkBundle, Symfony TwigBundle, and Twig 3.

Features

  • Symfony TwigBundle compiler passes and service definitions
  • Symfony Twig configuration under the twig extension alias
  • Template lookup in project templates/, bundle Resources/views, and bundle templates
  • Autoconfiguration for Twig extensions, loaders, runtimes, and Twig attributes
  • Injectable TemplateRendererInterface
  • Optional SymPress API for service-backed Twig globals

Configuration

Twig configuration uses Symfony's standard twig extension alias:

twig:
    default_path: '%kernel.project_dir%/templates'
    strict_variables: '%kernel.debug%'

The bundle loads its own service wiring from Resources/config/services.yaml and keeps Symfony TwigBundle service definitions intact. Project services can keep using normal Twig extension, loader, runtime, and attribute APIs.

Twig Extensions

Services implementing Twig's extension, loader, or runtime interfaces are autoconfigured the same way as in Symfony:

use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

final class ShopTwigExtension extends AbstractExtension
{
    public function getFunctions(): array
    {
        return [
            new TwigFunction('shop_name', fn (): string => 'SymPress'),
        ];
    }
}

Twig attributes from twig/twig are supported as well:

use Twig\Attribute\AsTwigFunction;

final class ShopTwigRuntime
{
    #[AsTwigFunction('shop_name')]
    public function shopName(): string
    {
        return 'SymPress';
    }
}

Twig Globals

Services can provide several globals by implementing GlobalProviderInterface:

use SymPress\TwigBundle\Extension\GlobalProviderInterface;

final class AppGlobals implements GlobalProviderInterface
{
    public function getGlobals(): iterable
    {
        return [
            'app_name' => 'SymPress',
        ];
    }
}

For one service-backed global, tag a service class with AsTwigGlobal:

use SymPress\TwigBundle\Attribute\AsTwigGlobal;

#[AsTwigGlobal('current_account')]
final class CurrentAccount
{
}

Rendering

Inject SymPress\TwigBundle\Renderer\TemplateRendererInterface when a service needs view rendering without extending a controller base class.

use SymPress\TwigBundle\Renderer\TemplateRendererInterface;

final readonly class PageRenderer
{
    public function __construct(
        private TemplateRendererInterface $templates,
    ) {
    }

    public function render(): string
    {
        return $this->templates->render('page.html.twig', [
            'title' => 'Hello SymPress',
        ]);
    }
}

Controllers can use TwigResponseTrait when they need a Symfony Response from a Twig template without coupling to a concrete renderer implementation.

Quality Checks

composer qa

The QA command runs PHPCS, PHPStan, and PHPUnit with the package configuration used by the GitHub Actions workflow.

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0-or-later
  • 更新时间: 2026-06-14

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固