babdev / renderer
dev-master / 2.x-dev
2019-01-01 21:31 UTC
Requires
- php: ~7.0
Requires (Dev)
- illuminate/events: 5.5.*|5.7.*
- illuminate/filesystem: 5.5.*|5.7.*
- illuminate/view: 5.5.*|5.7.*
- joomla/coding-standards: ~2.0@alpha
- league/plates: ~3.0
- mustache/mustache: ~2.3
- phpunit/phpunit: ~6.3
- symfony/templating: ~3.4|~4.0
- twig/twig: ~1.14|~2.0
Suggests
- illuminate/view: Install ~5.1 if you are using Laravel's Blade template engine.
- league/plates: Install ~3.0 if you are using the Plates template engine.
- mustache/mustache: Install ~2.3 if you are using the Mustache template engine.
- symfony/templating: Install ~2.7|~3.0|~4.0 if you are using Symfony's PHP template component.
- twig/twig: Install ~1.14|~2.0 if you are using the Twig template engine.
This package is not auto-updated.
Last update: 2019-02-20 19:29:02 UTC
README
接口
Renderer\RendererInterface
Renderer\RendererInterface
是一个接口,用于提供通用的渲染API。
类
Renderer\MustacheRenderer
Renderer\PhpEngineRenderer
Renderer\PlatesRenderer
Renderer\TwigRenderer
除了PlatesRenderer
之外的所有类都扩展了父渲染引擎类,以使这些引擎能够实现RendererInterface
。
用法
Renderer\RendererInterface
类可以像它们的父实现一样实例化,有关更详细的信息,请参阅供应商文档。
为了帮助使用这些类,此存储库的samples文件夹中提供了示例服务提供者,以及这些提供者所依赖的示例配置。
示例用例
这里提供了一个使用Renderer\RendererInterface
的示例。在这个例子中,我们的控制器类基于Joomla\View\ViewInterface
构建了一个视图类,并注入了所需的依赖项。
示例控制器
namespace Joomla\Controller; use Joomla\Renderer\RendererInterface; use Joomla\Controller\AbstractController; use Joomla\DI\ContainerAwareInterface; use Joomla\DI\ContainerAwareTrait; use Joomla\View\ViewInterface; /** * Default controller class for the application * * @since 1.0 */ class DefaultController extends AbstractController implements ContainerAwareInterface { use ContainerAwareTrait; /** * The default view for the application * * @var string * @since 1.0 */ protected $defaultView = 'dashboard'; /** * State object to inject into the model * * @var \Joomla\Registry\Registry * @since 1.0 */ protected $modelState = null; /** * Execute the controller * * This is a generic method to execute and render a view and is not suitable for tasks * * @return boolean True if controller finished execution * * @since 1.0 * @throws \RuntimeException */ public function execute() { try { // Initialize the view object $view = $this->initializeView(); // Render our view. $this->getApplication()->setBody($view->render()); return true; } catch (\Exception $e) { throw new \RuntimeException(sprintf('Error: ' . $e->getMessage()), $e->getCode()); } } /** * Method to initialize the model object * * @return \Joomla\Model\ModelInterface * * @since 1.0 * @throws \RuntimeException */ protected function initializeModel() { $model = '\\Joomla\\Model\\' . ucfirst($this->getInput()->getWord('view')) . 'Model'; // If a model doesn't exist for our view, revert to the default model if (!class_exists($model)) { $model = '\\Joomla\\Model\\DefaultModel'; // If there still isn't a class, panic. if (!class_exists($model)) { throw new \RuntimeException(sprintf('No model found for view %s', $vName), 500); } } return new $model($this->modelState); } /** * Method to initialize the renderer object * * @return RendererInterface Renderer object * * @since 1.0 * @throws \RuntimeException */ protected function initializeRenderer() { $type = $this->getContainer()->get('config')->get('template.renderer'); // Set the class name for the renderer's service provider $class = '\\Joomla\\Service\\' . ucfirst($type) . 'RendererProvider'; // Sanity check if (!class_exists($class)) { throw new \RuntimeException(sprintf('Renderer provider for renderer type %s not found.', ucfirst($type))); } // Add the provider to the DI container $this->getContainer()->registerServiceProvider(new $class); return $this->getContainer()->get('renderer'); } /** * Method to initialize the view object * * @return ViewInterface View object * * @since 1.0 * @throws \RuntimeException */ protected function initializeView() { // Initialize the model object $model = $this->initializeModel(); $view = ucfirst($this->getInput()->getWord('view', $this->defaultView)); $class = '\\Joomla\\View\\' . $view . 'HtmlView'; // Ensure the class exists, fall back to default otherwise if (!class_exists($class)) { $class = '\\Joomla\\View\\DefaultHtmlView'; // If we still have nothing, abort mission if (!class_exists($class)) { throw new \RuntimeException(sprintf('A view class was not found for the %s view.', $view)); } } // HTML views require a renderer object too, fetch it $renderer = $this->initializeRenderer(); // Instantiate the view now /* @type \Joomla\View\AbstractHtmlView $object */ $object = new $class($model, $renderer); // We need to set the layout too $object->setLayout(strtolower($view) . '.' . strtolower($this->getInput()->getWord('layout', 'index'))); return $object; } }
此示例中的视图类是从以下示例类扩展的,该示例类扩展了\Joomla\View\AbstractView
namespace Joomla\View; use Joomla\Renderer\RendererInterface; use Joomla\Model\ModelInterface; use Joomla\View\AbstractView; /** * Abstract HTML View class * * @since 1.0 */ abstract class AbstractHtmlView extends AbstractView { /** * The data array to pass to the renderer engine * * @var array * @since 1.0 */ private $data = array(); /** * The name of the layout to render * * @var string * @since 1.0 */ private $layout; /** * The renderer object * * @var RendererInterface * @since 1.0 */ private $renderer; /** * Class constructor * * @param ModelInterface $model The model object. * @param RendererInterface $renderer The renderer object. * * @since 1.0 */ public function __construct(ModelInterface $model, RendererInterface $renderer) { parent::__construct($model); $this->setRenderer($renderer); } /** * Retrieves the data array * * @return array * * @since 1.0 */ public function getData() { return $this->data; } /** * Retrieves the layout name * * @return string * * @since 1.0 * @throws \RuntimeException */ public function getLayout() { if (is_null($this->layout)) { throw new \RuntimeException('The layout name is not set.'); } return $this->layout; } /** * Retrieves the renderer object * * @return RendererInterface * * @since 1.0 */ public function getRenderer() { return $this->renderer; } /** * Method to render the view. * * @return string The rendered view. * * @since 1.0 * @throws \RuntimeException */ public function render() { return $this->getRenderer()->render($this->getLayout(), $this->getData()); } /** * Sets the data array * * @param array $data The data array. * * @return $this Method allows chaining * * @since 1.0 */ public function setData(array $data) { $this->data = $data; return $this; } /** * Sets the layout name * * @param string $layout The layout name. * * @return $this Method allows chaining * * @since 1.0 */ public function setLayout($layout) { $this->layout = $layout; return $this; } /** * Sets the renderer object * * @param RendererInterface $renderer The renderer object. * * @return $this Method allows chaining * * @since 1.0 */ public function setRenderer(RendererInterface $renderer) { $this->renderer = $renderer; return $this; } }
根据上述示例控制器建立的视图类
namespace Joomla\View; use Joomla\Model\DefaultModel; /** * HTML view class for the Dashboard * * @since 1.0 */ class DashboardHtmlView extends AbstractHtmlView { /** * Redeclared model object for proper typehinting * * @var DefaultModel * @since 1.0 */ protected $model; /** * Method to render the view. * * @return string The rendered view. * * @since 1.0 */ public function render() { $this->setData(['data' => $this->model->getData()]); return parent::render(); } }
通过Composer安装
您可以直接在命令行运行以下命令
composer require joomla/renderer