Based on https://github.com/skillfish/zf3-smarty-module and modified for use with Laminas
Although you could just clone this repository, it is highly recommended to install the module via composer.
- run
php composer.phar require divix1988/laminas-smarty-module
- add Smarty to your Laminas modules Configuration e.g /config/modules.config.php
- change the View Manager Strategy to Smarty in your module or apllication config like:
<?php
return [
'view_manager' => [
'strategies' => [
'Smarty\View\Strategy'
],
],
];
you might also want to change your View Manager's Template Map to actually use .tpl files instead of the default .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',
],
],
];
In order for template inheritance to work, you must terminate your ViewModel inside your Controller with $viewModel->setTerminal(true);
and make use of the smarty {extends}
tag. Otherwise the ViewModel will render the default layout template and inheritance won't work.
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}
Controller
public function indexAction()
{
$viewModel = new ViewModel();
$viewModel->setTerminal(true);
return $viewModel;
}
will result in
<html>
<head>
<title>Page name - Index</title>
</head>
<body>
This is the index template
</body>
</html>
The composer module currently requires:
"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"
},