sfPartialView.class.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * A View to render partials.
  11. *
  12. * @package symfony
  13. * @subpackage view
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfPartialView.class.php 17468 2009-04-21 07:21:38Z fabien $
  16. */
  17. class sfPartialView extends sfPHPView
  18. {
  19. protected
  20. $viewCache = null,
  21. $checkCache = false,
  22. $cacheKey = null,
  23. $partialVars = array();
  24. /**
  25. * Constructor.
  26. *
  27. * @see sfView
  28. */
  29. public function initialize($context, $moduleName, $actionName, $viewName)
  30. {
  31. $ret = parent::initialize($context, $moduleName, $actionName, $viewName);
  32. $this->viewCache = $this->context->getViewCacheManager();
  33. if (sfConfig::get('sf_cache'))
  34. {
  35. $this->checkCache = sfConfig::get('sf_lazy_cache_key') ? $this->viewCache->isActionCacheable($moduleName, $actionName) : true;
  36. }
  37. return $ret;
  38. }
  39. /**
  40. * Executes any presentation logic for this view.
  41. */
  42. public function execute()
  43. {
  44. }
  45. /**
  46. * @param array $partialvars
  47. */
  48. public function setPartialVars(array $partialVars)
  49. {
  50. $this->partialVars = $partialVars;
  51. $this->getAttributeHolder()->add($partialVars);
  52. }
  53. /**
  54. * Configures template for this view.
  55. */
  56. public function configure()
  57. {
  58. $this->setDecorator(false);
  59. $this->setTemplate($this->actionName.$this->getExtension());
  60. if ('global' == $this->moduleName)
  61. {
  62. $this->setDirectory($this->context->getConfiguration()->getDecoratorDir($this->getTemplate()));
  63. }
  64. else
  65. {
  66. $this->setDirectory($this->context->getConfiguration()->getTemplateDir($this->moduleName, $this->getTemplate()));
  67. }
  68. }
  69. /**
  70. * Renders the presentation.
  71. *
  72. * @return string Current template content
  73. */
  74. public function render()
  75. {
  76. if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
  77. {
  78. $timer = sfTimerManager::getTimer(sprintf('Partial "%s/%s"', $this->moduleName, $this->actionName));
  79. }
  80. if ($retval = $this->getCache())
  81. {
  82. return $retval;
  83. }
  84. else if ($this->checkCache)
  85. {
  86. $mainResponse = $this->context->getResponse();
  87. $responseClass = get_class($mainResponse);
  88. $this->context->setResponse($response = new $responseClass($this->context->getEventDispatcher(), $mainResponse->getOptions()));
  89. }
  90. try
  91. {
  92. // execute pre-render check
  93. $this->preRenderCheck();
  94. $this->getAttributeHolder()->set('sf_type', 'partial');
  95. // render template
  96. $retval = $this->renderFile($this->getDirectory().'/'.$this->getTemplate());
  97. }
  98. catch (Exception $e)
  99. {
  100. if ($this->checkCache)
  101. {
  102. $this->context->setResponse($mainResponse);
  103. $mainResponse->merge($response);
  104. }
  105. throw $e;
  106. }
  107. if ($this->checkCache)
  108. {
  109. $retval = $this->viewCache->setPartialCache($this->moduleName, $this->actionName, $this->cacheKey, $retval);
  110. $this->context->setResponse($mainResponse);
  111. $mainResponse->merge($response);
  112. }
  113. if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
  114. {
  115. $timer->addTime();
  116. }
  117. return $retval;
  118. }
  119. public function getCache()
  120. {
  121. if (!$this->checkCache)
  122. {
  123. return null;
  124. }
  125. $this->cacheKey = $this->viewCache->checkCacheKey($this->partialVars);
  126. if ($retval = $this->viewCache->getPartialCache($this->moduleName, $this->actionName, $this->cacheKey))
  127. {
  128. return $retval;
  129. }
  130. }
  131. }