Zend框架-布局

布局表示多个视图的公共部分,例如页面页眉和页脚。默认情况下,布局应存储在view / layout文件夹中。

布局配置在module.config.php的view_manager部分下定义。

骨架应用程序的默认配置如下-

'view_manager' => array( 
   '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.phtml', 
      'application/index/index' => __DIR__ . '/../view/application/index/index.phtml', 
      'error/404' => __DIR__ . '/../view/error/404.phtml', 
      'error/index' => __DIR__ . '/../view/error/index.phtml', 
   ), 
   'template_path_stack' => array( 
   __DIR__ . '/../view', 
),

在这里,template_map用于指定布局。如果找不到布局,则将返回错误。让我们看一下骨架应用程序的主要布局。

Layout.phtml

<?= $this->doctype() ?>  
<html lang = "en"> 
   <head> 
      <meta charset = "utf-8"> 
      <?= $this->headTitle('ZF Skeleton Application')->setSeparator(' - ')>
         setAutoEscape(false) ?>
      <?= $this->headMeta() 
         ->appendName('viewport', 'width = device-width, initial-scale = 1.0') 
         ->appendHttpEquiv('X-UA-Compatible', 'IE = edge') 
      ?>  
      
      <!-- Le styles --> 
      <?= $this->headLink(['rel' => 'shortcut icon', 'type' => 
         'image/vnd.microsoft.icon', 
         'href' => $this->basePath() . '/img/favicon.ico']) 
         ->prependStylesheet($this->basePath('css/style.css')) 
         ->prependStylesheet($this->basePath('css/bootstraptheme.min.css')) 
         ->prependStylesheet($this->basePath('css/bootstrap.min.css')) 
      ?>  
      
      <!-- Scripts --> 
      <?= $this->headScript() 
         ->prependFile($this->basePath('js/bootstrap.min.js')) 
         ->prependFile($this->basePath('js/jquery-3.1.0.min.js')) 
      ?> 
   </head> 
   
   <body> 
      <nav class = "navbar navbar-inverse navbar-fixed-top" role = "navigation"> 
         <div class = "container"> 
            <div class = "navbar-header"> 
               <button type = "button" class = "navbar-toggle" data-
                  toggle = "collapse" data-target = ".navbar-collapse"> 
                  <span class = "icon-bar"></span> 
                  <span class = "icon-bar"></span> 
                  <span class = "icon-bar"></span> 
               </button> 
            
               <a class = "navbar-brand" href = "<?= $this->url('home') ?>"> 
                  <img src = "<?= $this->basePath('img/zf-logo-mark.svg') ?>
                     " height = "28" alt = "Zend Framework <?= \Application\Module::
                     VERSION ?>"/> Skeleton Application 
               </a> 
            </div>
         
            <div class = "collapse navbar-collapse"> 
               <ul class = "nav navbar-nav"> 
                  <li class = "active"><a href = "<?= 
                     $this->url('home') ?>">Home</a></li> 
               </ul> 
            </div> 
         </div> 
      </nav> 
   
      <div class = "container"> 
         <?= $this->content ?> 
         <hr> 
         <footer> 
            <p>© 2005 - <?= date('Y') ?> by Zend Technologies Ltd. 
               All rights reserved.</p> 
         </footer> 
      </div> 
      <?= $this->inlineScript() ?> 
   </body> 
</html>

当您分析布局时,它主要使用视图助手,我们在上一章中已经讨论过。当我们仔细观察时,布局使用特殊变量$ this-> content。此变量很重要,因为它将被实际请求页面的视图脚本(模板)代替。

创建一个新的布局

让我们为“教程”模块创建新的布局。

首先,让我们在“ public / css”目录下创建一个tutorial.css文件

 body { 
   background-color: lightblue; 
} 
h1 { 
   color: white; 
   text-align: center; 
}

在/ myapp / module / Tutorial / view / layout /中创建一个新的布局文件newlayout.phtml,然后从现有布局复制内容。然后,使用布局标题部分内的HeadLink帮助器类添加tutorial.css样式表。

<?php echo $this->headLink()->appendStylesheet('/css/tutorial.css');?> 

使用URL帮助器在导航部分中添加新的关于链接。

<li><a href = "<?= $this->url('tutorial', ['action' => 'about']) ?>">About</a></li>

该布局页面对于教程模块应用程序是通用的。更新教程模块配置文件的view_manager部分。

'view_manager' => array( 
   'template_map' => array( 
      'layout/layout' => __DIR__ . '/../view/layout/newlayout.phtml'), 
   'template_path_stack' => array('tutorial' => __DIR__ . '/../view',), 
)

TutorialController中添加aboutAction函数。

 public function aboutAction() { 
} 

在myapp / module / Tutorial / view / tutorial / tutorial /上添加about.phtml并包含以下内容。

<h2>About page</h2>

现在,您准备好最终运行该应用程序-http:// localhost:8080 / tutorial / about。

Zend框架-布局

作者:terry,如若转载,请注明出处:https://www.web176.com/zendframework/289.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2020年10月23日 下午1:53
下一篇 2020年10月23日 下午1:57

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注