divix1988/laminas-smarty-module

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

1.0.1 2020-02-29 23:24 UTC

This package is auto-updated.

Last update: 2024-09-29 05:42:38 UTC


README

基于 https://github.com/skillfish/zf3-smarty-module 并修改以支持 Laminas

安装

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

  • 运行 php composer.phar require divix1988/laminas-smarty-module
  • 将 Smarty 添加到您的 Laminas 模块配置中,例如 /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":                                "^7.1",
  "smarty/smarty":                      "^3.1",
  "laminas/laminas-stdlib":             "^3.2",
  "laminas/laminas-mvc":                "^3.1",
  "laminas/laminas-servicemanager":     "^3.4",
  "laminas/laminas-modulemanager":      "^2.8"
},