gamajo/template-loader

一个用于WordPress插件的类,允许通过子主题 > 父主题 > 插件的方式加载模板片段

1.3.1 2018-09-24 10:23 UTC

This package is auto-updated.

Last update: 2024-09-13 00:57:50 UTC


README

Code Climate

一个类,可以复制到您的WordPress插件中,以允许通过子主题 > 父主题 > 插件的方式加载模板片段。

描述

Easy Digital Downloads、WooCommerce和事件日历插件等其他插件允许您将文件添加到主题中,以覆盖插件自带的默认模板。作为开发者,在自己的插件中添加这种便利性可能有点棘手。

WordPress中的get_template_part()函数从未真正为插件而设计,因为它依赖于locate_template(),它只检查子主题和父主题。因此,要添加一个最终回退,使用插件中的模板,我们必须使用自定义的locate_template()函数和自定义的get_template_part()函数。这里的解决方案只是将它们封装为一个类,以方便使用。

安装

这本身不是一个WordPress插件,所以常规说明不适用。取而代之的是

手动安装类

  1. class-gamajo-template-loader.php 复制到您的插件中。可以放在插件根目录下的一个文件中,或者更好的是,一个includes目录中。

或者

通过Composer安装类

  1. 告诉Composer将此类作为一个依赖项安装:composer require gamajo/template-loader
  2. 推荐:安装Mozart包:composer require coenjacobs/mozart --dev配置它
  3. 现在将类重命名为使用您自己的前缀,以防止与其他捆绑此类的插件发生冲突。

实现类

  1. 在相同目录下创建一个新文件,例如class-your-plugin-template-loader.php
  2. 在该文件中创建一个扩展Gamajo_Template_Loader(或通过Composer/Mozart安装的新前缀名称)的类。如果您需要,以下Meal Planner Template Loader示例类可以作为起点。
  3. 覆盖类属性以适应您的插件。您还可以根据需要覆盖get_templates_dir()方法。
  4. 现在您可以实例化您自定义的模板加载类,并使用它来调用get_template_part()方法。这可以在短代码回调中完成,或者您希望主题开发者将其包含在其文件中的任何内容。
// Template loader instantiated elsewhere, such as the main plugin file.
$meal_planner_template_loader = new Meal_Planner_Template_Loader();
  • 使用它来调用get_template_part()方法。这可以在短代码回调中完成,或者您希望主题开发者将其包含在其文件中的任何内容。

    $meal_planner_template_loader->get_template_part( 'recipe' );
  • 如果您想向模板传递数据,请在调用get_template_part()之前使用数组调用set_template_data()方法。set_template_data()返回加载器对象,以便进行方法链。

    $data = array( 'foo' => 'bar', 'baz' => 'boom' );
    $meal_planner_template_loader
        ->set_template_data( $data )
        ->get_template_part( 'recipe' );

    bar的值现在作为$data->foo在配方模板内部可用。

    如果您想使用不同的变量名,请向set_template_data()添加第二个参数

    $data = array( 'foo' => 'bar', 'baz' => 'boom' );
    $meal_planner_template_loader
        ->set_template_data( $data, 'context' )
        ->get_template_part( 'recipe', 'ingredients' );

    bar的值现在作为$context->foo在配方模板内部可用。

    这将尝试加载wp-content/themes/my-theme/meal-planner/recipe-ingredients.php,或wp-content/themes/my-theme/meal-planner/recipe.php,然后回退到wp-content/plugins/meal-planner/templates/recipe-ingredients.phpwp-content/plugins/meal-planner/templates/recipe.php

  • 您还可以将模板加载器对象传递给模板。

    $template_loader = new Your_Template_Loader();
    $template_loader->set_template_data(
        array(
      	      'template_loader' => $template_loader,
      		  // Optional other data as needed.
          )
    );

    然后在模板中,您可以使用

    $data->template_loader->get_template_part( 'recipe' );

Meal Planner 示例类

<?php
/**
 * Meal Planner
 *
 * @package   Meal_Planner
 * @author    Gary Jones
 * @link      http://example.com/meal-planner
 * @copyright 2013 Gary Jones
 * @license   GPL-2.0+
 */

if ( ! class_exists( 'Gamajo_Template_Loader' ) ) {
  require plugin_dir_path( __FILE__ ) . 'class-gamajo-template-loader.php';
}

/**
 * Template loader for Meal Planner.
 *
 * Only need to specify class properties here.
 *
 * @package Meal_Planner
 * @author  Gary Jones
 */
class Meal_Planner_Template_Loader extends Gamajo_Template_Loader {
  /**
   * Prefix for filter names.
   *
   * @since 1.0.0
   *
   * @var string
   */
  protected $filter_prefix = 'meal_planner';

  /**
   * Directory name where custom templates for this plugin should be found in the theme.
   *
   * @since 1.0.0
   *
   * @var string
   */
  protected $theme_template_directory = 'meal-planner';

  /**
   * Reference to the root directory path of this plugin.
   *
   * Can either be a defined constant, or a relative reference from where the subclass lives.
   *
   * In this case, `MEAL_PLANNER_PLUGIN_DIR` would be defined in the root plugin file as:
   *
   * ~~~
   * define( 'MEAL_PLANNER_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
   * ~~~
   *
   * @since 1.0.0
   *
   * @var string
   */
  protected $plugin_directory = MEAL_PLANNER_PLUGIN_DIR;

  /**
   * Directory name where templates are found in this plugin.
   *
   * Can either be a defined constant, or a relative reference from where the subclass lives.
   *
   * e.g. 'templates' or 'includes/templates', etc.
   *
   * @since 1.1.0
   *
   * @var string
   */
  protected $plugin_template_directory = 'templates';
}

使用示例

来自AudioThemeCue插件使用了此类。从https://github.com/AudioTheme/cue/tree/develop/includes开始,它在供应商目录中包含了这个类,然后在class-cue-template-loader.php文件中包含了我类的所需子类,并设置了一些基本属性。它还有一个模板在https://github.com/AudioTheme/cue/tree/develop/templates

如果您想要播放列表在您的主题中有不同的标记,可以将templates/playlist.php复制到wp-content/themes/{your-active-theme}/cue/playlist.php,并进行您想要的任何更改。WordPress将首先查找该文件,然后检查父主题位置(如果您的活动主题是子主题),最后回退到Cue插件附带默认模板。

变更日志

请参阅变更日志

许可证

GPL 2.0或更高版本.

贡献

欢迎贡献 - 请在develop分支上fork、修复并发送pull请求。

鸣谢

Gary Jones构建
版权所有 2013 Gary Jones