You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TYPO3 v13+中includeStaticTypoScriptSourcesAtEnd的替代方案是什么?

TYPO3 13 动态加载TypoScript的替代方案

核心替代机制:站点配置处理器与前端中间件

在TYPO3 13中TemplateService被移除后,实现全局站点级、页面层级动态加载TypoScript的需求,可通过以下两种方式完成:

1. 全局站点级TypoScript加载

通过站点配置扩展机制,在站点初始化阶段注入全局TypoScript:

  • 自定义实现SiteConfigurationProcessorInterface接口的处理器类
  • 在process()方法中读取全局TypoScript内容,追加到站点配置的TypoScript集合中
  • 示例代码:
use TYPO3\CMS\Core\Site\SiteConfigurationProcessorInterface;
use TYPO3\CMS\Core\Site\SiteConfiguration;

class GlobalTypoScriptProcessor implements SiteConfigurationProcessorInterface
{
    public function process(SiteConfiguration $siteConfiguration, array $siteConfig, string $siteIdentifier): array
    {
        // 读取全局TypoScript文件内容
        $globalTsContent = file_get_contents('EXT:your_extension/Configuration/TypoScript/Global/setup.typoscript');
        // 追加到站点的TypoScript配置里
        $siteConfig['settings']['typoScriptSetup'] .= LF . $globalTsContent;
        return $siteConfig;
    }
}
  • 在Configuration/Services.yaml中注册处理器:
services:
  YourVendor\YourExtension\Site\GlobalTypoScriptProcessor:
    tags:
      - name: site.configuration.processor

2. 页面层级主题TypoScript加载

利用前端中间件,在请求处理阶段根据当前页面属性动态加载主题TypoScript:

  • 自定义实现MiddlewareInterface接口的中间件类
  • 在process()方法中获取当前页面记录,根据预设字段(比如自定义的theme字段)加载对应主题的TypoScript,合并到当前请求的TypoScript配置中
  • 示例代码:
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\TypoScript\TypoScriptService;

class PageThemeTypoScriptMiddleware implements MiddlewareInterface
{
    public function __construct(protected TypoScriptService $typoScriptService) {}

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $pageInfo = $request->getAttribute('frontend.page.information');
        $themeKey = $pageInfo?->getPageRecord()['theme'] ?? '';
        if (!empty($themeKey)) {
            $themeTsPath = "EXT:your_extension/Configuration/TypoScript/Themes/{$themeKey}/setup.typoscript";
            $absolutePath = \TYPO3\CMS\Core\Core\Environment::getPublicPath() . '/' . $themeTsPath;
            if (file_exists($absolutePath)) {
                $themeTsContent = file_get_contents($absolutePath);
                // 解析并合并主题TypoScript到现有配置
                $currentSetup = $request->getAttribute('frontend.typoscript.setup') ?? [];
                $parsedThemeTs = $this->typoScriptService->parse($themeTsContent);
                $mergedSetup = array_merge_recursive($currentSetup, $parsedThemeTs);
                $request = $request->withAttribute('frontend.typoscript.setup', $mergedSetup);
            }
        }
        return $handler->handle($request);
    }
}
  • 在Configuration/Services.yaml中注册中间件,确保在TypoScript解析后执行:
services:
  YourVendor\YourExtension\Middleware\PageThemeTypoScriptMiddleware:
    tags:
      - name: middleware
        identifier: 'your-extension/theme-ts-loader'
        after: 'typo3/cms-frontend/typoscript'

关键注意事项

  • 始终使用EXT语法引用TypoScript文件,避免硬编码绝对路径
  • 页面主题加载可结合TYPO3的缓存机制优化性能,减少重复文件读取
  • 测试时需验证全局与主题TypoScript的合并优先级,确保最终配置符合预期

内容的提问来源于stack exchange,提问作者John Miller

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 19:22:26