brightnucleus / view
可重复使用的视图组件,可以提供不同的实现。
Requires
- brightnucleus/config: >=0.4
- brightnucleus/exceptions: >=0.2
- doctrine/collections: ^1.3
- symfony/finder: ^3.1
Requires (Dev)
- phpunit/phpunit: ~5
- squizlabs/php_codesniffer: ~2.5
README
这是一个可重复使用的视图组件,可以提供不同的实现(在单独的、可选的包中)。
目录
安装
使用此组件的最佳方式是通过Composer
composer require brightnucleus/view
基本用法
使用View
组件的最简单方式是通过其外观:BrightNucleus\Views
。
此外观利用了基本的BaseView
视图实现以及基本的PHPEngine
引擎实现。它可以直接使用,无需额外配置。
添加位置
您可以通过静态方法View::addLocation($location)
添加位置。每个位置都需要实现Location
。View
组件自带一个位置提供程序:FilesystemLocation
。
以下是如何将一组文件夹作为新位置添加的方法
<?php namespace View\Example; use View\Example\App; use BrightNucleus\View; use BrightNucleus\View\Location\FilesystemLocation; $folders = [ App::ROOT_FOLDER . '/child-theme/templates', App::ROOT_FOLDER . '/parent-theme/templates', App::ROOT_FOLDER . '/plugin/templates', ]; foreach ($folders as $folder) { Views::addLocation(new FilesystemLocation($folder)); }
渲染视图
要渲染一个视图,您需要将视图标识符传递给静态方法View::render($view, $context, $type)
。
$view
是一个视图标识符,通常是模板文件名,但也可以根据您配置的位置和引擎是其他内容。
您传入的$context
数组将在渲染模板时提取出来。
$type
参数允许您注入特定的视图/引擎组合,而不是让View
组件自行查找。
注意:为了有效地渲染视图,它需要在已注册的位置之一中找到。
以下是如何渲染一个简单视图的方法
<?php namespace View\Example; use View\Example\User; use BrightNucleus\Views; echo Views::render('welcome-user', [ 'userId' => User::getCurrentId() ]);
上下文
在正在渲染的模板内部,上下文变量可以作为属性使用。
例如,对于上面渲染的视图,您可以在模板中使用echo $this->userId;
来检索特定的上下文数据。
如果您将可调用的对象添加到上下文中作为属性,它们将作为视图模板中的方法实际起作用。
整个上下文都可以通过方法$this->getContext()
获取,该方法将返回一个关联数组。
请注意,没有进行自动转义,上下文数据的值是直接传递的。
部分
要从正在渲染的模板中渲染不同的模板作为部分,您可以使用方法$this->section($view, $context, $type)
。
这基本上与对View
对象的外部render()
调用的相同,但有以下区别
- 它重用了父级的
ViewBuilder
,具有相同的渲染引擎和相同的位置。 - 如果您提供额外的上下文,它将与父级的上下文合并。
以下是一个如何工作的示例
<?php namespace View\Example; // This is our template that is being rendered. ?><h1>Welcome screen for User with ID <?= $this->userId ?></h1> <p>This is an example template to show the rendering of partials.</p> <hr> <?= $this->section('user-notifications') ?> <hr> <?= $this->section('user-dashboard') ?>
高级用法
对于更高级的用例,您可能需要为您的视图或引擎提供自定义类。
实例化自定义ViewBuilder
为此,您需要手动创建您的ViewBuilder
对象,而不是依赖于Views
外观。通过手动实例化它,您可以提供自定义配置来映射您的类。
一旦您有了ViewBuilder
实例,您可以使用addLocation($location)
方法添加要扫描视图的位置,并使用create($view, $type)
方法创建实际的视图。然后可以通过其render($context)
方法渲染此视图。
<?php namespace View\Example; use BrightNucleus\Config\ConfigFactory; use BrightNucleus\View\ViewBuilder; use BrightNucleus\View\Location\FilesystemLocation; // Fetch the Config from somewhere. $config = ConfigFactory::create(__DIR__. '/config/views.php'); // Create a new instance of the ViewBuilder and add a location. $viewBuilder = new ViewBuilder( $config ); $viewBuilder->addLocation(new FilesystemLocation(__DIR__ . '/views')); // Create a new instance of a specific View. $view = $viewBuilder->create('my-view'); // Render the view. echo $view->render(['answer' => 42]);
配置模式
以下是一个提供自定义配置的示例。在这种情况下,我们想要用更酷的类替换默认类。
<?php namespace View\Example; use BrightNucleus\View\Engine\EngineFinder; use BrightNucleus\View\View\ViewFinder; $engineFinder = [ EngineFinder::CLASS_NAME_KEY => AwesomeEngineFinder::class, EngineFinder::ENGINES_KEY => [ 'AwesomeEngine' => AwesomeEngine::class, ], EngineFinder::NULL_OBJECT => AwesomeNullEngine::class, ]; $viewFinder = [ ViewFinder::CLASS_NAME_KEY => AwesomeViewFinder::class, ViewFinder::VIEWS_KEY => [ 'AwesomeView' => AwesomeView::class, ], ViewFinder::NULL_OBJECT => AwesomeNullView::class, ]; return [ 'BrightNucleus' => [ 'View' => [ 'EngineFinder' => $engineFinder, 'ViewFinder' => $viewFinder, ], ], ];
当然,您不需要覆盖所有类、视图或引擎。如果您只覆盖特定的键,其余部分将采用默认值。
贡献
欢迎所有反馈/错误报告/拉取请求。
许可
版权所有(c)2016-2017 Alain Schlesser,Bright Nucleus
本代码遵循MIT许可证。