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

Debug toolbar