CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 1.3 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 1.3
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Classes

  • AclBase
  • AclBehavior
  • AclComponent
  • AclNode
  • AclShell
  • Aco
  • AcoAction
  • AjaxHelper
  • ApcEngine
  • ApiShell
  • App
  • AppController
  • AppHelper
  • AppModel
  • Aro
  • AuthComponent
  • BakeShell
  • BakeTask
  • BehaviorCollection
  • Cache
  • CacheEngine
  • CacheHelper
  • CakeErrorController
  • CakeLog
  • CakeRoute
  • CakeSchema
  • CakeSession
  • CakeSocket
  • ClassRegistry
  • Component
  • Configure
  • ConnectionManager
  • ConsoleShell
  • ContainableBehavior
  • Controller
  • ControllerTask
  • CookieComponent
  • DataSource
  • DbAcl
  • DbConfigTask
  • DboMssql
  • DboMysql
  • DboMysqlBase
  • DboMysqli
  • DboOracle
  • DboPostgres
  • DboSource
  • DboSqlite
  • Debugger
  • EmailComponent
  • ErrorHandler
  • ExtractTask
  • File
  • FileEngine
  • FileLog
  • FixtureTask
  • Folder
  • FormHelper
  • Helper
  • HtmlHelper
  • HttpSocket
  • I18n
  • I18nModel
  • I18nShell
  • Inflector
  • IniAcl
  • JavascriptHelper
  • JqueryEngineHelper
  • JsBaseEngineHelper
  • JsHelper
  • L10n
  • MagicDb
  • MagicFileResource
  • MediaView
  • MemcacheEngine
  • Model
  • ModelBehavior
  • ModelTask
  • MootoolsEngineHelper
  • Multibyte
  • NumberHelper
  • Object
  • Overloadable
  • Overloadable2
  • PagesController
  • PaginatorHelper
  • Permission
  • PluginShortRoute
  • PluginTask
  • ProjectTask
  • PrototypeEngineHelper
  • RequestHandlerComponent
  • Router
  • RssHelper
  • Sanitize
  • Scaffold
  • ScaffoldView
  • SchemaShell
  • Security
  • SecurityComponent
  • SessionComponent
  • SessionHelper
  • Set
  • Shell
  • String
  • TemplateTask
  • TestSuiteShell
  • TestTask
  • TextHelper
  • ThemeView
  • TimeHelper
  • TranslateBehavior
  • TreeBehavior
  • Validation
  • View
  • ViewTask
  • XcacheEngine
  • Xml
  • XmlElement
  • XmlHelper
  • XmlManager
  • XmlNode
  • XmlTextNode

Functions

  • mb_encode_mimeheader
  • mb_stripos
  • mb_stristr
  • mb_strlen
  • mb_strpos
  • mb_strrchr
  • mb_strrichr
  • mb_strripos
  • mb_strrpos
  • mb_strstr
  • mb_strtolower
  • mb_strtoupper
  • mb_substr
  • mb_substr_count
  1: <?php
  2: /**
  3:  * Methods for displaying presentation data in the view.
  4:  *
  5:  * PHP versions 4 and 5
  6:  *
  7:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9:  *
 10:  * Licensed under The MIT License
 11:  * Redistributions of files must retain the above copyright notice.
 12:  *
 13:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 14:  * @link          http://cakephp.org CakePHP(tm) Project
 15:  * @package       cake
 16:  * @subpackage    cake.cake.libs.view
 17:  * @since         CakePHP(tm) v 0.10.0.1076
 18:  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 19:  */
 20: 
 21: /**
 22:  * Included libraries.
 23:  */
 24: App::import('Core', 'ClassRegistry');
 25: App::import('View', 'Helper', false);
 26: 
 27: /**
 28:  * View, the V in the MVC triad.
 29:  *
 30:  * Class holding methods for displaying presentation data.
 31:  *
 32:  * @package       cake
 33:  * @subpackage    cake.cake.libs.view
 34:  */
 35: class View extends Object {
 36: 
 37: /**
 38:  * Path parts for creating links in views.
 39:  *
 40:  * @var string Base URL
 41:  * @access public
 42:  */
 43:     var $base = null;
 44: 
 45: /**
 46:  * Stores the current URL (for links etc.)
 47:  *
 48:  * @var string Current URL
 49:  */
 50:     var $here = null;
 51: 
 52: /**
 53:  * Name of the plugin.
 54:  *
 55:  * @link          http://manual.cakephp.org/chapter/plugins
 56:  * @var string
 57:  */
 58:     var $plugin = null;
 59: 
 60: /**
 61:  * Name of the controller.
 62:  *
 63:  * @var string Name of controller
 64:  * @access public
 65:  */
 66:     var $name = null;
 67: 
 68: /**
 69:  * Action to be performed.
 70:  *
 71:  * @var string Name of action
 72:  * @access public
 73:  */
 74:     var $action = null;
 75: 
 76: /**
 77:  * Array of parameter data
 78:  *
 79:  * @var array Parameter data
 80:  */
 81:     var $params = array();
 82: 
 83: /**
 84:  * Current passed params
 85:  *
 86:  * @var mixed
 87:  */
 88:     var $passedArgs = array();
 89: 
 90: /**
 91:  * Array of data
 92:  *
 93:  * @var array Parameter data
 94:  */
 95:     var $data = array();
 96: 
 97: /**
 98:  * An array of names of built-in helpers to include.
 99:  *
100:  * @var mixed A single name as a string or a list of names as an array.
101:  * @access public
102:  */
103:     var $helpers = array('Html');
104: 
105: /**
106:  * Path to View.
107:  *
108:  * @var string Path to View
109:  */
110:     var $viewPath = null;
111: 
112: /**
113:  * Variables for the view
114:  *
115:  * @var array
116:  * @access public
117:  */
118:     var $viewVars = array();
119: 
120: /**
121:  * Name of layout to use with this View.
122:  *
123:  * @var string
124:  * @access public
125:  */
126:     var $layout = 'default';
127: 
128: /**
129:  * Path to Layout.
130:  *
131:  * @var string Path to Layout
132:  */
133:     var $layoutPath = null;
134: 
135: /**
136:  * Turns on or off Cake's conventional mode of rendering views. On by default.
137:  *
138:  * @var boolean
139:  * @access public
140:  */
141:     var $autoRender = true;
142: 
143: /**
144:  * Turns on or off Cake's conventional mode of finding layout files. On by default.
145:  *
146:  * @var boolean
147:  * @access public
148:  */
149:     var $autoLayout = true;
150: 
151: /**
152:  * File extension. Defaults to Cake's template ".ctp".
153:  *
154:  * @var string
155:  * @access public
156:  */
157:     var $ext = '.ctp';
158: 
159: /**
160:  * Sub-directory for this view file.
161:  *
162:  * @var string
163:  * @access public
164:  */
165:     var $subDir = null;
166: 
167: /**
168:  * Theme name.
169:  *
170:  * @var string
171:  * @access public
172:  */
173:     var $theme = null;
174: 
175: /**
176:  * Used to define methods a controller that will be cached.
177:  *
178:  * @see Controller::$cacheAction
179:  * @var mixed
180:  * @access public
181:  */
182:     var $cacheAction = false;
183: 
184: /**
185:  * holds current errors for the model validation
186:  *
187:  * @var array
188:  * @access public
189:  */
190:     var $validationErrors = array();
191: 
192: /**
193:  * True when the view has been rendered.
194:  *
195:  * @var boolean
196:  * @access public
197:  */
198:     var $hasRendered = false;
199: 
200: /**
201:  * Array of loaded view helpers.
202:  *
203:  * @var array
204:  * @access public
205:  */
206:     var $loaded = array();
207: 
208: /**
209:  * True if in scope of model-specific region
210:  *
211:  * @var boolean
212:  * @access public
213:  */
214:     var $modelScope = false;
215: 
216: /**
217:  * Name of current model this view context is attached to
218:  *
219:  * @var string
220:  * @access public
221:  */
222:     var $model = null;
223: 
224: /**
225:  * Name of association model this view context is attached to
226:  *
227:  * @var string
228:  * @access public
229:  */
230:     var $association = null;
231: 
232: /**
233:  * Name of current model field this view context is attached to
234:  *
235:  * @var string
236:  * @access public
237:  */
238:     var $field = null;
239: 
240: /**
241:  * Suffix of current field this view context is attached to
242:  *
243:  * @var string
244:  * @access public
245:  */
246:     var $fieldSuffix = null;
247: 
248: /**
249:  * The current model ID this view context is attached to
250:  *
251:  * @var mixed
252:  * @access public
253:  */
254:     var $modelId = null;
255: 
256: /**
257:  * List of generated DOM UUIDs
258:  *
259:  * @var array
260:  * @access public
261:  */
262:     var $uuids = array();
263: 
264: /**
265:  * Holds View output.
266:  *
267:  * @var string
268:  * @access public
269:  */
270:     var $output = false;
271: 
272: /**
273:  * List of variables to collect from the associated controller
274:  *
275:  * @var array
276:  * @access protected
277:  */
278:     var $__passedVars = array(
279:         'viewVars', 'action', 'autoLayout', 'autoRender', 'ext', 'base', 'webroot',
280:         'helpers', 'here', 'layout', 'name', 'layoutPath', 'viewPath',
281:         'params', 'data', 'plugin', 'passedArgs', 'cacheAction'
282:     );
283: 
284: /**
285:  * Scripts (and/or other <head /> tags) for the layout
286:  *
287:  * @var array
288:  * @access private
289:  */
290:     var $__scripts = array();
291: 
292: /**
293:  * Holds an array of paths.
294:  *
295:  * @var array
296:  * @access private
297:  */
298:     var $__paths = array();
299: 
300: /**
301:  * Constructor
302:  *
303:  * @param Controller $controller A controller object to pull View::__passedArgs from.
304:  * @param boolean $register Should the View instance be registered in the ClassRegistry
305:  * @return View
306:  */
307:     function __construct(&$controller, $register = true) {
308:         if (is_object($controller)) {
309:             $count = count($this->__passedVars);
310:             for ($j = 0; $j < $count; $j++) {
311:                 $var = $this->__passedVars[$j];
312:                 $this->{$var} = $controller->{$var};
313:             }
314:         }
315:         parent::__construct();
316: 
317:         if ($register) {
318:             ClassRegistry::addObject('view', $this);
319:         }
320:     }
321: 
322: /**
323:  * Renders a piece of PHP with provided parameters and returns HTML, XML, or any other string.
324:  *
325:  * This realizes the concept of Elements, (or "partial layouts")
326:  * and the $params array is used to send data to be used in the
327:  * Element.  Elements can be cached through use of the cache key.
328:  *
329:  * ### Special params
330:  *
331:  * - `cache` - enable caching for this element accepts boolean or strtotime compatible string.
332:  *   Can also be an array. If `cache` is an array,
333:  *   `time` is used to specify duration of cache.
334:  *   `key` can be used to create unique cache files.
335:  * - `plugin` - Load an element from a specific plugin.
336:  *
337:  * @param string $name Name of template file in the/app/views/elements/ folder
338:  * @param array $params Array of data to be made available to the for rendered
339:  *    view (i.e. the Element)
340:  * @return string Rendered Element
341:  * @access public
342:  */
343:     function element($name, $params = array(), $loadHelpers = false) {
344:         $file = $plugin = $key = null;
345: 
346:         if (isset($params['plugin'])) {
347:             $plugin = $params['plugin'];
348:         }
349: 
350:         if (isset($this->plugin) && !$plugin) {
351:             $plugin = $this->plugin;
352:         }
353: 
354:         if (isset($params['cache'])) {
355:             $expires = '+1 day';
356: 
357:             if (is_array($params['cache'])) {
358:                 $expires = $params['cache']['time'];
359:                 $key = Inflector::slug($params['cache']['key']);
360:             } else {
361:                 $key = implode('_', array_keys($params));
362:                 if ($params['cache'] !== true) {
363:                     $expires = $params['cache'];
364:                 }
365:             }
366: 
367:             if ($expires) {
368:                 $cacheFile = 'element_' . $key . '_' . $plugin . Inflector::slug($name);
369:                 $cache = cache('views' . DS . $cacheFile, null, $expires);
370: 
371:                 if (is_string($cache)) {
372:                     return $cache;
373:                 }
374:             }
375:         }
376:         $paths = $this->_paths($plugin);
377:         $exts = $this->_getExtensions();
378:         foreach ($exts as $ext) {
379:             foreach ($paths as $path) {
380:                 if (file_exists($path . 'elements' . DS . $name . $ext)) {
381:                     $file = $path . 'elements' . DS . $name . $ext;
382:                     break;
383:                 }
384:             }
385:             if ($file) {
386:                 break;
387:             }
388:         }
389: 
390:         if (is_file($file)) {
391:             $vars = array_merge($this->viewVars, $params);
392:             foreach ($this->loaded as $name => $helper) {
393:                 if (!isset($vars[$name])) {
394:                     $vars[$name] =& $this->loaded[$name];
395:                 }
396:             }
397:             $element = $this->_render($file, $vars, $loadHelpers);
398:             if (isset($params['cache']) && isset($cacheFile) && isset($expires)) {
399:                 cache('views' . DS . $cacheFile, $element, $expires);
400:             }
401:             return $element;
402:         }
403:         $file = $paths[0] . 'elements' . DS . $name . $this->ext;
404: 
405:         if (Configure::read() > 0) {
406:             return "Not Found: " . $file;
407:         }
408:     }
409: 
410: /**
411:  * Renders view for given action and layout. If $file is given, that is used
412:  * for a view filename (e.g. customFunkyView.ctp).
413:  *
414:  * @param string $action Name of action to render for
415:  * @param string $layout Layout to use
416:  * @param string $file Custom filename for view
417:  * @return string Rendered Element
418:  * @access public
419:  */
420:     function render($action = null, $layout = null, $file = null) {
421:         if ($this->hasRendered) {
422:             return true;
423:         }
424:         $out = null;
425: 
426:         if ($file != null) {
427:             $action = $file;
428:         }
429: 
430:         if ($action !== false && $viewFileName = $this->_getViewFileName($action)) {
431:             $out = $this->_render($viewFileName, $this->viewVars);
432:         }
433: 
434:         if ($layout === null) {
435:             $layout = $this->layout;
436:         }
437: 
438:         if ($out !== false) {
439:             if ($layout && $this->autoLayout) {
440:                 $out = $this->renderLayout($out, $layout);
441:                 $isCached = (
442:                     isset($this->loaded['cache']) ||
443:                     Configure::read('Cache.check') === true
444:                 );
445: 
446:                 if ($isCached) {
447:                     $replace = array('<cake:nocache>', '</cake:nocache>');
448:                     $out = str_replace($replace, '', $out);
449:                 }
450:             }
451:             $this->hasRendered = true;
452:         } else {
453:             $out = $this->_render($viewFileName, $this->viewVars);
454:             trigger_error(sprintf(__("Error in view %s, got: <blockquote>%s</blockquote>", true), $viewFileName, $out), E_USER_ERROR);
455:         }
456:         return $out;
457:     }
458: 
459: /**
460:  * Renders a layout. Returns output from _render(). Returns false on error.
461:  * Several variables are created for use in layout.
462:  *
463:  * - `title_for_layout` - A backwards compatible place holder, you should set this value if you want more control.
464:  * - `content_for_layout` - contains rendered view file
465:  * - `scripts_for_layout` - contains scripts added to header
466:  *
467:  * @param string $content_for_layout Content to render in a view, wrapped by the surrounding layout.
468:  * @return mixed Rendered output, or false on error
469:  * @access public
470:  */
471:     function renderLayout($content_for_layout, $layout = null) {
472:         $layoutFileName = $this->_getLayoutFileName($layout);
473:         if (empty($layoutFileName)) {
474:             return $this->output;
475:         }
476: 
477:         $dataForLayout = array_merge($this->viewVars, array(
478:             'content_for_layout' => $content_for_layout,
479:             'scripts_for_layout' => implode("\n\t", $this->__scripts),
480:         ));
481: 
482:         if (!isset($dataForLayout['title_for_layout'])) {
483:             $dataForLayout['title_for_layout'] = Inflector::humanize($this->viewPath);
484:         }
485: 
486:         if (empty($this->loaded) && !empty($this->helpers)) {
487:             $loadHelpers = true;
488:         } else {
489:             $loadHelpers = false;
490:             $dataForLayout = array_merge($dataForLayout, $this->loaded);
491:         }
492: 
493:         $this->_triggerHelpers('beforeLayout');
494:         $this->output = $this->_render($layoutFileName, $dataForLayout, $loadHelpers, true);
495: 
496:         if ($this->output === false) {
497:             $this->output = $this->_render($layoutFileName, $dataForLayout);
498:             trigger_error(sprintf(__("Error in layout %s, got: <blockquote>%s</blockquote>", true), $layoutFileName, $this->output), E_USER_ERROR);
499:             return false;
500:         }
501: 
502:         $this->_triggerHelpers('afterLayout');
503: 
504:         return $this->output;
505:     }
506: 
507: /**
508:  * Fire a callback on all loaded Helpers. All helpers must implement this method,
509:  * it is not checked before being called.  You can add additional helper callbacks in AppHelper.
510:  *
511:  * @param string $callback name of callback fire.
512:  * @access protected
513:  * @return void
514:  */
515:     function _triggerHelpers($callback) {
516:         if (empty($this->loaded)) {
517:             return false;
518:         }
519:         $helpers = array_keys($this->loaded);
520:         foreach ($helpers as $helperName) {
521:             $helper =& $this->loaded[$helperName];
522:             if (is_object($helper)) {
523:                 if (is_subclass_of($helper, 'Helper')) {
524:                     $helper->{$callback}();
525:                 }
526:             }
527:         }
528:     }
529: 
530: /**
531:  * Render cached view. Works in concert with CacheHelper and Dispatcher to
532:  * render cached view files.
533:  *
534:  * @param string $filename the cache file to include
535:  * @param string $timeStart the page render start time
536:  * @return boolean Success of rendering the cached file.
537:  * @access public
538:  */
539:     function renderCache($filename, $timeStart) {
540:         ob_start();
541:         include ($filename);
542: 
543:         if (Configure::read() > 0 && $this->layout != 'xml') {
544:             echo "<!-- Cached Render Time: " . round(getMicrotime() - $timeStart, 4) . "s -->";
545:         }
546:         $out = ob_get_clean();
547: 
548:         if (preg_match('/^<!--cachetime:(\\d+)-->/', $out, $match)) {
549:             if (time() >= $match['1']) {
550:                 @unlink($filename);
551:                 unset ($out);
552:                 return false;
553:             } else {
554:                 if ($this->layout === 'xml') {
555:                     header('Content-type: text/xml');
556:                 }
557:                 $commentLength = strlen('<!--cachetime:' . $match['1'] . '-->');
558:                 echo substr($out, $commentLength);
559:                 return true;
560:             }
561:         }
562:     }
563: 
564: /**
565:  * Returns a list of variables available in the current View context
566:  *
567:  * @return array Array of the set view variable names.
568:  * @access public
569:  */
570:     function getVars() {
571:         return array_keys($this->viewVars);
572:     }
573: 
574: /**
575:  * Returns the contents of the given View variable(s)
576:  *
577:  * @param string $var The view var you want the contents of.
578:  * @return mixed The content of the named var if its set, otherwise null.
579:  * @access public
580:  */
581:     function getVar($var) {
582:         if (!isset($this->viewVars[$var])) {
583:             return null;
584:         } else {
585:             return $this->viewVars[$var];
586:         }
587:     }
588: 
589: /**
590:  * Adds a script block or other element to be inserted in $scripts_for_layout in
591:  * the `<head />` of a document layout
592:  *
593:  * @param string $name Either the key name for the script, or the script content. Name can be used to
594:  *   update/replace a script element.
595:  * @param string $content The content of the script being added, optional.
596:  * @return void
597:  * @access public
598:  */
599:     function addScript($name, $content = null) {
600:         if (empty($content)) {
601:             if (!in_array($name, array_values($this->__scripts))) {
602:                 $this->__scripts[] = $name;
603:             }
604:         } else {
605:             $this->__scripts[$name] = $content;
606:         }
607:     }
608: 
609: /**
610:  * Generates a unique, non-random DOM ID for an object, based on the object type and the target URL.
611:  *
612:  * @param string $object Type of object, i.e. 'form' or 'link'
613:  * @param string $url The object's target URL
614:  * @return string
615:  * @access public
616:  */
617:     function uuid($object, $url) {
618:         $c = 1;
619:         $url = Router::url($url);
620:         $hash = $object . substr(md5($object . $url), 0, 10);
621:         while (in_array($hash, $this->uuids)) {
622:             $hash = $object . substr(md5($object . $url . $c), 0, 10);
623:             $c++;
624:         }
625:         $this->uuids[] = $hash;
626:         return $hash;
627:     }
628: 
629: /**
630:  * Returns the entity reference of the current context as an array of identity parts
631:  *
632:  * @return array An array containing the identity elements of an entity
633:  * @access public
634:  */
635:     function entity() {
636:         $assoc = ($this->association) ? $this->association : $this->model;
637:         if (!empty($this->entityPath)) {
638:             $path = explode('.', $this->entityPath);
639:             $count = count($path);
640:             if (
641:                 ($count == 1 && !empty($this->association)) ||
642:                 ($count == 1 && $this->model != $this->entityPath) ||
643:                 ($count == 1 && empty($this->association) && !empty($this->field)) ||
644:                 ($count  == 2 && !empty($this->fieldSuffix)) ||
645:                 is_numeric($path[0]) && !empty($assoc)
646:             ) {
647:                 array_unshift($path, $assoc);
648:             }
649:             return Set::filter($path);
650:         }
651:         return array_values(Set::filter(
652:             array($assoc, $this->modelId, $this->field, $this->fieldSuffix)
653:         ));
654:     }
655: 
656: /**
657:  * Allows a template or element to set a variable that will be available in
658:  * a layout or other element. Analagous to Controller::set.
659:  *
660:  * @param mixed $one A string or an array of data.
661:  * @param mixed $two Value in case $one is a string (which then works as the key).
662:  *    Unused if $one is an associative array, otherwise serves as the values to $one's keys.
663:  * @return void
664:  * @access public
665:  */
666:     function set($one, $two = null) {
667:         $data = null;
668:         if (is_array($one)) {
669:             if (is_array($two)) {
670:                 $data = array_combine($one, $two);
671:             } else {
672:                 $data = $one;
673:             }
674:         } else {
675:             $data = array($one => $two);
676:         }
677:         if ($data == null) {
678:             return false;
679:         }
680:         $this->viewVars = $data + $this->viewVars;
681:     }
682: 
683: /**
684:  * Displays an error page to the user. Uses layouts/error.ctp to render the page.
685:  *
686:  * @param integer $code HTTP Error code (for instance: 404)
687:  * @param string $name Name of the error (for instance: Not Found)
688:  * @param string $message Error message as a web page
689:  * @access public
690:  */
691:     function error($code, $name, $message) {
692:         header ("HTTP/1.1 {$code} {$name}");
693:         print ($this->_render(
694:             $this->_getLayoutFileName('error'),
695:             array('code' => $code, 'name' => $name, 'message' => $message)
696:         ));
697:     }
698: 
699: /**
700:  * Renders and returns output for given view filename with its
701:  * array of data.
702:  *
703:  * @param string $___viewFn Filename of the view
704:  * @param array $___dataForView Data to include in rendered view
705:  * @param boolean $loadHelpers Boolean to indicate that helpers should be loaded.
706:  * @param boolean $cached Whether or not to trigger the creation of a cache file.
707:  * @return string Rendered output
708:  * @access protected
709:  */
710:     function _render($___viewFn, $___dataForView, $loadHelpers = true, $cached = false) {
711:         $loadedHelpers = array();
712: 
713:         if ($this->helpers != false && $loadHelpers === true) {
714:             $loadedHelpers = $this->_loadHelpers($loadedHelpers, $this->helpers);
715:             $helpers = array_keys($loadedHelpers);
716:             $helperNames = array_map(array('Inflector', 'variable'), $helpers);
717: 
718:             for ($i = count($helpers) - 1; $i >= 0; $i--) {
719:                 $name = $helperNames[$i];
720:                 $helper =& $loadedHelpers[$helpers[$i]];
721: 
722:                 if (!isset($___dataForView[$name])) {
723:                     ${$name} =& $helper;
724:                 }
725:                 $this->loaded[$helperNames[$i]] =& $helper;
726:                 $this->{$helpers[$i]} =& $helper;
727:             }
728:             $this->_triggerHelpers('beforeRender');
729:             unset($name, $loadedHelpers, $helpers, $i, $helperNames, $helper);
730:         }
731: 
732:         extract($___dataForView, EXTR_SKIP);
733:         ob_start();
734: 
735:         if (Configure::read() > 0) {
736:             include ($___viewFn);
737:         } else {
738:             @include ($___viewFn);
739:         }
740: 
741:         if ($loadHelpers === true) {
742:             $this->_triggerHelpers('afterRender');
743:         }
744: 
745:         $out = ob_get_clean();
746:         $caching = (
747:             isset($this->loaded['cache']) &&
748:             (($this->cacheAction != false)) && (Configure::read('Cache.check') === true)
749:         );
750: 
751:         if ($caching) {
752:             if (is_a($this->loaded['cache'], 'CacheHelper')) {
753:                 $cache =& $this->loaded['cache'];
754:                 $cache->base = $this->base;
755:                 $cache->here = $this->here;
756:                 $cache->helpers = $this->helpers;
757:                 $cache->action = $this->action;
758:                 $cache->controllerName = $this->name;
759:                 $cache->layout = $this->layout;
760:                 $cache->cacheAction = $this->cacheAction;
761:                 $cache->viewVars = $this->viewVars;
762:                 $out = $cache->cache($___viewFn, $out, $cached);
763:             }
764:         }
765:         return $out;
766:     }
767: 
768: /**
769:  * Loads helpers, with their dependencies.
770:  *
771:  * @param array $loaded List of helpers that are already loaded.
772:  * @param array $helpers List of helpers to load.
773:  * @param string $parent holds name of helper, if loaded helper has helpers
774:  * @return array Array containing the loaded helpers.
775:  * @access protected
776:  */
777:     function &_loadHelpers(&$loaded, $helpers, $parent = null) {
778:         foreach ($helpers as $i => $helper) {
779:             $options = array();
780: 
781:             if (!is_int($i)) {
782:                 $options = $helper;
783:                 $helper = $i;
784:             }
785:             list($plugin, $helper) = pluginSplit($helper, true, $this->plugin);
786:             $helperCn = $helper . 'Helper';
787: 
788:             if (!isset($loaded[$helper])) {
789:                 if (!class_exists($helperCn)) {
790:                     $isLoaded = false;
791:                     if (!is_null($plugin)) {
792:                         $isLoaded = App::import('Helper', $plugin . $helper);
793:                     }
794:                     if (!$isLoaded) {
795:                         if (!App::import('Helper', $helper)) {
796:                             $this->cakeError('missingHelperFile', array(array(
797:                                 'helper' => $helper,
798:                                 'file' => Inflector::underscore($helper) . '.php',
799:                                 'base' => $this->base
800:                             )));
801:                             return false;
802:                         }
803:                     }
804:                     if (!class_exists($helperCn)) {
805:                         $this->cakeError('missingHelperClass', array(array(
806:                             'helper' => $helper,
807:                             'file' => Inflector::underscore($helper) . '.php',
808:                             'base' => $this->base
809:                         )));
810:                         return false;
811:                     }
812:                 }
813:                 $loaded[$helper] =& new $helperCn($options);
814:                 $vars = array('base', 'webroot', 'here', 'params', 'action', 'data', 'theme', 'plugin');
815:                 $c = count($vars);
816: 
817:                 for ($j = 0; $j < $c; $j++) {
818:                     $loaded[$helper]->{$vars[$j]} = $this->{$vars[$j]};
819:                 }
820: 
821:                 if (!empty($this->validationErrors)) {
822:                     $loaded[$helper]->validationErrors = $this->validationErrors;
823:                 }
824:                 if (is_array($loaded[$helper]->helpers) && !empty($loaded[$helper]->helpers)) {
825:                     $loaded =& $this->_loadHelpers($loaded, $loaded[$helper]->helpers, $helper);
826:                 }
827:             }
828:             if (isset($loaded[$parent])) {
829:                 $loaded[$parent]->{$helper} =& $loaded[$helper];
830:             }
831:         }
832:         return $loaded;
833:     }
834: 
835: /**
836:  * Returns filename of given action's template file (.ctp) as a string.
837:  * CamelCased action names will be under_scored! This means that you can have
838:  * LongActionNames that refer to long_action_names.ctp views.
839:  *
840:  * @param string $name Controller action to find template filename for
841:  * @return string Template filename
842:  * @access protected
843:  */
844:     function _getViewFileName($name = null) {
845:         $subDir = null;
846: 
847:         if (!is_null($this->subDir)) {
848:             $subDir = $this->subDir . DS;
849:         }
850: 
851:         if ($name === null) {
852:             $name = $this->action;
853:         }
854:         $name = str_replace('/', DS, $name);
855: 
856:         if (strpos($name, DS) === false && $name[0] !== '.') {
857:             $name = $this->viewPath . DS . $subDir . Inflector::underscore($name);
858:         } elseif (strpos($name, DS) !== false) {
859:             if ($name{0} === DS || $name{1} === ':') {
860:                 $name = trim($name, DS);
861:             } else if ($name[0] === '.') {
862:                 $name = substr($name, 3);
863:             } else {
864:                 $name = $this->viewPath . DS . $subDir . $name;
865:             }
866:         }
867:         $paths = $this->_paths(Inflector::underscore($this->plugin));
868: 
869:         $exts = $this->_getExtensions();
870:         foreach ($exts as $ext) {
871:             foreach ($paths as $path) {
872:                 if (file_exists($path . $name . $ext)) {
873:                     return $path . $name . $ext;
874:                 }
875:             }
876:         }
877:         $defaultPath = $paths[0];
878: 
879:         if ($this->plugin) {
880:             $pluginPaths = App::path('plugins');
881:             foreach ($paths as $path) {
882:                 if (strpos($path, $pluginPaths[0]) === 0) {
883:                     $defaultPath = $path;
884:                     break;
885:                 }
886:             }
887:         }
888:         return $this->_missingView($defaultPath . $name . $this->ext, 'missingView');
889:     }
890: 
891: /**
892:  * Returns layout filename for this template as a string.
893:  *
894:  * @param string $name The name of the layout to find.
895:  * @return string Filename for layout file (.ctp).
896:  * @access protected
897:  */
898:     function _getLayoutFileName($name = null) {
899:         if ($name === null) {
900:             $name = $this->layout;
901:         }
902:         $subDir = null;
903: 
904:         if (!is_null($this->layoutPath)) {
905:             $subDir = $this->layoutPath . DS;
906:         }
907:         $paths = $this->_paths(Inflector::underscore($this->plugin));
908:         $file = 'layouts' . DS . $subDir . $name;
909: 
910:         $exts = $this->_getExtensions();
911:         foreach ($exts as $ext) {
912:             foreach ($paths as $path) {
913:                 if (file_exists($path . $file . $ext)) {
914:                     return $path . $file . $ext;
915:                 }
916:             }
917:         }
918:         return $this->_missingView($paths[0] . $file . $this->ext, 'missingLayout');
919:     }
920: 
921: 
922: /**
923:  * Get the extensions that view files can use.
924:  *
925:  * @return array Array of extensions view files use.
926:  * @access protected
927:  */
928:     function _getExtensions() {
929:         $exts = array($this->ext);
930:         if ($this->ext !== '.ctp') {
931:             array_push($exts, '.ctp');
932:         }
933:         return $exts;
934:     }
935: 
936: /**
937:  * Return a misssing view error message
938:  *
939:  * @param string $viewFileName the filename that should exist
940:  * @return false
941:  * @access protected
942:  */
943:     function _missingView($file, $error = 'missingView') {
944:         if ($error === 'missingView') {
945:             $this->cakeError('missingView', array(
946:                 'className' => $this->name,
947:                 'action' => $this->action,
948:                 'file' => $file,
949:                 'base' => $this->base
950:             ));
951:             return false;
952:         } elseif ($error === 'missingLayout') {
953:             $this->cakeError('missingLayout', array(
954:                 'layout' => $this->layout,
955:                 'file' => $file,
956:                 'base' => $this->base
957:             ));
958:             return false;
959:         }
960:     }
961: 
962: /**
963:  * Return all possible paths to find view files in order
964:  *
965:  * @param string $plugin Optional plugin name to scan for view files.
966:  * @param boolean $cached Set to true to force a refresh of view paths.
967:  * @return array paths
968:  * @access protected
969:  */
970:     function _paths($plugin = null, $cached = true) {
971:         if ($plugin === null && $cached === true && !empty($this->__paths)) {
972:             return $this->__paths;
973:         }
974:         $paths = array();
975:         $viewPaths = App::path('views');
976:         $corePaths = array_flip(App::core('views'));
977: 
978:         if (!empty($plugin)) {
979:             $count = count($viewPaths);
980:             for ($i = 0; $i < $count; $i++) {
981:                 if (!isset($corePaths[$viewPaths[$i]])) {
982:                     $paths[] = $viewPaths[$i] . 'plugins' . DS . $plugin . DS;
983:                 }
984:             }
985:             $paths[] = App::pluginPath($plugin) . 'views' . DS;
986:         }
987:         $this->__paths = array_merge($paths, $viewPaths);
988:         return $this->__paths;
989:     }
990: }
991: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs