wpsmith / templates
WordPress 模板。
Requires
- php: >=7.0.0
- wpsmith/singleton: *
This package is auto-updated.
Last update: 2024-09-29 05:27:52 UTC
README
一个类,可以复制到您的 WordPress 插件中,允许通过子主题 > 父主题 > 插件来加载模板部分。
这主要基于 Gary Jones 的 Gamajo_Template_Loader。Gary 的版本与这个版本的主要区别有两点
- 我不想为每个我创建的插件都扩展类...叫我懒惰。
- 我不想自动加载模板部分。
描述
Easy Digital Downloads、WooCommerce 和事件日历插件等,允许您向您的主题添加文件以覆盖插件自带的基本模板。作为开发者,在自己的插件中添加这种便利性可能会有些棘手。
WordPress 中的 get_template_part()
函数从未真正为插件而设计,因为它依赖于 locate_template()
,该函数只检查子主题和父主题。因此,为了添加一个最终回退,该回退使用插件中的模板,我们必须使用自定义的 locate_template()
函数和自定义的 get_template_part()
函数。这里的解决方案只是将它们作为一个类捆绑起来以方便使用。
安装
这不是一个独立的 WordPress 插件,所以常规说明不适用。相反
手动安装类
- 复制
Templates/src/TemplateLoader.php
用于基本使用
或
- 复制
Templates/src/TemplateLoaderData.php
和Templates/src/TemplateLoader.php
](TemplateLoader.php) 到您的插件中。它可以是插件根目录中的文件,或者更好的是includes
目录。
或
通过 Composer 安装类
- 告诉 Composer 将此类作为依赖项安装:
composer require wpsmith/templates
- 建议:安装 Mozart 包:
composer require coenjacobs/mozart --dev
并 配置它。 - 现在,已将类重命名为使用您自己的前缀,以防止与其他捆绑此类的插件发生冲突。
实现 & 使用
与 Gamajo 的 Gamajo_Template_Loader 不同,您无需实现新类。
假设...
// Set at the root folder of your plugin (e.g., wp-content/plugins/yourplugin/yourplugin.php). // Defines YOUR_PLUGIN_DIRNAME as yourplugin. define( 'YOUR_PLUGIN_DIRNAME', dirname( __FILE__ ) );
初始化
我们可以初始化 TemplateLoader
// Create the loader with my prefix and directory. // This assumes: // - plugin templates are found in the plugin folder: wp-content/plugins/yourplugin/templates/... // - theme templates are found in the plugin folder: wp-content/themes/yourtheme/templates/... $template_loader = new TemplateLoader( [ 'prefix' => 'wps', 'plugin_directory' => YOUR_PLUGIN_DIRNAME, ] );
或者,
// Create the loader with all the parts. // This declares: // - plugin templates are found in the plugin folder: wp-content/plugins/yourplugin/templates/... // - theme templates are found in the plugin folder: wp-content/themes/yourtheme/templates/yourplugin/... $template_loader = new TemplateLoader( [ 'prefix' => 'wps', 'theme_template_directory' => 'templates/yourplugin', 'templates_directory' => 'templates', 'plugin_directory' => WPS_PLUGIN_DIRNAME, ] );
您可以使用模板加载辅助程序,它将始终返回相同的模板加载器
/** * Gets the plugin template loader. * * @return \WPS\WP\Templates\TemplateLoader */ function yourprefix_get_template_loader() { static $loader; if ( $loader === null ) { $loader = new \WPS\WP\Templates\TemplateLoader( [ 'prefix' => 'wps', 'theme_template_directory' => 'templates/yourplugin', 'templates_directory' => 'templates', 'plugin_directory' => WPS_PLUGIN_DIRNAME, ) ); } return $loader; }
最后,您还可以用它进行配置。
/** * Gets a configuration file as a data array. * * @return array */ function yourprefix_get_config( $config ) { static $loader; if ( $loader === null ) { $loader = new TemplateLoader( [ 'filter_prefix' => 'wps', 'theme_template_directory' => 'config', 'templates_directory' => 'config', 'plugin_directory' => WPS_PLUGIN_DIRNAME, ] ); } $template = $loader->get_template_part( 'config', $config ); $data = array(); if ( is_readable( $template ) ) { $data = require $template; } return (array) $data; }
使用
-
使用它调用
load_template_part()
方法。这可以是在短代码回调中,或者您希望主题开发者包含在其文件中的某些内容。$template_loader->load_template_part( 'recipe' );
-
使用它调用
get_template_part()
方法。这可以是在短代码回调中,或者您希望主题开发者包含在其文件中的某些内容。// This will return the path to the particular template part. $template_loader->get_template_part( 'recipe' ); // This will load the particular template part. $template_loader->get_template_part( 'recipe', null, true );
-
如果您想向模板传递数据,请在调用
get_template_part()
之前使用数组调用set_template_data()
方法。set_template_data()
返回加载器对象,以便进行方法链接。$data = [ 'foo' => 'bar', 'baz' => 'boom' ]; $template_loader ->set_template_data( $data ); ->get_template_part( 'recipe' );
现在
bar
的值在配方模板内部作为$wps_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/ingredients.php
wp-content/themes/my-theme/meal-planner/recipe.php
- 插件模板
wp-content/plugins/meal-planner/templates/recipe-ingredients.php
wp-content/plugins/meal-planner/templates/ingredients.php
.wp-content/plugins/meal-planner/templates/recipe.php
.
- 主题模板
变更日志
查看变更日志。
许可协议
贡献
欢迎贡献 - 请在 develop
分支上进行分支、修复并发送拉取请求。
鸣谢
基于 Gary Jones 的 Gamajo_Template_Loader 构建,作者 Travis Smith
版权 2013-2020 Travis Smith