Friday, March 7, 2014

zend2 framework develop log 2– render many partial layout

1.introduction

i want to dynamic use js and css file in layout.layout.

My origin method is design(bellow way-2,but way-1 is easy and useful):

there are two layout files:

Application\view\layout\:

                                        layout.layout

                                        rank.layout

and my view want to render rank.layout,and rank.layout will partial layout.layout.

3.code

3.1 way 1 – view –> call layout

module/Application/view/application/rank/index.phtml:

<?php 

$baseResPath = $this->basePath();
$this->headLink()
->appendStylesheet($baseResPath . '/css/book/SearchRank.css')
->appendStylesheet($baseResPath . '/css/book/BookShelf.css');

if(defined('PK_DEBUG_MODE')){
$this->inlineScript()->appendFile($baseResPath . '/js/third/jquery/jquery-old.js', 'text/javascript');
}else{
$this->inlineScript()->appendFile('http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js', 'text/javascript');
}

?>

module/Application/view/layout/layout.phtml:

<?php echo $this->doctype(); ?>
<html lang="en">
<head>
<meta charset="utf-8">
<?php echo $this->headTitle('pkrss '. $this->translate('images book'))->setSeparator(' - ')->setAutoEscape(false);
echo $this->headMeta()->appendName('viewport', 'width=device-width, initial-scale=1.0');


echo $this->headLink(array('rel' => 'shortcut icon', 'href' => $this->basePath() . 'favicon.ico'));
?>
</head>
<body>
<?php echo $this->content;

echo $this->inlineScript();
?>
</body>
</html>

result then output source for xxx/application/rank/index:


<!DOCTYPE html><html lang="en">
<head>
<meta charset="utf-8">
<title>pkrss images book</title><meta name="viewport" content="width=device-width, initial-scale=1.0"><link href="/book/public/css/book/SearchRank.css" media="screen" rel="stylesheet" type="text/css">
<link href="/book/public/css/book/BookShelf.css" media="screen" rel="stylesheet" type="text/css">
<link href="/book/publicfavicon.ico" rel="shortcut icon"> </head>
<body>
<script type="text/javascript" src="/book/public/js/third/jquery/jquery-old.js"></script> </body>
</html>



 


3.2 way 2 - use layout call layout


switch show rank.layout


module/Application/src/Application/Controller/RankController.php:

<?php
namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class RankController extends AbstractActionController
{
public function indexAction()
{
$this->layout('layout/rank');
return new ViewModel();
}
}

module/Application/view/layout/layout.phtml:

<?php echo $this->doctype(); ?>
<html lang="en">
<head>
<meta charset="utf-8">
<?php echo $this->headTitle('pkrss '. $this->translate('images book'))->setSeparator(' - ')->setAutoEscape(false);
echo $this->headMeta()->appendName('viewport', 'width=device-width, initial-scale=1.0');

echo $this->headLink(array('rel' => 'shortcut icon', 'href' => $this->basePath() . 'favicon.ico'));
// echo $this->headScript()->prependFile($this->basePath() . '/js/jquery-old.js', 'text/javascript')
?>
</head>
<body>
<?php echo $this->content;

echo $this->inlineScript();
?>
</body>
</html>



module/Application/view/layout/rank.phtml:

<?php 

$baseResPath = $this->basePath(); // '/s/ful/';
$this->headLink()
->appendStylesheet($baseResPath . '/third/bookreader/BookReader/BookReader.css')
->appendStylesheet($baseResPath . '/css/book/BookReader.css')
->appendStylesheet($baseResPath . '/css/book/BookShelf.css');

if(defined('PK_DEBUG_MODE')){
$this->inlineScript()->appendFile($baseResPath . '/js/third/jquery/jquery-old.js', 'text/javascript')
->appendFile($baseResPath . '/js/third/jquery-ui/jquery-ui.js')
->appendFile($baseResPath . '/js/third/dragscrollable/dragscrollable.js')
->appendFile($baseResPath . '/js/third/jquery-colorbox/jquery.colorbox.js')
->appendFile($baseResPath . '/js/third/jquery-ui/jquery.ui.ipad.js')
->appendFile($baseResPath . '/js/third/jquery-bt/jquery.bt.js')
->appendFile($baseResPath . '/js/book/BookReader.js')
->appendFile($baseResPath . '/js/book/utils.js')
->appendFile($baseResPath . '/js/native/core.js')
->appendFile($baseResPath . '/js/utils/db.js')
->appendFile($baseResPath . '/js/book/pkBookShelf.js')
->appendFile($baseResPath . '/js/book/pkBookImage.js')
->appendFile($baseResPath . '/js/book/main.js')
->appendFile($baseResPath . '/js/book/BookReaderJSSimple.js')
->appendFile($baseResPath . '/js/plugin/book_photomenu.js')
->appendFile($baseResPath . '/js/plugin/o_flickr.js')
->appendFile($baseResPath . '/js/plugin/o_bing.js')
->appendFile($baseResPath . '/js/plugin/book_localphoto.js')
->appendFile($baseResPath . '/js/plugin/o_authhtm.js');
}else{
$this->inlineScript()->appendFile('http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js', 'text/javascript')
->appendFile('http://ajax.aspnetcdn.com/ajax/jquery.ui/1.10.4/jquery-ui.min.js')
->appendFile($baseResPath . '/js/book-all.js');
}

echo $this->partial('layout/layout',array('content' => $this->content));
?>



module/Application/config/module.config.php:

<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

return array(
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
),
),
// The following is a route to simplify getting started creating
// new controllers and actions without needing to create a new
// module. Simply drop new controllers in, and you can access them
// using the path /application/:controller/:action
'application' => array(
'type' => 'Literal',
'options' => array(
'route' => '/application',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
),
),
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController',
'Application\Controller\Rank' => 'Application\Controller\RankController',
),
),
'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',
'layout/rank' => __DIR__ . '/../view/layout/rank.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',
),
),
'view_helpers' => array(
'invokables' => array(
'OutputHelper' => 'book\module\Application\OutputHelper',
),
)
);



result then output source for xxx/application/rank/index:

<!DOCTYPE html><html lang="en">
<head>
<meta charset="utf-8">
<title>pkrss images book</title><meta name="viewport" content="width=device-width, initial-scale=1.0"><link href="/book/public/third/bookreader/BookReader/BookReader.css" media="screen" rel="stylesheet" type="text/css">
<link href="/book/public/css/book/BookReader.css" media="screen" rel="stylesheet" type="text/css">
<link href="/book/public/css/book/BookShelf.css" media="screen" rel="stylesheet" type="text/css">
<link href="/book/publicfavicon.ico" rel="shortcut icon"> </head>
<body>
<script type="text/javascript" src="/book/public/js/third/jquery/jquery-old.js"></script>
<script type="text/javascript" src="/book/public/js/third/jquery-ui/jquery-ui.js"></script>
<script type="text/javascript" src="/book/public/js/third/dragscrollable/dragscrollable.js"></script>
<script type="text/javascript" src="/book/public/js/third/jquery-colorbox/jquery.colorbox.js"></script>
<script type="text/javascript" src="/book/public/js/third/jquery-ui/jquery.ui.ipad.js"></script>
<script type="text/javascript" src="/book/public/js/third/jquery-bt/jquery.bt.js"></script>
<script type="text/javascript" src="/book/public/js/book/BookReader.js"></script>
<script type="text/javascript" src="/book/public/js/book/utils.js"></script>
<script type="text/javascript" src="/book/public/js/native/core.js"></script>
<script type="text/javascript" src="/book/public/js/utils/db.js"></script>
<script type="text/javascript" src="/book/public/js/book/pkBookShelf.js"></script>
<script type="text/javascript" src="/book/public/js/book/pkBookImage.js"></script>
<script type="text/javascript" src="/book/public/js/book/main.js"></script>
<script type="text/javascript" src="/book/public/js/book/BookReaderJSSimple.js"></script>
<script type="text/javascript" src="/book/public/js/plugin/book_photomenu.js"></script>
<script type="text/javascript" src="/book/public/js/plugin/o_flickr.js"></script>
<script type="text/javascript" src="/book/public/js/plugin/o_bing.js"></script>
<script type="text/javascript" src="/book/public/js/plugin/book_localphoto.js"></script>
<script type="text/javascript" src="/book/public/js/plugin/o_authhtm.js"></script> </body>
</html>



4.Last


Thanks !

1 comment:

51 special xen vps plans said...

Ιt's great thɑtt you are getting thoughts from this article ass well as
from ouг disϲussion made at this place.