zend framework 2一大优点就是有很多module可供直接使用。
要添加smarty,直接到zf2网站上查找smarty模块。
http://modules.zendframework.com/ 会发现在 SmartyModule。
smarty的例子框架:https://github.com/MurgaNikolay/ZendSkeletonApplication。这个框架中Application模块使用了smarty。
使用方法:
INSTALLATION
- clone it
- add it to the modules-part in your application.config.php
- edit SmartyModule/config/module.config.php as you wish
不过你也可以自己修改SmartyModule/config/module.config.php的配置项,来讲其放到vendor下。
修改Application/config/module.config.php
'view_manager' => array(
'defaultSuffix' =>'tpl',
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.tpl',
//'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.tpl',
'error/index' => __DIR__ . '/../view/error/index.tpl',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
添加一个action。
<?php
namespace User\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use SmartyModule\View\Renderer\SmartyRenderer;
class IndexController extends AbstractActionController
{
public function indexAction()
{
$model = new ViewModel();
$model->hello = "a11";
return $model;
}
}
添加module\Application\view\layout\layout.tpl
{$this->doctype()}
<html lang="en">
<head>
<meta charset="utf-8">
{$this->headTitle()->setSeparator(' - ')->setAutoEscape(false)}
{$basePath = $this->basePath()}
{$this->headLink()->appendStylesheet("`$basePath`/css/bootstrap.min.css")
->appendStylesheet("`$basePath`/css/style.css")
->appendStylesheet("`$basePath`/css/bootstrap-responsive.min.css")}
{$this->headLink([ 'rel' => 'shortcut icon', 'type' => 'image/vnd.microsoft.icon', 'href' =>
"`$basePath`/images/favicon.ico"])}
{$this->headScript()->appendFile("`$basePath`/js/html5.js", "text/javascript", ['conditional' => 'lt IE9'])}
{$this->headTitle('ZF2 Skeleton Application')}
{$this->headMeta()}
<!-- Le styles -->
{$this->headLink()}
<!-- Scripts -->
{$this->headScript()}
</head>
<body>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="{$this->url('home')}">Skeleton Application</a>
<div class="nav-collapse">
<ul class="nav">
<li class="active"><a href="{$this->url('home')}">Home</a></li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
</div>
</div>
<div class="container">
{$this->content}
<hr>
<footer>
<p>© 2006 - 2012 by Zend Technologies Ltd. All rights reserved.</p>
</footer>
</div>
<!-- /container -->
</body>
</html>
添加module\Application\view\error\error.tpl
<h1>An error occurred</h1>
<h2>{$this->message}</h2>
{if $this->display_exceptions}
{if $this->exception}
<hr/>
<h2>Additional information:</h2>
<h3>{get_class($this->exception)}</h3>
<dl>
<dt>File:</dt>
<dd>
<pre class="prettyprint linenums">{$this->exception->getFile()}:{$this->exception->getLine()}</pre>
</dd>
<dt>Message:</dt>
<dd>
<pre class="prettyprint linenums">{$this->exception->getMessage()}</pre>
</dd>
<dt>Stack trace:</dt>
<dd>
<pre class="prettyprint linenums">{$this->exception->getTraceAsString()}</pre>
</dd>
</dl>
{$e = $this->exception->getPrevious()}
{if $e}
<hr/>
<h2>Previous exceptions:</h2>
<ul class="unstyled">
{while $e}
<li>
<h3>{get_class($e)}</h3>
<dl>
<dt>File:</dt>
<dd>
<pre class="prettyprint linenums">{$e->getFile()}:{$e->getLine()}</pre>
</dd>
<dt>Message:</dt>
<dd>
<pre class="prettyprint linenums">{$e->getMessage()}</pre>
</dd>
<dt>Stack trace:</dt>
<dd>
<pre class="prettyprint linenums">{$e->getTraceAsString()}</pre>
</dd>
</dl>
{$e = $e->getPrevious()}
{/while}
</li>
</ul>
{/if}
{else}
<h3>No Exception available</h3>
{/if}
{/if}
添加module\Application\view\application\index\index.tpl
<p>
{$this->hello}
</p>
this is index page
可以访问首页试试