/
View.php
1560 lines (1384 loc) · 46.4 KB
/
View.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 0.10.0
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\View;
use Cake\Cache\Cache;
use Cake\Core\App;
use Cake\Core\Plugin;
use Cake\Event\EventDispatcherInterface;
use Cake\Event\EventDispatcherTrait;
use Cake\Event\EventManager;
use Cake\Http\Response;
use Cake\Http\ServerRequest;
use Cake\Log\LogTrait;
use Cake\Routing\RequestActionTrait;
use Cake\Routing\Router;
use Cake\Utility\Inflector;
use Cake\View\Exception\MissingElementException;
use Cake\View\Exception\MissingLayoutException;
use Cake\View\Exception\MissingTemplateException;
use InvalidArgumentException;
use LogicException;
use RuntimeException;
/**
* View, the V in the MVC triad. View interacts with Helpers and view variables passed
* in from the controller to render the results of the controller action. Often this is HTML,
* but can also take the form of JSON, XML, PDF's or streaming files.
*
* CakePHP uses a two-step-view pattern. This means that the template content is rendered first,
* and then inserted into the selected layout. This also means you can pass data from the template to the
* layout using `$this->set()`
*
* View class supports using plugins as themes. You can set
*
* ```
* public function beforeRender(\Cake\Event\Event $event)
* {
* $this->viewBuilder()->setTheme('SuperHot');
* }
* ```
*
* in your Controller to use plugin `SuperHot` as a theme. Eg. If current action
* is PostsController::index() then View class will look for template file
* `plugins/SuperHot/Template/Posts/index.ctp`. If a theme template
* is not found for the current action the default app template file is used.
*
* @property \Cake\View\Helper\FlashHelper $Flash
* @property \Cake\View\Helper\FormHelper $Form
* @property \Cake\View\Helper\HtmlHelper $Html
* @property \Cake\View\Helper\NumberHelper $Number
* @property \Cake\View\Helper\PaginatorHelper $Paginator
* @property \Cake\View\Helper\RssHelper $Rss
* @property \Cake\View\Helper\SessionHelper $Session
* @property \Cake\View\Helper\TextHelper $Text
* @property \Cake\View\Helper\TimeHelper $Time
* @property \Cake\View\Helper\UrlHelper $Url
* @property \Cake\View\ViewBlock $Blocks
* @property string $view
* @property string $viewPath
*/
class View implements EventDispatcherInterface
{
use CellTrait {
cell as public;
}
use EventDispatcherTrait;
use LogTrait;
use RequestActionTrait;
use ViewVarsTrait;
/**
* Helpers collection
*
* @var \Cake\View\HelperRegistry
*/
protected $_helpers;
/**
* ViewBlock instance.
*
* @var \Cake\View\ViewBlock
*/
public $Blocks;
/**
* The name of the plugin.
*
* @var string
*/
public $plugin;
/**
* Name of the controller that created the View if any.
*
* @var string
*/
public $name;
/**
* Current passed params. Passed to View from the creating Controller for convenience.
*
* @var array
* @deprecated 3.1.0 Use `$this->request->getParam('pass')` instead.
*/
public $passedArgs = [];
/**
* An array of names of built-in helpers to include.
*
* @var array
*/
public $helpers = [];
/**
* The name of the subfolder containing templates for this View.
*
* @var string
*/
public $templatePath;
/**
* The name of the template file to render. The name specified
* is the filename in /src/Template/<SubFolder> without the .ctp extension.
*
* @var string
*/
public $template;
/**
* The name of the layout file to render the template inside of. The name specified
* is the filename of the layout in /src/Template/Layout without the .ctp
* extension.
*
* @var string
*/
public $layout = 'default';
/**
* The name of the layouts subfolder containing layouts for this View.
*
* @var string
*/
public $layoutPath;
/**
* Turns on or off CakePHP's conventional mode of applying layout files. On by default.
* Setting to off means that layouts will not be automatically applied to rendered templates.
*
* @var bool
*/
public $autoLayout = true;
/**
* File extension. Defaults to CakePHP's template ".ctp".
*
* @var string
*/
protected $_ext = '.ctp';
/**
* Sub-directory for this template file. This is often used for extension based routing.
* Eg. With an `xml` extension, $subDir would be `xml/`
*
* @var string|null
*/
public $subDir;
/**
* The view theme to use.
*
* @var string|null
*/
public $theme;
/**
* True when the view has been rendered.
*
* @var bool
*/
public $hasRendered = false;
/**
* List of generated DOM UUIDs.
*
* @var array
*/
public $uuids = [];
/**
* An instance of a \Cake\Http\ServerRequest object that contains information about the current request.
* This object contains all the information about a request and several methods for reading
* additional information about the request.
*
* @var \Cake\Http\ServerRequest
*/
public $request;
/**
* Reference to the Response object
*
* @var \Cake\Http\Response
*/
public $response;
/**
* The Cache configuration View will use to store cached elements. Changing this will change
* the default configuration elements are stored under. You can also choose a cache config
* per element.
*
* @var string
* @see \Cake\View\View::element()
*/
public $elementCache = 'default';
/**
* List of variables to collect from the associated controller.
*
* @var array
*/
protected $_passedVars = [
'viewVars', 'autoLayout', 'helpers', 'template', 'layout', 'name', 'theme',
'layoutPath', 'templatePath', 'plugin', 'passedArgs'
];
/**
* Holds an array of paths.
*
* @var array
*/
protected $_paths = [];
/**
* Holds an array of plugin paths.
*
* @var array
*/
protected $_pathsForPlugin = [];
/**
* The names of views and their parents used with View::extend();
*
* @var array
*/
protected $_parents = [];
/**
* The currently rendering view file. Used for resolving parent files.
*
* @var string
*/
protected $_current;
/**
* Currently rendering an element. Used for finding parent fragments
* for elements.
*
* @var string
*/
protected $_currentType = '';
/**
* Content stack, used for nested templates that all use View::extend();
*
* @var array
*/
protected $_stack = [];
/**
* Constant for view file type 'view'
*
* @var string
* @deprecated 3.1.0 Use TYPE_TEMPLATE instead.
*/
const TYPE_VIEW = 'view';
/**
* Constant for view file type 'template'.
*
* @var string
*/
const TYPE_TEMPLATE = 'view';
/**
* Constant for view file type 'element'
*
* @var string
*/
const TYPE_ELEMENT = 'element';
/**
* Constant for name of view file 'Element'
*
* @var string
*/
const NAME_ELEMENT = 'Element';
/**
* Constant for view file type 'layout'
*
* @var string
*/
const TYPE_LAYOUT = 'layout';
/**
* Constant for template folder 'Template'
*
* @var string
*/
const NAME_TEMPLATE = 'Template';
/**
* Constructor
*
* @param \Cake\Http\ServerRequest|null $request Request instance.
* @param \Cake\Http\Response|null $response Response instance.
* @param \Cake\Event\EventManager|null $eventManager Event manager instance.
* @param array $viewOptions View options. See View::$_passedVars for list of
* options which get set as class properties.
*/
public function __construct(
ServerRequest $request = null,
Response $response = null,
EventManager $eventManager = null,
array $viewOptions = []
) {
if (isset($viewOptions['view'])) {
$this->setTemplate($viewOptions['view']);
}
if (isset($viewOptions['viewPath'])) {
$this->setTemplatePath($viewOptions['viewPath']);
}
foreach ($this->_passedVars as $var) {
if (isset($viewOptions[$var])) {
$this->{$var} = $viewOptions[$var];
}
}
if ($eventManager !== null) {
$this->setEventManager($eventManager);
}
$this->request = $request ?: Router::getRequest(true);
$this->response = $response ?: new Response();
if (!$this->request) {
$this->request = new ServerRequest([
'base' => '',
'url' => '',
'webroot' => '/'
]);
}
$this->Blocks = new ViewBlock();
$this->initialize();
$this->loadHelpers();
}
/**
* Initialization hook method.
*
* Properties like $helpers etc. cannot be initialized statically in your custom
* view class as they are overwritten by values from controller in constructor.
* So this method allows you to manipulate them as required after view instance
* is constructed.
*
* @return void
*/
public function initialize()
{
}
/**
* Get path for templates files.
*
* @return string
*/
public function getTemplatePath()
{
return $this->templatePath;
}
/**
* Set path for templates files.
*
* @param string $path Path for template files.
* @return $this
*/
public function setTemplatePath($path)
{
$this->templatePath = $path;
return $this;
}
/**
* Get/set path for templates files.
*
* @deprecated 3.5.0 Use getTemplatePath()/setTemplatePath() instead.
* @param string|null $path Path for template files. If null returns current path.
* @return string|null
*/
public function templatePath($path = null)
{
if ($path === null) {
return $this->templatePath;
}
$this->templatePath = $path;
}
/**
* Get path for layout files.
*
* @return string
*/
public function getLayoutPath()
{
return $this->layoutPath;
}
/**
* Set path for layout files.
*
* @param string $path Path for layout files.
* @return $this
*/
public function setLayoutPath($path)
{
$this->layoutPath = $path;
return $this;
}
/**
* Get/set path for layout files.
*
* @deprecated 3.5.0 Use getLayoutPath()/setLayoutPath() instead.
* @param string|null $path Path for layout files. If null returns current path.
* @return string|null
*/
public function layoutPath($path = null)
{
if ($path === null) {
return $this->layoutPath;
}
$this->layoutPath = $path;
}
/**
* Returns if CakePHP's conventional mode of applying layout files is enabled.
* Disabled means that layouts will not be automatically applied to rendered views.
*
* @return bool
*/
public function isAutoLayoutEnabled()
{
return $this->autoLayout;
}
/**
* Turns on or off CakePHP's conventional mode of applying layout files.
* On by default. Setting to off means that layouts will not be
* automatically applied to rendered views.
*
* @param bool $enable Boolean to turn on/off.
* @return $this
*/
public function enableAutoLayout($enable = true)
{
$this->autoLayout = (bool)$enable;
return $this;
}
/**
* Turns on or off CakePHP's conventional mode of applying layout files.
* On by default. Setting to off means that layouts will not be
* automatically applied to rendered templates.
*
* @deprecated 3.5.0 Use isAutoLayoutEnabled()/enableAutoLayout() instead.
* @param bool|null $autoLayout Boolean to turn on/off. If null returns current value.
* @return bool|null
*/
public function autoLayout($autoLayout = null)
{
if ($autoLayout === null) {
return $this->autoLayout;
}
$this->autoLayout = $autoLayout;
}
/**
* Get the current view theme.
*
* @return string|null
*/
public function getTheme()
{
return $this->theme;
}
/**
* Set the view theme to use.
*
* @param string|null $theme Theme name.
* @return $this
*/
public function setTheme($theme)
{
$this->theme = $theme;
return $this;
}
/**
* The view theme to use.
*
* @deprecated 3.5.0 Use getTheme()/setTheme() instead.
* @param string|null $theme Theme name. If null returns current theme.
* @return string|null
*/
public function theme($theme = null)
{
if ($theme === null) {
return $this->theme;
}
$this->theme = $theme;
}
/**
* Get the name of the template file to render. The name specified is the
* filename in /src/Template/<SubFolder> without the .ctp extension.
*
* @return string
*/
public function getTemplate()
{
return $this->template;
}
/**
* Set the name of the template file to render. The name specified is the
* filename in /src/Template/<SubFolder> without the .ctp extension.
*
* @param string $name Template file name to set.
* @return $this
*/
public function setTemplate($name)
{
$this->template = $name;
return $this;
}
/**
* Get/set the name of the template file to render. The name specified is the
* filename in /src/Template/<SubFolder> without the .ctp extension.
*
* @deprecated 3.5.0 Use getTemplate()/setTemplate() instead.
* @param string|null $name Template file name to set. If null returns current name.
* @return string|null
*/
public function template($name = null)
{
if ($name === null) {
return $this->template;
}
$this->template = $name;
}
/**
* Get the name of the layout file to render the template inside of.
* The name specified is the filename of the layout in /src/Template/Layout
* without the .ctp extension.
*
* @return string
*/
public function getLayout()
{
return $this->layout;
}
/**
* Set the name of the layout file to render the template inside of.
* The name specified is the filename of the layout in /src/Template/Layout
* without the .ctp extension.
*
* @param string $name Layout file name to set.
* @return $this
*/
public function setLayout($name)
{
$this->layout = $name;
return $this;
}
/**
* Get/set the name of the layout file to render the template inside of.
* The name specified is the filename of the layout in /src/Template/Layout
* without the .ctp extension.
*
* @deprecated 3.5.0 Use getLayout()/setLayout() instead.
* @param string|null $name Layout file name to set. If null returns current name.
* @return string|null
*/
public function layout($name = null)
{
if ($name === null) {
return $this->layout;
}
$this->layout = $name;
}
/**
* Renders a piece of PHP with provided parameters and returns HTML, XML, or any other string.
*
* This realizes the concept of Elements, (or "partial layouts") and the $params array is used to send
* data to be used in the element. Elements can be cached improving performance by using the `cache` option.
*
* @param string $name Name of template file in the /src/Template/Element/ folder,
* or `MyPlugin.template` to use the template element from MyPlugin. If the element
* is not found in the plugin, the normal view path cascade will be searched.
* @param array $data Array of data to be made available to the rendered view (i.e. the Element)
* @param array $options Array of options. Possible keys are:
* - `cache` - Can either be `true`, to enable caching using the config in View::$elementCache. Or an array
* If an array, the following keys can be used:
* - `config` - Used to store the cached element in a custom cache configuration.
* - `key` - Used to define the key used in the Cache::write(). It will be prefixed with `element_`
* - `callbacks` - Set to true to fire beforeRender and afterRender helper callbacks for this element.
* Defaults to false.
* - `ignoreMissing` - Used to allow missing elements. Set to true to not throw exceptions.
* - `plugin` - setting to false will force to use the application's element from plugin templates, when the
* plugin has element with same name. Defaults to true
* @return string Rendered Element
* @throws \Cake\View\Exception\MissingElementException When an element is missing and `ignoreMissing`
* is false.
*/
public function element($name, array $data = [], array $options = [])
{
$options += ['callbacks' => false, 'cache' => null, 'plugin' => null];
if (isset($options['cache'])) {
$options['cache'] = $this->_elementCache($name, $data, $options);
}
$pluginCheck = $options['plugin'] !== false;
$file = $this->_getElementFileName($name, $pluginCheck);
if ($file && $options['cache']) {
return $this->cache(function () use ($file, $data, $options) {
echo $this->_renderElement($file, $data, $options);
}, $options['cache']);
}
if ($file) {
return $this->_renderElement($file, $data, $options);
}
if (empty($options['ignoreMissing'])) {
list ($plugin, $name) = pluginSplit($name, true);
$name = str_replace('/', DIRECTORY_SEPARATOR, $name);
$file = $plugin . static::NAME_ELEMENT . DIRECTORY_SEPARATOR . $name . $this->_ext;
throw new MissingElementException([$file]);
}
}
/**
* Create a cached block of view logic.
*
* This allows you to cache a block of view output into the cache
* defined in `elementCache`.
*
* This method will attempt to read the cache first. If the cache
* is empty, the $block will be run and the output stored.
*
* @param callable $block The block of code that you want to cache the output of.
* @param array $options The options defining the cache key etc.
* @return string The rendered content.
* @throws \RuntimeException When $options is lacking a 'key' option.
*/
public function cache(callable $block, array $options = [])
{
$options += ['key' => '', 'config' => $this->elementCache];
if (empty($options['key'])) {
throw new RuntimeException('Cannot cache content with an empty key');
}
$result = Cache::read($options['key'], $options['config']);
if ($result) {
return $result;
}
ob_start();
$block();
$result = ob_get_clean();
Cache::write($options['key'], $result, $options['config']);
return $result;
}
/**
* Checks if an element exists
*
* @param string $name Name of template file in the /src/Template/Element/ folder,
* or `MyPlugin.template` to check the template element from MyPlugin. If the element
* is not found in the plugin, the normal view path cascade will be searched.
* @return bool Success
*/
public function elementExists($name)
{
return (bool)$this->_getElementFileName($name);
}
/**
* Renders view for given template file and layout.
*
* Render triggers helper callbacks, which are fired before and after the template are rendered,
* as well as before and after the layout. The helper callbacks are called:
*
* - `beforeRender`
* - `afterRender`
* - `beforeLayout`
* - `afterLayout`
*
* If View::$autoRender is false and no `$layout` is provided, the template will be returned bare.
*
* Template and layout names can point to plugin templates/layouts. Using the `Plugin.template` syntax
* a plugin template/layout can be used instead of the app ones. If the chosen plugin is not found
* the template will be located along the regular view path cascade.
*
* @param string|false|null $view Name of view file to use
* @param string|null $layout Layout to use.
* @return string|null Rendered content or null if content already rendered and returned earlier.
* @throws \Cake\Core\Exception\Exception If there is an error in the view.
* @triggers View.beforeRender $this, [$viewFileName]
* @triggers View.afterRender $this, [$viewFileName]
*/
public function render($view = null, $layout = null)
{
if ($this->hasRendered) {
return null;
}
$defaultLayout = null;
if ($layout !== null) {
$defaultLayout = $this->layout;
$this->layout = $layout;
}
$viewFileName = $view !== false ? $this->_getViewFileName($view) : null;
if ($viewFileName) {
$this->_currentType = static::TYPE_TEMPLATE;
$this->dispatchEvent('View.beforeRender', [$viewFileName]);
$this->Blocks->set('content', $this->_render($viewFileName));
$this->dispatchEvent('View.afterRender', [$viewFileName]);
}
if ($this->layout && $this->autoLayout) {
$this->Blocks->set('content', $this->renderLayout('', $this->layout));
}
if ($layout !== null) {
$this->layout = $defaultLayout;
}
$this->hasRendered = true;
return $this->Blocks->get('content');
}
/**
* Renders a layout. Returns output from _render(). Returns false on error.
* Several variables are created for use in layout.
*
* @param string $content Content to render in a template, wrapped by the surrounding layout.
* @param string|null $layout Layout name
* @return mixed Rendered output, or false on error
* @throws \Cake\Core\Exception\Exception if there is an error in the view.
* @triggers View.beforeLayout $this, [$layoutFileName]
* @triggers View.afterLayout $this, [$layoutFileName]
*/
public function renderLayout($content, $layout = null)
{
$layoutFileName = $this->_getLayoutFileName($layout);
if (empty($layoutFileName)) {
return $this->Blocks->get('content');
}
if (!empty($content)) {
$this->Blocks->set('content', $content);
}
$this->dispatchEvent('View.beforeLayout', [$layoutFileName]);
$title = $this->Blocks->get('title');
if ($title === '') {
$title = Inflector::humanize($this->templatePath);
$this->Blocks->set('title', $title);
}
$this->_currentType = static::TYPE_LAYOUT;
$this->Blocks->set('content', $this->_render($layoutFileName));
$this->dispatchEvent('View.afterLayout', [$layoutFileName]);
return $this->Blocks->get('content');
}
/**
* Returns a list of variables available in the current View context
*
* @return array Array of the set view variable names.
*/
public function getVars()
{
return array_keys($this->viewVars);
}
/**
* Returns the contents of the given View variable.
*
* @param string $var The view var you want the contents of.
* @param mixed $default The default/fallback content of $var.
* @return mixed The content of the named var if its set, otherwise $default.
*/
public function get($var, $default = null)
{
if (!isset($this->viewVars[$var])) {
return $default;
}
return $this->viewVars[$var];
}
/**
* Get the names of all the existing blocks.
*
* @return array An array containing the blocks.
* @see \Cake\View\ViewBlock::keys()
*/
public function blocks()
{
return $this->Blocks->keys();
}
/**
* Start capturing output for a 'block'
*
* You can use start on a block multiple times to
* append or prepend content in a capture mode.
*
* ```
* // Append content to an existing block.
* $this->start('content');
* echo $this->fetch('content');
* echo 'Some new content';
* $this->end();
*
* // Prepend content to an existing block
* $this->start('content');
* echo 'Some new content';
* echo $this->fetch('content');
* $this->end();
* ```
*
* @param string $name The name of the block to capture for.
* @return $this
* @see \Cake\View\ViewBlock::start()
*/
public function start($name)
{
$this->Blocks->start($name);
return $this;
}
/**
* Append to an existing or new block.
*
* Appending to a new block will create the block.
*
* @param string $name Name of the block
* @param mixed $value The content for the block. Value will be type cast
* to string.
* @return $this
* @see \Cake\View\ViewBlock::concat()
*/
public function append($name, $value = null)
{
$this->Blocks->concat($name, $value);
return $this;
}
/**
* Prepend to an existing or new block.
*
* Prepending to a new block will create the block.
*
* @param string $name Name of the block
* @param mixed $value The content for the block. Value will be type cast
* to string.
* @return $this
* @see \Cake\View\ViewBlock::concat()
*/
public function prepend($name, $value)
{
$this->Blocks->concat($name, $value, ViewBlock::PREPEND);
return $this;
}
/**
* Set the content for a block. This will overwrite any
* existing content.
*
* @param string $name Name of the block
* @param mixed $value The content for the block. Value will be type cast
* to string.
* @return $this
* @see \Cake\View\ViewBlock::set()
*/
public function assign($name, $value)
{
$this->Blocks->set($name, $value);
return $this;
}
/**
* Reset the content for a block. This will overwrite any
* existing content.
*
* @param string $name Name of the block
* @return $this
* @see \Cake\View\ViewBlock::set()
*/
public function reset($name)
{
$this->assign($name, '');
return $this;
}
/**
* Fetch the content for a block. If a block is
* empty or undefined '' will be returned.
*
* @param string $name Name of the block
* @param string $default Default text
* @return string The block content or $default if the block does not exist.
* @see \Cake\View\ViewBlock::get()
*/
public function fetch($name, $default = '')
{
return $this->Blocks->get($name, $default);
}
/**
* End a capturing block. The compliment to View::start()
*
* @return $this
* @see \Cake\View\ViewBlock::end()
*/
public function end()
{
$this->Blocks->end();
return $this;
}
/**
* Check if a block exists
*
* @param string $name Name of the block
*
* @return bool
*/
public function exists($name)
{
return $this->Blocks->exists($name);
}
/**
* Provides template or element extension/inheritance. Views can extends a
* parent view and populate blocks in the parent template.
*
* @param string $name The template or element to 'extend' the current one with.
* @return $this
* @throws \LogicException when you extend a template with itself or make extend loops.