htmlburger/wpemerge-blade

在WP Emerge中启用Blade视图的使用。

0.17.0 2022-04-16 17:22 UTC

This package is auto-updated.

Last update: 2024-09-16 22:12:29 UTC


README

WP Emerge中启用Blade视图的使用。

摘要

快速入门

  1. 在你的主题目录中运行 composer require htmlburger/wpemerge-blade
  2. \WPEmergeBlade\View\ServiceProvider 添加到WP Emerge配置中的提供者数组
    \App::make()->bootstrap( [
        'providers' => [
            \WPEmergeBlade\View\ServiceProvider::class,
        ],
    ] );
  3. 如果你使用的是WP Emerge Starter Theme,你可以用theme/views-alternative/blade/内的视图替换你的主题视图。
  4. 如果你使用的是WP Emerge Starter Theme,你必须启用filter_core_templates配置选项,以便支持WordPress核心模板(index.phpsingle.php等)的.blade.php模板。

选项

默认选项

[
    // 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,

    // Filter core theme templates to search for .blade.php files.
    // This is only necessary in themes.
    'filter_core_templates' => false,

    // Options passed directly to Blade.
    'options' => [
        // 'views' defaults to the main ['views'] key of the configuration.
        'views' => [get_stylesheet_directory(), get_template_directory()],
        // 'cache' defaults to the main ['cache']['path'] key of the configuration.
        'cache' => 'wp-content/uploads/wpemerge/cache/blade',
    ],
]

你可以通过指定WP Emerge配置数组中的blade键来更改这些选项

\App::make()->bootstrap( [
    // ... other WP Emerge options
    'blade' => [
        // ... other WP Emerge Blade options
        'options' => [
            // ... other Blade options
            'cache' => get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'blade-cache',
        ],
    ],
] );

扩展Blade

你可以使用以下方式通过自定义指令扩展Blade,例如

// \App::resolve() used for brevity's sake - use a Service Provider instead.
$blade = \App::resolve( WPEMERGEBLADE_VIEW_BLADE_VIEW_ENGINE_KEY );
$blade->compiler()->directive( 'mydirective', function( $expression ) {
    return "<?php echo 'MyDirective: ' . $expression . '!'; ?>";
} );

有了这个,你现在就有了一个自定义的Blade指令

@mydirective('foobar')

有关如何扩展Blade的更多信息,请参阅https://laravel.net.cn/docs/5.4/blade#extending-blade

WooCommerce

为了使用Blade渲染WooCommerce模板,你不能使用WooCommerce模板的.blade.php扩展,因为它将无法检测到。相反,使用你的文件的常规.php扩展,例如

  • my-theme/woocommerce.php
  • my-theme/woocommerce/single-product.php
  • my-theme/woocommerce/archive-product.php

尽管这些文件是.php,这个扩展将使用Blade渲染它们。