htmlburger/wpemerge-twig

启用 WP Emerge 中的 Twig 视图。

0.16.0 2020-05-23 12:15 UTC

This package is auto-updated.

Last update: 2024-09-15 02:06:22 UTC


README

启用在 WP Emerge 中使用 Twig 视图。

快速入门

  1. 在主题目录中运行 composer require htmlburger/wpemerge-twig
  2. 启动 WPEmerge 时,将 \WPEmergeTwig\View\ServiceProvider 添加到提供者数组中
    \App::make()->bootstrap( [
        'providers' => [
            \WPEmergeTwig\View\ServiceProvider::class,
        ],
    ] );

选项

默认选项

[
    // Automatically replace the default view engine for WP Emerge.
    'replace_default_engine' => true,

    // Pass .php views to the default view engine.
    // replace_default_engine must be true for this to take effect.
    'proxy_php_views' => true,

    // One or more directories to search for views.
    // Defaults to the main ['views'] key of the configuration.
    'views' => [get_stylesheet_directory(), get_template_directory()],

    // Options passed directly to Twig.
    'options' => [
        // 'cache' defaults to the main ['cache']['path'] key of the configuration.
        'cache' => 'wp-content/uploads/wpemerge/cache/twig',
    ],
]

您可以通过在 WP Emerge 配置数组中指定 twig 键来更改这些选项

\App::make()->bootstrap( [
    // ... other WP Emerge options
    'twig' => [
        // ... other WP Emerge Twig options
        'options' => [
            // ... other Twig options
            'cache' => false,
        ],
    ],
] );

有关支持哪些 Twig 选项的更多信息,请参阅 https://twig.symfony.ac.cn/doc/2.x/api.html

扩展 Twig

您可以使用以下方法通过自定义过滤器扩展 twig,例如

$myfilter = new Twig_Filter( 'myfilter', function( $string ) {
    return strtoupper( $string );
} );

// \App::resolve() used for brevity's sake - use a Service Provider instead.
$twig = \App::resolve( WPEMERGETWIG_VIEW_TWIG_VIEW_ENGINE_KEY );
$twig->environment()->addFilter( $myfilter );

通过这种方式,现在您已经拥有了您自己的自定义 Twig 过滤器

{{ 'hello world!' | myfilter }}

有关如何扩展 Twig 的更多信息,请参阅 https://twig.symfony.ac.cn/doc/2.x/advanced.html#creating-extensions