1. sfPHPView.class.php
  2. /** * A view that uses PHP as the templating engine. * * @package symfony * @subpackage view * @author Fabien Potencier * @author Sean Kerr * @version SVN: $Id: sfPHPView.class.php 24615 2009-11-30 22:30:46Z Kris.Wallsmith $ */
  3. class sfPHPView extends sfView
  4. {
  5. /**
  6. * Executes any presentation logic for this view.
  7. */
  8. public function execute()
  9. {
  10. }
  11. /**
  12. * Loads core and standard helpers to be use in the template.
  13. */
  14. protected function loadCoreAndStandardHelpers()
  15. {
  16. static $coreHelpersLoaded = 0;
  17. if ($coreHelpersLoaded)
  18. {
  19. return;
  20. }
  21. $coreHelpersLoaded = 1;
  22. $helpers = array_unique(array_merge(array('Helper', 'Url', 'Asset', 'Tag', 'Escaping'), sfConfig::get('sf_standard_helpers')));
  23. $this->context->getConfiguration()->loadHelpers($helpers);
  24. }
  25. /**
  26. * Renders the presentation.
  27. *
  28. * @param string $_sfFile Filename
  29. *
  30. * @return string File content
  31. */
  32. protected function renderFile($_sfFile)
  33. {
  34. if (sfConfig::get('sf_logging_enabled'))
  35. {
  36. $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Render "%s"', $_sfFile))));
  37. }
  38. $this->loadCoreAndStandardHelpers();
  39. // EXTR_REFS can't be used (see #3595 and #3151)
  40. $vars = $this->attributeHolder->toArray();
  41. extract($vars);
  42. // render
  43. ob_start();
  44. ob_implicit_flush(0);
  45. try
  46. {
  47. require($_sfFile);
  48. }
  49. catch (Exception $e)
  50. {
  51. // need to end output buffering before throwing the exception #7596
  52. ob_end_clean();
  53. throw $e;
  54. }
  55. return ob_get_clean();
  56. }
  57. /**
  58. * Retrieves the template engine associated with this view.
  59. *
  60. * Note: This will return null because PHP itself has no engine reference.
  61. *
  62. * @return null
  63. */
  64. public function getEngine()
  65. {
  66. return null;
  67. }
  68. /**
  69. * Configures template.
  70. *
  71. * @return void
  72. */
  73. public function configure()
  74. {
  75. // store our current view
  76. $this->context->set('view_instance', $this);
  77. // require our configuration
  78. require($this->context->getConfigCache()->checkConfig('modules/'.$this->moduleName.'/config/view.yml'));
  79. // set template directory
  80. if (!$this->directory)
  81. {
  82. $this->setDirectory($this->context->getConfiguration()->getTemplateDir($this->moduleName, $this->getTemplate()));
  83. }
  84. }
  85. /**
  86. * Loop through all template slots and fill them in with the results of presentation data.
  87. *
  88. * @param string $content A chunk of decorator content
  89. *
  90. * @return string A decorated template
  91. */
  92. protected function decorate($content)
  93. {
  94. if (sfConfig::get('sf_logging_enabled'))
  95. {
  96. $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Decorate content with "%s/%s"', $this->getDecoratorDirectory(), $this->getDecoratorTemplate()))));
  97. }
  98. // set the decorator content as an attribute
  99. $attributeHolder = $this->attributeHolder;
  100. $this->attributeHolder = $this->initializeAttributeHolder(array('sf_content' => new sfOutputEscaperSafe($content)));
  101. $this->attributeHolder->set('sf_type', 'layout');
  102. // render the decorator template and return the result
  103. $ret = $this->renderFile($this->getDecoratorDirectory().'/'.$this->getDecoratorTemplate());
  104. $this->attributeHolder = $attributeHolder;
  105. return $ret;
  106. }
  107. /**
  108. * Renders the presentation.
  109. *
  110. * @return string A string representing the rendered presentation
  111. */
  112. public function render()
  113. {
  114. $content = null;
  115. if (sfConfig::get('sf_cache'))
  116. {
  117. $viewCache = $this->context->getViewCacheManager();
  118. $uri = $viewCache->getCurrentCacheKey();
  119. if (null !== $uri)
  120. {
  121. list($content, $decoratorTemplate) = $viewCache->getActionCache($uri);
  122. if (null !== $content)
  123. {
  124. $this->setDecoratorTemplate($decoratorTemplate);
  125. }
  126. }
  127. }
  128. // render template if no cache
  129. if (null === $content)
  130. {
  131. // execute pre-render check
  132. $this->preRenderCheck();
  133. $this->attributeHolder->set('sf_type', 'action');
  134. // render template file
  135. $content = $this->renderFile($this->getDirectory().'/'.$this->getTemplate());
  136. if (sfConfig::get('sf_cache') && null !== $uri)
  137. {
  138. $content = $viewCache->setActionCache($uri, $content, $this->isDecorator() ? $this->getDecoratorDirectory().'/'.$this->getDecoratorTemplate() : false);
  139. }
  140. }
  141. // now render decorator template, if one exists
  142. if ($this->isDecorator())
  143. {
  144. $content = $this->decorate($content);
  145. }
  146. return $content;
  147. }
  148. }

Debug toolbar