php - Using Layouts setRoot() in standalone \Zend\View -
php - Using Layouts setRoot() in standalone \Zend\View -
what need
i needed templating engine little site , decided seek using \zend\view
. i'm not using zend framework though.
so want info processing in custom scripts outside of zend, , pass variables \zend\view
, create render templates.
i referring next manual: zend.view.quick-start.html. basic implementation (where utilize \zend\view\renderer\phprenderer
, \zend\view\model\viewmodel
only) works expected. run errors when seek more complex, such using mutual layout.
if got right, \zend\view\model\viewmodel
s can nested, , can phone call kid ones using predefined placeholders (zend.view.quick-start.html#nesting-view-models). there should 1 primary viewmodel
serve root (zend.view.quick-start.html#dealing-with-layouts), contain skeleton html page doctype , whatever pass headtitle()
, headmeta()
etc.
i tried create viewmodel, point root layout using settemplate()
, pass setroot()
function, didn't work. tried search code function , , found in /zend/view/helper/viewmodel.php. takes instance of zend\view\model\modelinterface
, not instance of \zend\view\model\viewmodel
. when seek create new modelinterface , settemplate it, throws me
fatal error: cannot instantiate interface zend\view\model\modelinterface
it becomes overcomplicated, , can't figure out.
here's code:<?php require_once '/path/to/zend/autoloader.php'; $resolver = new \zend\view\resolver\templatepathstack(); $resolver->addpath('/path/to/views/'); $viewmodel = new \zend\view\model\viewmodel(); $viewmodel->settemplate('layout.phtml'); //$modelinterface = new \zend\view\model\modelinterface(); //$modelinterface->settemplate('layout.phtml'); // class has setroot() declared //$zendviewhelperviewmodel = new \zend\view\helper\viewmodel(); //$zendviewhelperviewmodel->setroot($modelinterface); $renderer = new \zend\view\renderer\phprenderer(); $renderer->setresolver($resolver); //$renderer->layout('layout.phtml'); $renderer->headtitle('zend'); $renderer->headmeta()->appendhttpequiv('content-type', 'text/html; charset=utf-8'); $renderer->headlink()->setstylesheet('/test1.css'); $renderer->headlink()->appendstylesheet('/test2.css'); $renderer->headscript()->setfile('/javascript1.js'); $renderer->headscript()->appendfile('/javascript2.js'); $viewmodel->setvariable('date', gmdate('r')); echo $renderer->render($viewmodel); //echo $renderer->render($zendviewhelperviewmodel);
here's layout.phtml
code: <?php $this->headmeta()->appendhttpequiv('content-type', 'text/html;charset=utf-8'); $this->headtitle()->setseparator(' - '); ?> <!doctype html> <html> <head> <?php echo $this->headmeta(); ?> <?php echo $this->headtitle(); ?> <?php echo $this->headlink() . "\n" ?> <?php echo $this->headscript(). "\n"?> </head> <body> <div id="nav"> <?php echo $this->layout()->nav ?> </div> <div id="content"> <?php echo $this->layout()->content; ?> </div> </body> </html>
what get: fatal error: uncaught exception 'zend\view\exception\runtimeexception' message 'zend\view\helper\layout::getroot: no view model registered root in renderer' in zend/view/helper/layout.php:64 stack trace:
0 zend/view/helper/layout.php(37): zend\view\helper\layout->getroot()
1 [internal function]: zend\view\helper\layout->__invoke()
2 zend/view/renderer/phprenderer.php(399): call_user_func_array(object(zend\view\helper\layout), array)
3 views/layout.phtml(16): zend\view\renderer\phprenderer->__call('layout', array)
4 views/layout.phtml(16): zend\view\renderer\phprenderer->layout()
5 zend/view/renderer/phprenderer.php(506): include('...')
6 zendviewtest.php(32): zend\view\renderer\phprenderer->render(object(zend\view\model\viewmodel))
7 {main} thrown in zend/view/helper/layout.php on line 64
i believe because of $this->layout()
reference. when remove part, starts working normally.
but think $this->layout()
necessary, because it's natural way of passing page content. if remove , utilize plain setvariable('content', ...)
break zend conventions, right?
i'd utilize nested layouts header, footer etc. i'm not sure how implement that. tried searching keywords , error codes, can't find helpful. please advise.
update best way handle , display page contents? since can't utilize echo $this->layout()->content
part suggested in documentation, should replace with?
$this->layout()
returns layout view model
witch don't have. render other view scripts within 1 utilize partial view helper
.
print $this->partial('path/to/other/view/script',array('param1'=>'param1'));
layout in zend framework defined in view_manager's configs , handled controller, since doing these steps manually u don't need layout... set variables $viewmodel->setvariable('param1', 'param1value');
, utilize in view $this->param1
...
but think $this->layout() necessary, because it's natural way of passing page content. if remove , utilize plain setvariable('content', ...) break zend conventions, right?
you absolutely wrong, application completly based on zend mvc , never have used $this->layout()
, said $this->layout()
returns layout view model
witch created view manager layout.phtml file , view script have layout. setvariable('content', ...) way can proceed , there no other way ...
php zend-framework model-view-controller zend-framework2 zend-view
Comments
Post a Comment