italystrap / view
以面向对象的方式查看加载模板部分和视图的API
1.1.2
2023-01-18 18:05 UTC
Requires
- php: >=7.2
- italystrap/config: ^2.1
- italystrap/finder: dev-master
Requires (Dev)
- brianium/paratest: ^4.0
- codeception/c3: 2.*
- codeception/module-asserts: ^1.0
- codeception/module-cli: ^1.0
- codeception/module-db: ^1.0
- codeception/module-filesystem: ^1.0
- codeception/module-phpbrowser: ^1.0
- codeception/module-webdriver: ^1.0
- codeception/util-universalframework: ^1.0
- dealerdirect/phpcodesniffer-composer-installer: ^0.5.0
- infection/infection: ^0.15.3
- italystrap/debug: ^2.0
- lucatume/function-mocker-le: ^1.0
- lucatume/wp-browser: ^2.4
- nunomaduro/phpinsights: ^1.14.0
- phpbench/phpbench: @dev
- phpcompatibility/php-compatibility: *
- phpmetrics/phpmetrics: ^2.5
- phpstan/phpstan: ^0.11.16
- squizlabs/php_codesniffer: *
- szepeviktor/phpstan-wordpress: ^0.3.0
- vimeo/psalm: ^3.9
This package is auto-updated.
Last update: 2024-09-18 21:51:59 UTC
README
PHP 消毒和验证面向对象方式
目录
安装
使用此包的最佳方式是通过Composer
composer require italystrap/view
基本用法
$finder = new \ItalyStrap\View\ViewFinder(); $finder->in( 'full/path/to/the/views/' ); //or $finder->in( ['full/path/to/the/views/','full/path/to/the/views/'] ); $view = new \ItalyStrap\View\View( $finder ); $view->render( 'slug', $data ); // Data could be the type of: string|int|array|object // Or $view->render( ['slug'], $data ); // Or $view->render( ['slug', 'name'], $data ); // Or $view->render( ['slug', 'name', 'subName'], $data );
对于WordPress用户
默认情况下,它将在子目录 -> 父目录 -> theme-compat 目录中搜索,就像原始的 \get_template_part()
一样
// It will search in the root of your theme slug-name.php -> slug.php \ItalyStrap\View\get_template_part( 'slug', 'name', $data ); // Or use ItalyStrap\View; // theme_path/slug-name.php // theme_path/slug.php get_template_part( 'slug', 'name', $data ); // theme_path/slug-slug1-name.php // theme_path/slug-name.php // theme_path/slug.php get_template_part( ['slug', 'slug1'], 'name', $data );
如果您需要添加更多或不同的目录以搜索文件,可以使用 'italystrap_view_get_template_part_directories'
钩子过滤主题。
\add_filter( 'italystrap_view_get_template_part_directories', function( array $dirs ) { // Add here your logic for dirs // For example you can add subdirs or remove dirs // You can add directories for languages // You can add directories from plugins and so on. // The sky is the limit. return $dirs; });
一些示例结果
plugin_path/some_dir_path/slug-name.php plugin_path/some_dir_path/slug.php theme_path/other_dir_path/slug-slug-name.php theme_path/other_dir_path/slug-name.php theme_path/other_dir_path/slug.php
theme_with_locale_path/locale/slug-name.php
等等。
在模板文件内部
在文件或模板部分内部,如果您添加了一些 $data 值,您可以使用它如下
$data = [ 'title' => 'Ciao Mondo', ]; // some-template-part.php use ItalyStrap\View; get_template_part( ['some','template'], 'part', $data ); //or //... //$view->render( ['some','template','part'], $data ); // inside some-template-part.php echo $this->title; //or echo $this->get( 'title', 'Some default title' );
高级用法
参见 测试文件夹
贡献
欢迎所有反馈/错误报告/拉取请求。
许可
版权 (c) 2019 Enea Overclokk, ItalyStrap
此代码根据 MIT 许可。
致谢
对于视图中的闭包以及一些关于 Symphony Finder 的想法