Zend reuse views
For every new controller and action I create, Zend expects a template file in /views/scripts/controllername
. Howev开发者_Go百科er, I would like to have multiple actions share a single template into which text from a database can be injected.
I currently use a layout and delegate page specific views with echo $this->layout()->content
. I tried the following:
class SomeController extends Zend_Controller_Action{
public function someAction() {
$path = $this->view->getScriptPath();
$this->view = new Zend_View();
$this->view->setScriptPath($path);
$this->view->render('default.phtml');
}
}
However, I get an error that the script 'some/some.phtml' not found in path. How do I do this correctly?
You have to use 'default' (without the extension) and call the method directly (not on the view), e.g.
$this->render('default');
See Zend_Controller_Action::render
render( string|null $action = null, string|null $name = null, bool $noController = false ) : void
Render a view
Renders a view. By default, views are found in the view script path as /.phtml. You may change the script suffix by resetting {@link $viewSuffix}. You may omit the controller directory prefix by specifying boolean true for $noController.
By default, the rendered contents are appended to the response. You may specify the named body content segment to set by specifying a $name.
If you want to provide a specific script, use
$this->renderScript('controller/action.phtml');
See Zend_Controller_Action::renderScript
renderScript( string $script, string $name = null ) : void
Render a given view script
Similar to {@link render()}, this method renders a view script. Unlike render(), however, it does not autodetermine the view script via {@link getViewScript()}, but instead renders the script passed to it. Use this if you know the exact view script name and path you wish to use, or if using paths that do not conform to the spec defined with getViewScript().
By default, the rendered contents are appended to the response. You may specify the named body content segment to set by specifying a $name.
If you would choose to use viewRenderer and you would need to pick a view from another controller you must set $noController parameter to TRUE:
BookController:
public function saveBookAction()
{
...
$this->_helper->viewRenderer('/approval/index', null, $noController = true);
}
This would look for the view /views/scripts/approval/index.phtml instead of the default /views/scripts/book/save-book.phtml.
精彩评论