sfPartialView.class.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 13479 2008-11-29 13:52:41Z Kris.Wallsmith $
  16. */
  17. class sfPartialView extends sfPHPView
  18. {
  19. protected
  20. $partialVars = array();
  21. /**
  22. * Executes any presentation logic for this view.
  23. */
  24. public function execute()
  25. {
  26. }
  27. /**
  28. * @param array $partialvars
  29. */
  30. public function setPartialVars(array $partialVars)
  31. {
  32. $this->partialVars = $partialVars;
  33. $this->getAttributeHolder()->add($partialVars);
  34. }
  35. /**
  36. * Configures template for this view.
  37. */
  38. public function configure()
  39. {
  40. $this->setDecorator(false);
  41. $this->setTemplate($this->actionName.$this->getExtension());
  42. if ('global' == $this->moduleName)
  43. {
  44. $this->setDirectory($this->context->getConfiguration()->getDecoratorDir($this->getTemplate()));
  45. }
  46. else
  47. {
  48. $this->setDirectory($this->context->getConfiguration()->getTemplateDir($this->moduleName, $this->getTemplate()));
  49. }
  50. }
  51. /**
  52. * Renders the presentation.
  53. *
  54. * @return string Current template content
  55. */
  56. public function render()
  57. {
  58. if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
  59. {
  60. $timer = sfTimerManager::getTimer(sprintf('Partial "%s/%s"', $this->moduleName, $this->actionName));
  61. }
  62. if ($retval = $this->getCache())
  63. {
  64. return $retval;
  65. }
  66. else if (sfConfig::get('sf_cache'))
  67. {
  68. $mainResponse = $this->context->getResponse();
  69. $responseClass = get_class($mainResponse);
  70. $this->context->setResponse($response = new $responseClass($this->context->getEventDispatcher(), array_merge($mainResponse->getOptions(), array('content_type' => $mainResponse->getContentType()))));
  71. }
  72. // execute pre-render check
  73. $this->preRenderCheck();
  74. $this->getAttributeHolder()->set('sf_type', 'partial');
  75. // render template
  76. $retval = $this->renderFile($this->getDirectory().'/'.$this->getTemplate());
  77. if (sfConfig::get('sf_cache'))
  78. {
  79. $retval = $this->viewCache->setPartialCache($this->moduleName, $this->actionName, $this->cacheKey, $retval);
  80. $this->context->setResponse($mainResponse);
  81. $mainResponse->merge($response);
  82. }
  83. if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
  84. {
  85. $timer->addTime();
  86. }
  87. return $retval;
  88. }
  89. public function getCache()
  90. {
  91. if (!sfConfig::get('sf_cache'))
  92. {
  93. return null;
  94. }
  95. $this->viewCache = $this->context->getViewCacheManager();
  96. $this->viewCache->registerConfiguration($this->moduleName);
  97. $this->cacheKey = $this->viewCache->computeCacheKey($this->partialVars);
  98. if ($retval = $this->viewCache->getPartialCache($this->moduleName, $this->actionName, $this->cacheKey))
  99. {
  100. return $retval;
  101. }
  102. }
  103. }