7.8 添加模板文件

最后更新于:2022-04-01 00:41:31

模板是数据处理后的最终展现平台,也是用户操作与感知的入口。Album模块使用的模板有4个:index.phtml , add.phtml , edit.phtml , delete.phtml , paginator.phtml 分别 列表、添加、修改、删除、分页导航 ### 7.8.1 列表模板 index.phtml 添加文件:/module/Album/view/album/album/index.phtml,内容如下: ~~~ <table> <tr> <th><?php echo $this->translate("Title") ?></th> <th><?php echo $this->translate("Artist") ?></th> <th><a href="<?php echo $this->url('album', array('action' => 'add')); ?>"><?php echo $this->translate("Add new album") ?></a></th> </tr> <?php foreach ($paginator as $album) : ?> <tr> <td><?php echo $this->escapeHtml($album->title); ?></td> <td><?php echo $this->escapeHtml($album->artist); ?></td> <td> <a href="<?php echo $this->url('album', array('action' => 'edit', 'id' => $album->id)); ?>"><?php echo $this->translate("Edit") ?></a> <a href="<?php echo $this->url('album', array('action' => 'delete', 'id' => $album->id)); ?>"><?php echo $this->translate("Delete") ?></a> </td> </tr> <?php endforeach; ?> </table> <?php echo $this->paginationControl($this->paginator,'sliding',array('partial/paginator.phtml','Album'),array('route'=>'album')); ?> ~~~ ### 7.8.2 列表模板 add.phtml 添加文件:/module/Album/view/album/album/add.phtml,内容如下: ~~~ <?php $form = $this->form; $form->setAttribute('action',$this->url('album',array('action'=>'add'))); echo $this->form()->openTag($form); echo $this->formCollection($this->form); echo $this->form()->closeTag(); ?> ~~~ ### 7.8.3 列表模板 edit.phtml 添加文件:/module/Album/view/album/album/edit.phtml,内容如下: ~~~ <?php $form = $this->form; $form->setAttribute('action',$this->url('album',array('action'=>'edit','id'=>$this->id))); echo $this->form()->openTag($form); echo $this->formCollection($this->form); echo $this->form()->closeTag(); ?> ~~~ ### 7.8.4 列表模板 delete.phtml 添加文件:/module/Album/view/album/album/delete.phtml,内容如下: ~~~ <?php $title = 'Delete album'; $this->headTitle($title); ?> <h1><?php echo $this->escapeHtml($title); ?></h1> <p>Are you sure that you want to delete '<?php echo $this->escapeHtml($album->title); ?>' by '<?php echo $this->escapeHtml($album->artist); ?>'? </p> <?php $url = $this->url('album', array( 'action' => 'delete', 'id' => $this->id, )); ?> <form action="<?php echo $url; ?>" method="post"> <div> <input type="hidden" name="id" value="<?php echo (int) $album->id; ?>" /> <input type="submit" name="del" value="Yes" /> <input type="submit" name="del" value="No" /> </div> </form> ~~~ ### 7.8.5 列表模板 paginator.phtml 添加文件:/module/Album/view/partial/paginator.phtml,内容如下: ~~~ <?php if ($this->pageCount): ?> <div class="pagination pagination-centered"> <ul> <!-- Previous page link --> <?php if (isset($this->previous)): ?> <li> <a href="<?php echo $this->url($this->route); ?>?page=<?php echo $this->previous;?>"><<</a> </li> <?php else: ?> <li class="disabled"> <a href="#"> << </a> </li> <?php endif; ?> <!-- Numbered page links --> <?php foreach ($this->pagesInRange as $page): ?> <?php if ($page != $this->current): ?> <li> <a href="<?php echo $this->url($this->route); ?>?page=<?php echo $page; ?>"> <?php echo $page; ?> </a> </li> <?php else: ?> <li class="active"> <a href="#"><?php echo $page; ?></a> </li> <?php endif; ?> <?php endforeach; ?> <!-- Next page link --> <?php if (isset($this->next)): ?> <li> <a href="<?php echo $this->url($this->route); ?>?page=<?php echo $this->next; ?>"> >> </a> </li> <?php else: ?> <li class="disabled"> <a href="#"> >> </a> </li> <?php endif; ?> </ul> </div> <?php endif; ?> ~~~ 经过以上的准备工作,接下可以通过 http://localhost/album 来打开album 列表的,打开的页面结果如下所示: ~~~ header Title Artist Add new album First album artist01 EditDelete Second album artist02 EditDelete Third album artist03 EditDelete fourth album artist04 EditDelete Fifth album artist05 EditDelete << 1 2 >> footer ~~~ 点击 列表上面的Add new album 可以进行 album 添加,页面结果如下所示: ~~~ header 窗体顶端 Title窗体底端 Artist footer ~~~ 点击列表右边的 Edit 可以对album 的信息进行修改,页面结果如下所示: ~~~ header 窗体顶端 Title窗体底端 Artist footer ~~~ 点击列表右边的 Delete 可以对album 记录进行删除,确认删除页面如下所示: ~~~ header Delete album Are you sure that you want to delete 'First album' by 'artist01'? 窗体顶端 窗体底端 footer ~~~ 到此已经完成了对整个Album模块的的构造及功能的实现,此实例虽然没有实际的应用意义,但他已经完整的展示了在ZF2一个完整模块的存在形式,以及与其他模块同时并存且同时协同工作的具体应用,在进行具体项目开发的时候可以借鉴或参考此例以便开发出不同功能的模块,使用项目模块能够共同协同工作。
';