stellarwp / templates
WordPress 模板库。
1.0.0
2024-03-18 15:08 UTC
Requires
- php: >=7.4
- stellarwp/arrays: ^1.2.1
Requires (Dev)
- lucatume/wp-browser: >=3.2.3 < 3.5.1
- stellarwp/coding-standards: ^2.0
- symfony/event-dispatcher-contracts: ^2.5.1
- symfony/string: ^5.4
- szepeviktor/phpstan-wordpress: ^1.1
README
一个库,用于在WordPress插件中包含模板,用户可以选择在主题或子主题的特定目录中覆盖这些模板。
安装
建议您通过 Composer 将模板作为项目依赖项安装。
composer require stellarwp/templates
实际上,我们建议使用 Strauss 将此库包含到您的项目中。
幸运的是,将Strauss添加到您的
composer.json
文件中,比添加一个典型的依赖项稍微复杂一点,所以请查看我们的 strauss 文档。
关于示例的说明
由于建议使用Strauss作为此库命名空间的限定词,所有示例都将使用 Boomshakalaka
命名空间限定词。
配置
在使用此库的功能之前,需要对其进行一些配置。配置是通过 Config
类完成的。
use Boomshakalaka\StellarWP\Templates\Config; add_action( 'plugins_loaded', function() { Config::set_hook_prefix( 'boom-shakalaka' ); Config::set_path( PATH_TO_YOUR_PROJECT_ROOT ); } );
配置好库后,扩展 Template 类来定义您插件中模板的位置。
使用类扩展,在类的 plugin_path
属性中手动定义模板的基本文件夹位置。以下是一个该类的示例
use Boomshakalaka\StellarWP\Templates\Template; class My_Custom_Template extends Template { /** * Defines the base path for the templates. * * @since 1.0.0 */ protected array $template_base_path = [ PATH_TO_YOUR_PROJECT_ROOT ]; }
完成这些后,您可以实例化类并定义一些其他设置
$template = new My_Custom_Template(); // Set the folder within your plugin where templates are stored. $template->set_template_folder( 'src/views/products' ); // Should users be able to override templates in their theme? $template->set_template_folder_lookup( true );
设置好模板类后,调用 $template->template('template-file')
将包含模板,并尝试在以下位置寻找它
- 子主题
../themes/child/boom-shakalaka/products/template-file
- 父主题
../themes/parent/boom-shakalaka/products/template-file
- 插件
../plugins/your-plugin/src/views/products/template-file