Development

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

You must first sign up to be able to contribute.

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

Revision 28713, 5.1 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     $this->context->getConfiguration()->loadHelpers($helpers);
47   }
48
49   /**
50    * Renders the presentation.
51    *
52    * @param  string $_sfFile  Filename
53    *
54    * @return string File content
55    */
56   protected function renderFile($_sfFile)
57   {
58     if (sfConfig::get('sf_logging_enabled'))
59     {
60       $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Render "%s"', $_sfFile))));
61     }
62
63     $this->loadCoreAndStandardHelpers();
64
65     // EXTR_REFS can't be used (see #3595 and #3151)
66     $vars = $this->attributeHolder->toArray();
67     extract($vars);
68
69     // render
70     ob_start();
71     ob_implicit_flush(0);
72
73     try
74     {
75       require($_sfFile);
76     }
77     catch (Exception $e)
78     {
79       // need to end output buffering before throwing the exception #7596
80       ob_end_clean();
81       throw $e;
82     }
83
84     return ob_get_clean();
85   }
86
87   /**
88    * Retrieves the template engine associated with this view.
89    *
90    * Note: This will return null because PHP itself has no engine reference.
91    *
92    * @return null
93    */
94   public function getEngine()
95   {
96     return null;
97   }
98
99   /**
100    * Configures template.
101    *
102    * @return void
103    */
104   public function configure()
105   {
106     // store our current view
107     $this->context->set('view_instance', $this);
108
109     // require our configuration
110     require($this->context->getConfigCache()->checkConfig('modules/'.$this->moduleName.'/config/view.yml'));
111
112     // set template directory
113     if (!$this->directory)
114     {
115       $this->setDirectory($this->context->getConfiguration()->getTemplateDir($this->moduleName, $this->getTemplate()));
116     }
117   }
118
119   /**
120    * Loop through all template slots and fill them in with the results of presentation data.
121    *
122    * @param  string $content  A chunk of decorator content
123    *
124    * @return string A decorated template
125    */
126   protected function decorate($content)
127   {
128     if (sfConfig::get('sf_logging_enabled'))
129     {
130       $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Decorate content with "%s/%s"', $this->getDecoratorDirectory(), $this->getDecoratorTemplate()))));
131     }
132
133     // set the decorator content as an attribute
134     $attributeHolder = $this->attributeHolder;
135
136     $this->attributeHolder = $this->initializeAttributeHolder(array('sf_content' => new sfOutputEscaperSafe($content)));
137     $this->attributeHolder->set('sf_type', 'layout');
138
139     // check to see if the decorator template exists
140     if (!is_readable($this->getDecoratorDirectory().'/'.$this->getDecoratorTemplate()))
141     {
142       throw new sfRenderException(sprintf('The decorator template "%s" does not exist or is unreadable in "%s".', $this->decoratorTemplate, $this->decoratorDirectory));
143     }
144
145     // render the decorator template and return the result
146     $ret = $this->renderFile($this->getDecoratorDirectory().'/'.$this->getDecoratorTemplate());
147
148     $this->attributeHolder = $attributeHolder;
149
150     return $ret;
151   }
152
153   /**
154    * Renders the presentation.
155    *
156    * @return string A string representing the rendered presentation
157    */
158   public function render()
159   {
160     $content = null;
161     if (sfConfig::get('sf_cache'))
162     {
163       $viewCache = $this->context->getViewCacheManager();
164       $uri = $viewCache->getCurrentCacheKey();
165
166       if (null !== $uri)
167       {
168         list($content, $decoratorTemplate) = $viewCache->getActionCache($uri);
169         if (null !== $content)
170         {
171           $this->setDecoratorTemplate($decoratorTemplate);
172         }
173       }
174     }
175
176     // render template if no cache
177     if (null === $content)
178     {
179       // execute pre-render check
180       $this->preRenderCheck();
181
182       $this->attributeHolder->set('sf_type', 'action');
183
184       // render template file
185       $content = $this->renderFile($this->getDirectory().'/'.$this->getTemplate());
186
187       if (sfConfig::get('sf_cache') && null !== $uri)
188       {
189         $content = $viewCache->setActionCache($uri, $content, $this->isDecorator() ? $this->getDecoratorDirectory().'/'.$this->getDecoratorTemplate() : false);
190       }
191     }
192
193     // now render decorator template, if one exists
194     if ($this->isDecorator())
195     {
196       $content = $this->decorate($content);
197     }
198
199     return $content;
200   }
201 }
202
Note: See TracBrowser for help on using the browser.