mtymek/blast-view

使用Zend Framework 2的模板引擎(Zend\View)的独立模板渲染器

0.6.1 2015-08-13 12:35 UTC

This package is auto-updated.

Last update: 2024-09-05 03:54:49 UTC


README

使用Zend Framework 2的模板引擎(Zend\View)的独立模板渲染器 - Zend\View.

您可以使用它来在ZF2 MVC之外的环境中渲染PHTML文件,同时拥有所有Zend\View功能:嵌套模板、视图助手等。

示例用例

  • 从命令行应用程序生成HTML
  • 渲染HTML电子邮件
  • 在不使用其他Zend Framework组件的情况下使用Zend\View

Build Status Coverage Status

安装

  1. 使用Composer安装包

    $ composer require mtymek/blast-view ~0.5
  2. Blast\View添加到系统配置中的模块列表(config/application.config.php文件)。

配置

Blast\View使用与常规Zend应用程序类似的配置键。目前您可以配置两个选项

  • 视图模板的位置(template_path_stack键)
  • 默认布局(layout_template键)
return [
    'view_manager' => [
        'layout_template' => 'layout/layout.phtml',
        'template_path_stack' => [
            __DIR__ . '/../view',
        ],
    ],
];

使用方法

一旦安装,Blast\View模块将在应用程序的主要ServiceManager中注册新的服务:Blast\View\View。它可以从那里轻松提取出来以在ZF2 MVC之外渲染模板。

use Zend\View\Model\ViewModel;
use Blast\View\View;

$viewModel = new ViewModel(
    [
        'name' => 'Mat'
    ]
);
$viewModel->setTemplate('index.phtml');

$view = $serviceManager->get(View::class);
echo $view->render($viewModel);

布局

布局只是一个包装您主要视图脚本的父级ViewModel。它应该在渲染主要模板之前使用setLayout()方法注入。

$layout = new ViewModel();
$layout->setTemplate('layout/layout.phtml');
$view->setLayout($layout);

$viewModel = new ViewModel();
$viewModel->setTemplate('index.phtml');
echo $view->render($viewModel);

布局模板的结构与ZF2相同

<html>
<head>
</head>
<body>
    <?php echo $this->content ?>
</body>
</html>

视图助手

Blast\View支持通过view_helpers配置键自定义视图助手,方式与ZF2相同。以下是一个示例配置,它注册了foo视图助手

return [
    'view_helpers' => [
        'invokables' => [
            'foo' => FooHelper::class,
        ]
    ],
];