italystrap / view
View API for loading template parts and views the OOP way
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-10-18 22:01:57 UTC
README
PHP Sanitizer and Validation OOP way
Table Of Contents
Installation
The best way to use this package is through Composer:
composer require italystrap/view
Basic Usage
$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 );
For WordPress User
By default it will search in child -> parent -> theme-compat directories like the original \get_template_part()
does
// 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 );
If you need to add more or different direcories for searchinf files you can filter theme with the 'italystrap_view_get_template_part_directories'
hook.
\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; });
Some example of results:
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
And so on.
Inside the file template
Inside the file or template part you require if you added some $data value you can use it like so:
$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' );
Advanced Usage
See in Tests Foldes
Contributing
All feedback / bug reports / pull requests are welcome.
License
Copyright (c) 2019 Enea Overclokk, ItalyStrap
This code is licensed under the MIT.
Credits
For the Closure in the View and for some ideas with the Symphony Finder