1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
|
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Controller
* @subpackage Zend_Controller_Action_Helper
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: ViewRenderer.php 20261 2010-01-13 18:55:25Z matthew $
*/
/**
* @see Zend_Controller_Action_Helper_Abstract
*/
require_once 'Zend/Controller/Action/Helper/Abstract.php';
/**
* @see Zend_View
*/
require_once 'Zend/View.php';
/**
* View script integration
*
* Zend_Controller_Action_Helper_ViewRenderer provides transparent view
* integration for action controllers. It allows you to create a view object
* once, and populate it throughout all actions. Several global options may be
* set:
*
* - noController: if set true, render() will not look for view scripts in
* subdirectories named after the controller
* - viewSuffix: what view script filename suffix to use
*
* The helper autoinitializes the action controller view preDispatch(). It
* determines the path to the class file, and then determines the view base
* directory from there. It also uses the module name as a class prefix for
* helpers and views such that if your module name is 'Search', it will set the
* helper class prefix to 'Search_View_Helper' and the filter class prefix to ;
* 'Search_View_Filter'.
*
* Usage:
* <code>
* // In your bootstrap:
* Zend_Controller_Action_HelperBroker::addHelper(new Zend_Controller_Action_Helper_ViewRenderer());
*
* // In your action controller methods:
* $viewHelper = $this->_helper->getHelper('view');
*
* // Don't use controller subdirectories
* $viewHelper->setNoController(true);
*
* // Specify a different script to render:
* $this->_helper->viewRenderer('form');
*
* </code>
*
* @uses Zend_Controller_Action_Helper_Abstract
* @package Zend_Controller
* @subpackage Zend_Controller_Action_Helper
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Controller_Action_Helper_ViewRenderer extends Zend_Controller_Action_Helper_Abstract
{
/**
* @var Zend_View_Interface
*/
public $view;
/**
* Word delimiters
* @var array
*/
protected $_delimiters;
/**
* @var Zend_Filter_Inflector
*/
protected $_inflector;
/**
* Inflector target
* @var string
*/
protected $_inflectorTarget = '';
/**
* Current module directory
* @var string
*/
protected $_moduleDir = '';
/**
* Whether or not to autorender using controller name as subdirectory;
* global setting (not reset at next invocation)
* @var boolean
*/
protected $_neverController = false;
/**
* Whether or not to autorender postDispatch; global setting (not reset at
* next invocation)
* @var boolean
*/
protected $_neverRender = false;
/**
* Whether or not to use a controller name as a subdirectory when rendering
* @var boolean
*/
protected $_noController = false;
/**
* Whether or not to autorender postDispatch; per controller/action setting (reset
* at next invocation)
* @var boolean
*/
protected $_noRender = false;
/**
* Characters representing path delimiters in the controller
* @var string|array
*/
protected $_pathDelimiters;
/**
* Which named segment of the response to utilize
* @var string
*/
protected $_responseSegment = null;
/**
* Which action view script to render
* @var string
*/
protected $_scriptAction = null;
/**
* View object basePath
* @var string
*/
protected $_viewBasePathSpec = ':moduleDir/views';
/**
* View script path specification string
* @var string
*/
protected $_viewScriptPathSpec = ':controller/:action.:suffix';
/**
* View script path specification string, minus controller segment
* @var string
*/
protected $_viewScriptPathNoControllerSpec = ':action.:suffix';
/**
* View script suffix
* @var string
*/
protected $_viewSuffix = 'phtml';
/**
* Constructor
*
* Optionally set view object and options.
*
* @param Zend_View_Interface $view
* @param array $options
* @return void
*/
public function __construct(Zend_View_Interface $view = null, array $options = array())
{
if (null !== $view) {
$this->setView($view);
}
if (!empty($options)) {
$this->_setOptions($options);
}
}
/**
* Clone - also make sure the view is cloned.
*
* @return void
*/
public function __clone()
{
if (isset($this->view) && $this->view instanceof Zend_View_Interface) {
$this->view = clone $this->view;
}
}
/**
* Set the view object
*
* @param Zend_View_Interface $view
* @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
*/
public function setView(Zend_View_Interface $view)
{
$this->view = $view;
return $this;
}
/**
* Get current module name
*
* @return string
*/
public function getModule()
{
$request = $this->getRequest();
$module = $request->getModuleName();
if (null === $module) {
$module = $this->getFrontController()->getDispatcher()->getDefaultModule();
}
return $module;
}
/**
* Get module directory
*
* @throws Zend_Controller_Action_Exception
* @return string
*/
public function getModuleDirectory()
{
$module = $this->getModule();
$moduleDir = $this->getFrontController()->getControllerDirectory($module);
if ((null === $moduleDir) || is_array($moduleDir)) {
/**
* @see Zend_Controller_Action_Exception
*/
require_once 'Zend/Controller/Action/Exception.php';
throw new Zend_Controller_Action_Exception('ViewRenderer cannot locate module directory for module "' . $module . '"');
}
$this->_moduleDir = dirname($moduleDir);
return $this->_moduleDir;
}
/**
* Get inflector
*
* @return Zend_Filter_Inflector
*/
public function getInflector()
{
if (null === $this->_inflector) {
/**
* @see Zend_Filter_Inflector
*/
require_once 'Zend/Filter/Inflector.php';
/**
* @see Zend_Filter_PregReplace
*/
require_once 'Zend/Filter/PregReplace.php';
/**
* @see Zend_Filter_Word_UnderscoreToSeparator
*/
require_once 'Zend/Filter/Word/UnderscoreToSeparator.php';
$this->_inflector = new Zend_Filter_Inflector();
$this->_inflector->setStaticRuleReference('moduleDir', $this->_moduleDir) // moduleDir must be specified before the less specific 'module'
->addRules(array(
':module' => array('Word_CamelCaseToDash', 'StringToLower'),
':controller' => array('Word_CamelCaseToDash', new Zend_Filter_Word_UnderscoreToSeparator('/'), 'StringToLower', new Zend_Filter_PregReplace('/\./', '-')),
':action' => array('Word_CamelCaseToDash', new Zend_Filter_PregReplace('#[^a-z0-9' . preg_quote('/', '#') . ']+#i', '-'), 'StringToLower'),
))
->setStaticRuleReference('suffix', $this->_viewSuffix)
->setTargetReference($this->_inflectorTarget);
}
// Ensure that module directory is current
$this->getModuleDirectory();
return $this->_inflector;
}
/**
* Set inflector
*
* @param Zend_Filter_Inflector $inflector
* @param boolean $reference Whether the moduleDir, target, and suffix should be set as references to ViewRenderer properties
* @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
*/
public function setInflector(Zend_Filter_Inflector $inflector, $reference = false)
{
$this->_inflector = $inflector;
if ($reference) {
$this->_inflector->setStaticRuleReference('suffix', $this->_viewSuffix)
->setStaticRuleReference('moduleDir', $this->_moduleDir)
->setTargetReference($this->_inflectorTarget);
}
return $this;
}
/**
* Set inflector target
*
* @param string $target
* @return void
*/
protected function _setInflectorTarget($target)
{
$this->_inflectorTarget = (string) $target;
}
/**
* Set internal module directory representation
*
* @param string $dir
* @return void
*/
protected function _setModuleDir($dir)
{
$this->_moduleDir = (string) $dir;
}
/**
* Get internal module directory representation
*
* @return string
*/
protected function _getModuleDir()
{
return $this->_moduleDir;
}
/**
* Generate a class prefix for helper and filter classes
*
* @return string
*/
protected function _generateDefaultPrefix()
{
$default = 'Zend_View';
if (null === $this->_actionController) {
return $default;
}
$class = get_class($this->_actionController);
if (!strstr($class, '_')) {
return $default;
}
$module = $this->getModule();
if ('default' == $module) {
return $default;
}
$prefix = substr($class, 0, strpos($class, '_')) . '_View';
return $prefix;
}
/**
* Retrieve base path based on location of current action controller
*
* @return string
*/
protected function _getBasePath()
{
if (null === $this->_actionController) {
return './views';
}
$inflector = $this->getInflector();
$this->_setInflectorTarget($this->getViewBasePathSpec());
$dispatcher = $this->getFrontController()->getDispatcher();
$request = $this->getRequest();
$parts = array(
'module' => (($moduleName = $request->getModuleName()) != '') ? $dispatcher->formatModuleName($moduleName) : $moduleName,
'controller' => $request->getControllerName(),
'action' => $dispatcher->formatActionName($request->getActionName())
);
$path = $inflector->filter($parts);
return $path;
}
/**
* Set options
*
* @param array $options
* @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
*/
protected function _setOptions(array $options)
{
foreach ($options as $key => $value)
{
switch ($key) {
case 'neverRender':
case 'neverController':
case 'noController':
case 'noRender':
$property = '_' . $key;
$this->{$property} = ($value) ? true : false;
break;
case 'responseSegment':
case 'scriptAction':
case 'viewBasePathSpec':
case 'viewScriptPathSpec':
case 'viewScriptPathNoControllerSpec':
case 'viewSuffix':
$property = '_' . $key;
$this->{$property} = (string) $value;
break;
default:
break;
}
}
return $this;
}
/**
* Initialize the view object
*
* $options may contain the following keys:
* - neverRender - flag dis/enabling postDispatch() autorender (affects all subsequent calls)
* - noController - flag indicating whether or not to look for view scripts in subdirectories named after the controller
* - noRender - flag indicating whether or not to autorender postDispatch()
* - responseSegment - which named response segment to render a view script to
* - scriptAction - what action script to render
* - viewBasePathSpec - specification to use for determining view base path
* - viewScriptPathSpec - specification to use for determining view script paths
* - viewScriptPathNoControllerSpec - specification to use for determining view script paths when noController flag is set
* - viewSuffix - what view script filename suffix to use
*
* @param string $path
* @param string $prefix
* @param array $options
* @throws Zend_Controller_Action_Exception
* @return void
*/
public function initView($path = null, $prefix = null, array $options = array())
{
if (null === $this->view) {
$this->setView(new Zend_View());
}
// Reset some flags every time
$options['noController'] = (isset($options['noController'])) ? $options['noController'] : false;
$options['noRender'] = (isset($options['noRender'])) ? $options['noRender'] : false;
$this->_scriptAction = null;
$this->_responseSegment = null;
// Set options first; may be used to determine other initializations
$this->_setOptions($options);
// Get base view path
if (empty($path)) {
$path = $this->_getBasePath();
if (empty($path)) {
/**
* @see Zend_Controller_Action_Exception
*/
require_once 'Zend/Controller/Action/Exception.php';
throw new Zend_Controller_Action_Exception('ViewRenderer initialization failed: retrieved view base path is empty');
}
}
if (null === $prefix) {
$prefix = $this->_generateDefaultPrefix();
}
// Determine if this path has already been registered
$currentPaths = $this->view->getScriptPaths();
$path = str_replace(array('/', '\\'), '/', $path);
$pathExists = false;
foreach ($currentPaths as $tmpPath) {
$tmpPath = str_replace(array('/', '\\'), '/', $tmpPath);
if (strstr($tmpPath, $path)) {
$pathExists = true;
break;
}
}
if (!$pathExists) {
$this->view->addBasePath($path, $prefix);
}
// Register view with action controller (unless already registered)
if ((null !== $this->_actionController) && (null === $this->_actionController->view)) {
$this->_actionController->view = $this->view;
$this->_actionController->viewSuffix = $this->_viewSuffix;
}
}
/**
* init - initialize view
*
* @return void
*/
public function init()
{
if ($this->getFrontController()->getParam('noViewRenderer')) {
return;
}
$this->initView();
}
/**
* Set view basePath specification
*
* Specification can contain one or more of the following:
* - :moduleDir - current module directory
* - :controller - name of current controller in the request
* - :action - name of current action in the request
* - :module - name of current module in the request
*
* @param string $path
* @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
*/
public function setViewBasePathSpec($path)
{
$this->_viewBasePathSpec = (string) $path;
return $this;
}
/**
* Retrieve the current view basePath specification string
*
* @return string
*/
public function getViewBasePathSpec()
{
return $this->_viewBasePathSpec;
}
/**
* Set view script path specification
*
* Specification can contain one or more of the following:
* - :moduleDir - current module directory
* - :controller - name of current controller in the request
* - :action - name of current action in the request
* - :module - name of current module in the request
*
* @param string $path
* @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
*/
public function setViewScriptPathSpec($path)
{
$this->_viewScriptPathSpec = (string) $path;
return $this;
}
/**
* Retrieve the current view script path specification string
*
* @return string
*/
public function getViewScriptPathSpec()
{
return $this->_viewScriptPathSpec;
}
/**
* Set view script path specification (no controller variant)
*
* Specification can contain one or more of the following:
* - :moduleDir - current module directory
* - :controller - name of current controller in the request
* - :action - name of current action in the request
* - :module - name of current module in the request
*
* :controller will likely be ignored in this variant.
*
* @param string $path
* @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
*/
public function setViewScriptPathNoControllerSpec($path)
{
$this->_viewScriptPathNoControllerSpec = (string) $path;
return $this;
}
/**
* Retrieve the current view script path specification string (no controller variant)
*
* @return string
*/
public function getViewScriptPathNoControllerSpec()
{
return $this->_viewScriptPathNoControllerSpec;
}
/**
* Get a view script based on an action and/or other variables
*
* Uses values found in current request if no values passed in $vars.
*
* If {@link $_noController} is set, uses {@link $_viewScriptPathNoControllerSpec};
* otherwise, uses {@link $_viewScriptPathSpec}.
*
* @param string $action
* @param array $vars
* @return string
*/
public function getViewScript($action = null, array $vars = array())
{
$request = $this->getRequest();
if ((null === $action) && (!isset($vars['action']))) {
$action = $this->getScriptAction();
if (null === $action) {
$action = $request->getActionName();
}
$vars['action'] = $action;
} elseif (null !== $action) {
$vars['action'] = $action;
}
$inflector = $this->getInflector();
if ($this->getNoController() || $this->getNeverController()) {
$this->_setInflectorTarget($this->getViewScriptPathNoControllerSpec());
} else {
$this->_setInflectorTarget($this->getViewScriptPathSpec());
}
return $this->_translateSpec($vars);
}
/**
* Set the neverRender flag (i.e., globally dis/enable autorendering)
*
* @param boolean $flag
* @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
*/
public function setNeverRender($flag = true)
{
$this->_neverRender = ($flag) ? true : false;
return $this;
}
/**
* Retrieve neverRender flag value
*
* @return boolean
*/
public function getNeverRender()
{
return $this->_neverRender;
}
/**
* Set the noRender flag (i.e., whether or not to autorender)
*
* @param boolean $flag
* @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
*/
public function setNoRender($flag = true)
{
$this->_noRender = ($flag) ? true : false;
return $this;
}
/**
* Retrieve noRender flag value
*
* @return boolean
*/
public function getNoRender()
{
return $this->_noRender;
}
/**
* Set the view script to use
*
* @param string $name
* @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
*/
public function setScriptAction($name)
{
$this->_scriptAction = (string) $name;
return $this;
}
/**
* Retrieve view script name
*
* @return string
*/
public function getScriptAction()
{
return $this->_scriptAction;
}
/**
* Set the response segment name
*
* @param string $name
* @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
*/
public function setResponseSegment($name)
{
if (null === $name) {
$this->_responseSegment = null;
} else {
$this->_responseSegment = (string) $name;
}
return $this;
}
/**
* Retrieve named response segment name
*
* @return string
*/
public function getResponseSegment()
{
return $this->_responseSegment;
}
/**
* Set the noController flag (i.e., whether or not to render into controller subdirectories)
*
* @param boolean $flag
* @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
*/
public function setNoController($flag = true)
{
$this->_noController = ($flag) ? true : false;
return $this;
}
/**
* Retrieve noController flag value
*
* @return boolean
*/
public function getNoController()
{
return $this->_noController;
}
/**
* Set the neverController flag (i.e., whether or not to render into controller subdirectories)
*
* @param boolean $flag
* @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
*/
public function setNeverController($flag = true)
{
$this->_neverController = ($flag) ? true : false;
return $this;
}
/**
* Retrieve neverController flag value
*
* @return boolean
*/
public function getNeverController()
{
return $this->_neverController;
}
/**
* Set view script suffix
*
* @param string $suffix
* @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
*/
public function setViewSuffix($suffix)
{
$this->_viewSuffix = (string) $suffix;
return $this;
}
/**
* Get view script suffix
*
* @return string
*/
public function getViewSuffix()
{
return $this->_viewSuffix;
}
/**
* Set options for rendering a view script
*
* @param string $action View script to render
* @param string $name Response named segment to render to
* @param boolean $noController Whether or not to render within a subdirectory named after the controller
* @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
*/
public function setRender($action = null, $name = null, $noController = null)
{
if (null !== $action) {
$this->setScriptAction($action);
}
if (null !== $name) {
$this->setResponseSegment($name);
}
if (null !== $noController) {
$this->setNoController($noController);
}
return $this;
}
/**
* Inflect based on provided vars
*
* Allowed variables are:
* - :moduleDir - current module directory
* - :module - current module name
* - :controller - current controller name
* - :action - current action name
* - :suffix - view script file suffix
*
* @param array $vars
* @return string
*/
protected function _translateSpec(array $vars = array())
{
$inflector = $this->getInflector();
$request = $this->getRequest();
$dispatcher = $this->getFrontController()->getDispatcher();
$module = $dispatcher->formatModuleName($request->getModuleName());
$controller = $request->getControllerName();
$action = $dispatcher->formatActionName($request->getActionName());
$params = compact('module', 'controller', 'action');
foreach ($vars as $key => $value) {
switch ($key) {
case 'module':
case 'controller':
case 'action':
case 'moduleDir':
case 'suffix':
$params[$key] = (string) $value;
break;
default:
break;
}
}
if (isset($params['suffix'])) {
$origSuffix = $this->getViewSuffix();
$this->setViewSuffix($params['suffix']);
}
if (isset($params['moduleDir'])) {
$origModuleDir = $this->_getModuleDir();
$this->_setModuleDir($params['moduleDir']);
}
$filtered = $inflector->filter($params);
if (isset($params['suffix'])) {
$this->setViewSuffix($origSuffix);
}
if (isset($params['moduleDir'])) {
$this->_setModuleDir($origModuleDir);
}
return $filtered;
}
/**
* Render a view script (optionally to a named response segment)
*
* Sets the noRender flag to true when called.
*
* @param string $script
* @param string $name
* @return void
*/
public function renderScript($script, $name = null)
{
if (null === $name) {
$name = $this->getResponseSegment();
}
$this->getResponse()->appendBody(
$this->view->render($script),
$name
);
$this->setNoRender();
}
/**
* Render a view based on path specifications
*
* Renders a view based on the view script path specifications.
*
* @param string $action
* @param string $name
* @param boolean $noController
* @return void
*/
public function render($action = null, $name = null, $noController = null)
{
$this->setRender($action, $name, $noController);
$path = $this->getViewScript();
$this->renderScript($path, $name);
}
/**
* Render a script based on specification variables
*
* Pass an action, and one or more specification variables (view script suffix)
* to determine the view script path, and render that script.
*
* @param string $action
* @param array $vars
* @param string $name
* @return void
*/
public function renderBySpec($action = null, array $vars = array(), $name = null)
{
if (null !== $name) {
$this->setResponseSegment($name);
}
$path = $this->getViewScript($action, $vars);
$this->renderScript($path);
}
/**
* postDispatch - auto render a view
*
* Only autorenders if:
* - _noRender is false
* - action controller is present
* - request has not been re-dispatched (i.e., _forward() has not been called)
* - response is not a redirect
*
* @return void
*/
public function postDispatch()
{
if ($this->_shouldRender()) {
$this->render();
}
}
/**
* Should the ViewRenderer render a view script?
*
* @return boolean
*/
protected function _shouldRender()
{
return (!$this->getFrontController()->getParam('noViewRenderer')
&& !$this->_neverRender
&& !$this->_noRender
&& (null !== $this->_actionController)
&& $this->getRequest()->isDispatched()
&& !$this->getResponse()->isRedirect()
);
}
/**
* Use this helper as a method; proxies to setRender()
*
* @param string $action
* @param string $name
* @param boolean $noController
* @return void
*/
public function direct($action = null, $name = null, $noController = null)
{
$this->setRender($action, $name, $noController);
}
}
|