2012-04-12 82 views
2

我在zend框架中使用布局。就好像现在看起来很丑很多很多代码一样。我想参考下面的代码知道。我有可能给它一个更清洁的方法。这是我的代码。优化Zend布局

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
     <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" /> 
     <title>Administrative Panel</title> 
     <link href="/css/main.css" rel="stylesheet" type="text/css" /> 
     <script type="text/javascript" src="/js/jquery.min.js"></script> 
     <script type="text/javascript" src="/js/plugins/spinner/ui.spinner.js"></script> 
     <script type="text/javascript" src="/js/plugins/spinner/jquery.mousewheel.js"></script> 
     <script type="text/javascript" src="/js/jquery-ui.min.js"></script> 
     <script type="text/javascript" src="/js/plugins/forms/uniform.js"></script> 
     <script type="text/javascript" src="/js/custom.js"></script> 
    </head> 
    <body> 
     <!-- Left side content --> 
     <div id="leftSide"> 
      <div class="logo"><a href="<?php echo $this->url(array(), 'admin-dashboard'); ?>"><img src="/images/logo.png" alt="" /></a></div> 
      <!-- Divider Decorator --> 
      <div class="sidebarSep mt0"></div> 
      <!-- Search widget --> 
      <form action="" class="sidebarSearch"> 
       <input type="text" name="search" placeholder="search…" id="ac" /> 
       <input type="submit" value="" /> 
      </form> 
      <!-- Divider Decorator --> 
      <div class="sidebarSep"></div> 
      <!-- Statistics --> 
      <div class="numStats"> 
       <ul> 
        <li><?php echo $this->itemCount; ?><span>Items</span></li> 
        <li><?php echo $this->categoryCount; ?><span>Categories</span></li> 
        <li class="last"><?php echo $this->userCount; ?><span>Users</span></li> 
       </ul> 
       <div class="clear"></div> 
      </div> 
      <!-- Divicer Decorator --> 
      <div class="sidebarSep"></div> 
      <!-- Sidebar buttons --> 
      <a href="#" title="" class="sButton sBlue"><img src="/images/icons/sPlus.png" alt="" /><span>Add new item</span></a> 
      <a href="#" title="" class="sButton sRed" style="margin-top: 12px;"><img src="/images/icons/sPlus.png" alt="" /><span>Add new user</span></a> 
      <!-- Divider Decorator --> 
      <div class="sidebarSep"></div> 
      <!-- Left navigation --> 
      <?php $this->navigation()->menu()->setPartial(array('partials/sidebar.phtml', 'admin')); echo $this->navigation()->menu()->render(); ?> 
     </div> 
     <!-- Right side --> 
     <div id="rightSide"> 
      <!-- Top fixed navigation --> 
      <?php echo $this->render('topnav.phtml') ?> 
      <!-- Title area --> 
      <div class="titleArea"></div> 
      <!-- Action Navigation --> 
      <?php echo $this->placeholder('action-navigation'); ?> 
      <!-- Main content wrapper --> 
      <div class="wrapper"> 
       <?php echo $this->layout()->content ?> 
      </div> 
      <!-- Footer line --> 
      <?php echo $this->render('footer.phtml') ?> 
     </div> 
     <div class="clear"></div> 
    </body> 
</html> 

我见过很多次了,大多数人都用一些辅助方法来清理布局文件。从自己的Zend框架

<!DOCTYPE html 
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <?php echo $this->headTitle() ?> 
    <?php echo $this->headScript() ?> 
    <?php echo $this->headStyle() ?> 
</head> 
<body> 
    <?php echo $this->render('header.phtml') ?> 

    <div id="nav"><?php echo $this->placeholder('nav') ?></div> 

    <div id="content"><?php echo $this->layout()->content ?></div> 

    <?php echo $this->render('footer.phtml') ?> 
</body> 
</html> 

的例子,他们使用了一些辅助方法,如

$this->docType() 
$this->headTitle() 
$this->headScript() 
$this->headStyle() 

这种方法看起来很多清洁我。因为他们在相同的文件中没有多说这些。我想知道那个辅助方法的确切目的是什么?以及如何在我的布局文件中使用它?

回答

4

您将在手册中找到关于查看帮助者和占位符的所有信息,尤其是有关占位符具体实现的this section

请参阅this section of the manual来创建您自己的视图助手。自定义视图帮助器基本上允许您在布局或任何需要此内容的视图中使用echo $this->myViewHelperName();来呈现HTML中的某些内容。

2

最有用的我用布局视图助手是为管理风格& JavaScript的资产助手:

$this->headScript() 
$this->headStyle() 

基本上这些助手让您管理您的样式表& javascript中的任何视图或布局资产电话:

$this->headLink()->prependStylesheet($this->baseUrl('/media/css/shared.css')); 

$this->headScript()->prependFile('https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.min.js'); 

如果您的应用程序中的每个模块都有特定的样式表,那么这非常有用。您可以从其他视图调用prependStylesheet/prependFile,并且布局中的最终回显调用将打印您加载的文件。

要打印出HTML head标签,请使用您在示例布局中具有相同的代码:

<?php echo $this->headScript() ?> 
<?php echo $this->headStyle() ?> 

注意有没有这么前置一个附加样式表的样式和功能的函数:

$this->headLink()->prependStylesheet($this->baseUrl('/media/css/shared.css')); 
$this->headLink()->appendStylesheet($this->baseUrl('/media/css/shared.css')); 

您加载样式表的顺序将决定资产的顺序。这是非常重要的,因为通常你需要在任何其他插件之前加载jquery javascript资源。

我同意,ZF的手册几乎没有提及这些帮助者的任何用法。 (http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.headstyle)