Development

/branches/1.3/lib/view/sfPHPView.class.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/view/sfPHPView.class.php

Revision 28713, 5.3 kB (checked in by fabien, 4 years ago)

[1.3, 1.4] fixed layout.php is required even if you use decorate_with() to use another layout (closes #8441)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  * (c) 2004-2006 Sean Kerr <sean@code-box.org>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 /**
13  * A view that uses PHP as the templating engine.
14  *
15  * @package    symfony
16  * @subpackage view
17  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
18  * @author     Sean Kerr <sean@code-box.org>
19  * @version    SVN: $Id$
20  */
21 class sfPHPView extends sfView
22 {
23   /**
24    * Executes any presentation logic for this view.
25    */
26   public function execute()
27   {
28   }
29
30   /**
31    * Loads core and standard helpers to be use in the template.
32    */
33   protected function loadCoreAndStandardHelpers()
34   {
35     static $coreHelpersLoaded = 0;
36
37     if ($coreHelpersLoaded)
38     {
39       return;
40     }
41
42     $coreHelpersLoaded = 1;
43
44     $helpers = array_unique(array_merge(array('Helper', 'Url', 'Asset', 'Tag', 'Escaping'), sfConfig::get('sf_standard_helpers')));
45
46     // remove default Form helper if compat_10 is false
47     if (!sfConfig::get('sf_compat_10') && false !== $i = array_search('Form', $helpers))
48     {
49       unset($helpers[$i]);
50     }
51
52     $this->context->getConfiguration()->loadHelpers($helpers);
53   }
54
55   /**
56    * Renders the presentation.
57    *
58    * @param  string $_sfFile  Filename
59    *
60    * @return string File content
61    */
62   protected function renderFile($_sfFile)
63   {
64     if (sfConfig::get('sf_logging_enabled'))
65     {
66       $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Render "%s"', $_sfFile))));
67     }
68
69     $this->loadCoreAndStandardHelpers();
70
71     // EXTR_REFS can't be used (see #3595 and #3151)
72     $vars = $this->attributeHolder->toArray();
73     extract($vars);
74
75     // render
76     ob_start();
77     ob_implicit_flush(0);
78
79     try
80     {
81       require($_sfFile);
82     }
83     catch (Exception $e)
84     {
85       // need to end output buffering before throwing the exception #7596
86       ob_end_clean();
87       throw $e;
88     }
89
90     return ob_get_clean();
91   }
92
93   /**
94    * Retrieves the template engine associated with this view.
95    *
96    * Note: This will return null because PHP itself has no engine reference.
97    *
98    * @return null
99    */
100   public function getEngine()
101   {
102     return null;
103   }
104
105   /**
106    * Configures template.
107    *
108    * @return void
109    */
110   public function configure()
111   {
112     // store our current view
113     $this->context->set('view_instance', $this);
114
115     // require our configuration
116     require($this->context->getConfigCache()->checkConfig('modules/'.$this->moduleName.'/config/view.yml'));
117
118     // set template directory
119     if (!$this->directory)
120     {
121       $this->setDirectory($this->context->getConfiguration()->getTemplateDir($this->moduleName, $this->getTemplate()));
122     }
123   }
124
125   /**
126    * Loop through all template slots and fill them in with the results of presentation data.
127    *
128    * @param  string $content  A chunk of decorator content
129    *
130    * @return string A decorated template
131    */
132   protected function decorate($content)
133   {
134     if (sfConfig::get('sf_logging_enabled'))
135     {
136       $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Decorate content with "%s/%s"', $this->getDecoratorDirectory(), $this->getDecoratorTemplate()))));
137     }
138
139     // set the decorator content as an attribute
140     $attributeHolder = $this->attributeHolder;
141
142     $this->attributeHolder = $this->initializeAttributeHolder(array('sf_content' => new sfOutputEscaperSafe($content)));
143     $this->attributeHolder->set('sf_type', 'layout');
144
145     // check to see if the decorator template exists
146     if (!is_readable($this->getDecoratorDirectory().'/'.$this->getDecoratorTemplate()))
147     {
148       throw new sfRenderException(sprintf('The decorator template "%s" does not exist or is unreadable in "%s".', $this->decoratorTemplate, $this->decoratorDirectory));
149     }
150
151     // render the decorator template and return the result
152     $ret = $this->renderFile($this->getDecoratorDirectory().'/'.$this->getDecoratorTemplate());
153
154     $this->attributeHolder = $attributeHolder;
155
156     return $ret;
157   }
158
159   /**
160    * Renders the presentation.
161    *
162    * @return string A string representing the rendered presentation
163    */
164   public function render()
165   {
166     $content = null;
167     if (sfConfig::get('sf_cache'))
168     {
169       $viewCache = $this->context->getViewCacheManager();
170       $uri = $viewCache->getCurrentCacheKey();
171
172       if (null !== $uri)
173       {
174         list($content, $decoratorTemplate) = $viewCache->getActionCache($uri);
175         if (null !== $content)
176         {
177           $this->setDecoratorTemplate($decoratorTemplate);
178         }
179       }
180     }
181
182     // render template if no cache
183     if (null === $content)
184     {
185       // execute pre-render check
186       $this->preRenderCheck();
187
188       $this->attributeHolder->set('sf_type', 'action');
189
190       // render template file
191       $content = $this->renderFile($this->getDirectory().'/'.$this->getTemplate());
192
193       if (sfConfig::get('sf_cache') && null !== $uri)
194       {
195         $content = $viewCache->setActionCache($uri, $content, $this->isDecorator() ? $this->getDecoratorDirectory().'/'.$this->getDecoratorTemplate() : false);
196       }
197     }
198
199     // now render decorator template, if one exists
200     if ($this->isDecorator())
201     {
202       $content = $this->decorate($content);
203     }
204
205     return $content;
206   }
207 }
208
Note: See TracBrowser for help on using the browser.