gpt4 book ai didi

zend-framework - zend framework 两步 View

转载 作者:行者123 更新时间:2023-12-01 11:51:02 25 4
gpt4 key购买 nike

我是 zendframework 的新手。我正在尝试实现两步 View 布局:

Bootstrap.php( View /Bootstrap.php)

<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

public function _initRoutes()
{
$options = array(
'layout' => 'layout',
'layoutPath' => '/layout/layout.phtml',);
$layout = Zend_Layout::startMvc($options);
}
}?>

layout.phtml(application/view/scripts/layout/layout.phtml)

<?php
include "header.php";

?>
// view contents goes here.
<?php

$this->layout()->content;

?>
// footer goes here.
<?php
include "footer.phtml";
?>

我是一个绝对的初学者,我很感激一步一步的解释。谢谢。

最佳答案

启用布局的最简单方法是从命令行运行 Zend_Tool 命令 zf enable layout,这将添加行
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
到您的 application.ini 并构建布局目录和默认文件 layout.phtml

或者,您可以在 application.ini 文件中用两行指定布局路径和默认布局名称:

resources.layout.layoutPath = APPLICATION_PATH "/layouts" //path to layout
resources.layout.layout = master //name of layout without extension

其他布局/ View 选项可以在您的 application.ini 中设置以在您的 View 中调用:

;View Settings
;*************
resources.view[]=
resources.view.charset = "UTF-8"
resources.view.encoding = "UTF-8"
resources.view.doctype = "HTML5"
resources.view.language = "en"
resources.view.contentType = "text/html; charset=UTF-8"

然后在您的 bootstrap.php 中,您可以调用这些资源来初始化您的 View :

 /**
* initialize the registry and asign application.ini to config namespace
*/
protected function _initRegistry() {

//make application.ini configuration available in registry
$config = new Zend_Config($this->getOptions());
Zend_Registry::set('config', $config);
}

/**
* initialize the view and return it
* @return \Zend_View
*/
protected function _initView() {
//Initialize view
$view = new Zend_View();
//add custom view helper path
$view->addHelperPath('/../library/Application/View/Helper');
//set doctype for default layout
$view->doctype(Zend_Registry::get('config')->resources->view->doctype);
//set default title
$view->headTitle('Our Home');
//set head meta data
$view->headMeta()->appendHttpEquiv('Content-Type', Zend_Registry::get(
'config')->resources->view->contentType);
//set css includes
$view->headLink()->setStylesheet('/css/normalize.css');
$view->headLink()->appendStylesheet('/css/blueprint/src/liquid.css');
$view->headLink()->appendStylesheet('/css/blueprint/src/typography.css');
$view->headLink()->appendStylesheet(
'/javascript/mediaelement/build/mediaelementplayer.css');
$view->headLink()->appendStylesheet('/css/main.css');
$view->headLink()->appendStylesheet('/css/nav.css');
$view->headLink()->appendStylesheet('/css/table.css');
//add javascript files
$view->headScript()->setFile('/javascript/mediaelement/build/jquery.js');

//add it to the view renderer
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
'ViewRenderer');
$viewRenderer->setView($view);
//Return it, so that it can be stored by the bootstrap
return $view;
}

我还包含了一个方便的方法 _initRegistry() 以使用最少的代码使配置选项随处可用。

ZF 中的布局是一个简单的 html 页面,为动态或配置选项添加了占位符:

<?php
echo $this->doctype() . "\n"; //placeholder assigned in bootstrap $view->doctype
?>
<html>
<head>
<title></title>
<?php echo $this->headMeta() . "\n" //placeholder assigned in bootstrap ?>
<?php echo $this->headLink() . "\n" //placeholder assigned in bootstrap ?>
<?php echo $this->headscript(). "\n" //placeholder assigned in bootstrap?>
</head>
<body>
<section class="container">
<header class="block">
<hgroup id="header" class ="column span-24">
<h1>Our Page</h1>
</hgroup>
<nav>
<div id="nav" class="column span-24">
<?php echo $this->layout()->nav //custom placeholder ?>
</div>
</nav>
</header>
<section class="block">
<div id="main" class="column span-18 border">
<div id="flash">
<?php
//flash messenger display location
if (count($this->messages) > 0) {
printf("<h3 id='flash'>%s</h3>", $this->messages[0]);
}
?>
</div>
<?php echo $this->layout()->content; //placeholder for redering views ?>
</div>
<aside id="sidebar" class="column span-4 last">
<?php echo $this->layout()->search //custom placeholder ?>
<div id="subNav">
<?php echo $this->layout()->subNav //custom placeholder ?>
</div>
<div id="adminMenu">
<?php echo $this->layout()->adminMenu //custom placeholder ?>
</div>
</aside>
</section>
<footer class="block">
<div id="footer" class="column span-24">
<p>Created by <em>Your Name</em> with <a href="http://framework.zend.com/">Zend Framework. &COPY; </a></p>
</div>
</footer>
</section>
</body>
</html>
<?php echo $this->inlineScript() ?> //javascript includes at bottom of page

希望这对您有所帮助。

关于zend-framework - zend framework 两步 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11405653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com