zend framework2添加smarty模块

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安装到module目录下,与Application平行,而不是放到vendor目录下。

不过你也可以自己修改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

可以访问首页试试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值