skillfish/zf3-smarty-module

Zend Framework 3 模块,用于将 Smarty 3 集成为视图管理器

dev-master 2021-12-09 18:42 UTC

This package is not auto-updated.

Last update: 2024-09-28 19:31:12 UTC


README

基于 https://github.com/randlem/zf2-smarty-module 并修改以适应 ZF3

安装

虽然您可以克隆此存储库,但强烈建议通过 composer 安装模块。

  • 运行 php composer.phar require skillfish/zf3-smarty-module
  • 将 Smarty 添加到您的 ZF3 模块配置中,例如 /config/modules.config.php
  • 在您的模块或应用程序配置中将视图管理器策略更改为 Smarty,如下所示
<?php
return [
  'view_manager' => [
    'strategies' => [
      'Smarty\View\Strategy'
    ],
  ],
];

您可能还希望将视图管理器的模板映射更改为实际使用 .tpl 文件而不是默认的 .phtml

<?php
return [
  'view_manager' => [
    'template_map' => [
      'layout/layout' => '/module/Application/view/layout/layout.tpl',
      'application/index/index' => '/module/Application/view/application/index/index.tpl',
      'error/404' => '/module/Application/view/error/404.tpl',
      'error/index' => '/module/Applicationview/error/index.tpl',
    ],
  ],
];

模板继承

为了使模板继承正常工作,您必须在控制器中终止您的 ViewModel 使用 $viewModel->setTerminal(true); 并使用 smarty 的 {extends} 标签。否则,ViewModel 将渲染默认布局模板,继承将不会工作。

示例

layout.tpl

<html>
<head>
  <title>{block 'title'}Page name{/block}</title>
</head>
<body>
  {block 'content'}{/block}
</body>
</html>

index.tpl

{extends 'layout.tpl'}
{block 'title' append} - Index{/block}
{block 'content'}This is the index template{/block}

控制器

public function indexAction()
{
    $viewModel = new ViewModel();
    $viewModel->setTerminal(true);
    return $viewModel;
}

将导致

<html>
<head>
  <title>Page name - Index</title>
</head>
<body>
  This is the index template
</body>
</html>

要求

composer 模块目前需要

"require": {
  "php":                                ">5.4",
  "smarty/smarty":                      "~3.1.29",
  "zendframework/zend-stdlib":          "~3.0.1",
  "zendframework/zend-mvc":             "~3.0.1",
  "zendframework/zend-servicemanager":  "~3.1",
  "zendframework/zend-modulemanager":   "~2.7.1"
},