20
20
use Cake \Network \Response ;
21
21
use Cake \Utility \Inflector ;
22
22
use Cake \View \Helper ;
23
+ use Cake \View \Helper \StringTemplateTrait ;
23
24
use Cake \View \View ;
24
25
25
26
/**
31
32
*/
32
33
class HtmlHelper extends Helper {
33
34
35
+ use StringTemplateTrait;
36
+
34
37
/**
35
38
* Reference to the Response object
36
39
*
@@ -74,6 +77,42 @@ class HtmlHelper extends Helper {
74
77
'javascriptend ' => '</script> '
75
78
);
76
79
80
+ /**
81
+ * Default templates the helper users.
82
+ *
83
+ * @var array
84
+ */
85
+ protected $ _defaultTemplates = [
86
+ 'meta ' => '<meta{{attrs}}/> ' ,
87
+ 'metalink ' => '<link href="{{url}}"{{attrs}}/> ' ,
88
+ 'link ' => '<a href="{{url}}"{{attrs}}>{{content}}</a> ' ,
89
+ 'mailto ' => '<a href="mailto:{{url}}"{{attrs}}>{{content}}</a> ' ,
90
+ 'image ' => '<img src="{{url}}"{{attrs}}/> ' ,
91
+ 'tableheader ' => '<th{{attrs}}>{{content}}</th> ' ,
92
+ 'tableheaderrow ' => '<tr{{attrs}}>{{content}}</tr> ' ,
93
+ 'tablecell ' => '<td{{attrs}}>{{content}}</td> ' ,
94
+ 'tablerow ' => '<tr{{attrs}}>{{content}}</tr> ' ,
95
+ 'block ' => '<div{{attrs}}>{{content}}</div> ' ,
96
+ 'blockstart ' => '<div{{attrs}}> ' ,
97
+ 'blockend ' => '</div> ' ,
98
+ 'tag ' => '<{{tag}}{{attrs}}>{{content}}</{{tag}}> ' ,
99
+ 'tagstart ' => '<{{tag}}{{attrs}}> ' ,
100
+ 'tagend ' => '</{{tag}}> ' ,
101
+ 'tagselfclosing ' => '<{{tag}}{{attrs}}/> ' ,
102
+ 'para ' => '<p{{attrs}}>{{content}}</p> ' ,
103
+ 'parastart ' => '<p{{attrs}}> ' ,
104
+ 'css ' => '<link rel="{{rel}}" type="text/css" href="{{url}}"{{attrs}}/> ' ,
105
+ 'style ' => '<style type="text/css"{{attrs}}>{{content}}</style> ' ,
106
+ 'charset ' => '<meta http-equiv="Content-Type" content="text/html; charset={{charset}}" /> ' ,
107
+ 'ul ' => '<ul{{attrs}}>{{content}}</ul> ' ,
108
+ 'ol ' => '<ol{{attrs}}>{{content}}</ol> ' ,
109
+ 'li ' => '<li{{attrs}}>{{content}}</li> ' ,
110
+ 'javascriptblock ' => '<script{{attrs}}>{{content}}</script> ' ,
111
+ 'javascriptstart ' => '<script> ' ,
112
+ 'javascriptlink ' => '<script type="text/javascript" src="{{url}}"{{attrs}}></script> ' ,
113
+ 'javascriptend ' => '</script> '
114
+ ];
115
+
77
116
/**
78
117
* Breadcrumbs.
79
118
*
@@ -116,26 +155,22 @@ class HtmlHelper extends Helper {
116
155
*
117
156
* ### Settings
118
157
*
119
- * - `configFile` A file containing an array of tags you wish to redefine.
158
+ * - `templates` Either a filename to a config containing templates.
159
+ * Or an array of templates to load. See Cake\View\StringTemplate for
160
+ * template formatting.
120
161
*
121
162
* ### Customizing tag sets
122
163
*
123
- * Using the `configFile` option you can redefine the tag HtmlHelper will use.
124
- * The file named should be compatible with HtmlHelper::loadConfig().
164
+ * Using the `templates` option you can redefine the tag HtmlHelper will use.
125
165
*
126
166
* @param View $View The View this helper is being attached to.
127
167
* @param array $settings Configuration settings for the helper.
128
168
*/
129
169
public function __construct (View $ View , $ settings = array ()) {
130
170
parent ::__construct ($ View , $ settings );
131
- if (is_object ($ this ->_View ->response )) {
132
- $ this ->response = $ this ->_View ->response ;
133
- } else {
134
- $ this ->response = new Response ();
135
- }
136
- if (!empty ($ settings ['configFile ' ])) {
137
- $ this ->loadConfig ($ settings ['configFile ' ]);
138
- }
171
+ $ this ->response = $ this ->_View ->response ?: new Response ();
172
+
173
+ $ this ->initStringTemplates ($ this ->_defaultTemplates );
139
174
}
140
175
141
176
/**
@@ -248,12 +283,20 @@ public function meta($type, $url = null, $options = array()) {
248
283
if (isset ($ options ['link ' ])) {
249
284
$ options ['link ' ] = $ this ->assetUrl ($ options ['link ' ]);
250
285
if (isset ($ options ['rel ' ]) && $ options ['rel ' ] === 'icon ' ) {
251
- $ out = sprintf ($ this ->_tags ['metalink ' ], $ options ['link ' ], $ this ->_parseAttributes ($ options , array ('block ' , 'link ' ), ' ' , ' ' ));
286
+ $ out = $ this ->formatTemplate ('metalink ' , [
287
+ 'url ' => $ options ['link ' ],
288
+ 'attrs ' => $ this ->_templater ->formatAttributes ($ options , ['block ' , 'link ' ])
289
+ ]);
252
290
$ options ['rel ' ] = 'shortcut icon ' ;
253
291
}
254
- $ out .= sprintf ($ this ->_tags ['metalink ' ], $ options ['link ' ], $ this ->_parseAttributes ($ options , array ('block ' , 'link ' ), ' ' , ' ' ));
292
+ $ out .= $ this ->formatTemplate ('metalink ' , [
293
+ 'url ' => $ options ['link ' ],
294
+ 'attrs ' => $ this ->_templater ->formatAttributes ($ options , ['block ' , 'link ' ])
295
+ ]);
255
296
} else {
256
- $ out = sprintf ($ this ->_tags ['meta ' ], $ this ->_parseAttributes ($ options , array ('block ' , 'type ' ), ' ' , ' ' ));
297
+ $ out = $ this ->formatTemplate ('meta ' , [
298
+ 'attrs ' => $ this ->_templater ->formatAttributes ($ options , ['block ' , 'type ' ])
299
+ ]);
257
300
}
258
301
259
302
if (empty ($ options ['block ' ])) {
@@ -274,7 +317,9 @@ public function charset($charset = null) {
274
317
if (empty ($ charset )) {
275
318
$ charset = strtolower (Configure::read ('App.encoding ' ));
276
319
}
277
- return sprintf ($ this ->_tags ['charset ' ], (!empty ($ charset ) ? $ charset : 'utf-8 ' ));
320
+ return $ this ->formatTemplate ('charset ' , [
321
+ 'charset ' => (!empty ($ charset ) ? $ charset : 'utf-8 ' )
322
+ ]);
278
323
}
279
324
280
325
/**
@@ -338,7 +383,11 @@ public function link($title, $url = null, $options = array(), $confirmMessage =
338
383
$ options ['onclick ' ] .= 'event.returnValue = false; return false; ' ;
339
384
unset($ options ['default ' ]);
340
385
}
341
- return sprintf ($ this ->_tags ['link ' ], $ url , $ this ->_parseAttributes ($ options ), $ title );
386
+ return $ this ->formatTemplate ('link ' , [
387
+ 'url ' => $ url ,
388
+ 'attrs ' => $ this ->_templater ->formatAttributes ($ options ),
389
+ 'content ' => $ title
390
+ ]);
342
391
}
343
392
344
393
/**
@@ -417,18 +466,16 @@ public function css($path, $options = array()) {
417
466
}
418
467
419
468
if ($ options ['rel ' ] == 'import ' ) {
420
- $ out = sprintf (
421
- $ this ->_tags ['style ' ],
422
- $ this ->_parseAttributes ($ options , array ('rel ' , 'block ' ), '' , ' ' ),
423
- '@import url( ' . $ url . '); '
424
- );
469
+ $ out = $ this ->formatTemplate ('style ' , [
470
+ 'attrs ' => $ this ->_templater ->formatAttributes ($ options , ['rel ' , 'block ' ]),
471
+ 'content ' => '@import url( ' . $ url . '); ' ,
472
+ ]);
425
473
} else {
426
- $ out = sprintf (
427
- $ this ->_tags ['css ' ],
428
- $ options ['rel ' ],
429
- $ url ,
430
- $ this ->_parseAttributes ($ options , array ('rel ' , 'block ' ), '' , ' ' )
431
- );
474
+ $ out = $ this ->formatTemplate ('css ' , [
475
+ 'rel ' => $ options ['rel ' ],
476
+ 'url ' => $ url ,
477
+ 'attrs ' => $ this ->_templater ->formatAttributes ($ options , ['rel ' , 'block ' ]),
478
+ ]);
432
479
}
433
480
434
481
if (empty ($ options ['block ' ])) {
@@ -509,8 +556,10 @@ public function script($url, $options = array()) {
509
556
$ url = $ this ->assetUrl ($ url , $ options + array ('pathPrefix ' => Configure::read ('App.jsBaseUrl ' ), 'ext ' => '.js ' ));
510
557
$ options = array_diff_key ($ options , array ('fullBase ' => null , 'pathPrefix ' => null ));
511
558
}
512
- $ attributes = $ this ->_parseAttributes ($ options , array ('block ' , 'once ' ), ' ' );
513
- $ out = sprintf ($ this ->_tags ['javascriptlink ' ], $ url , $ attributes );
559
+ $ out = $ this ->formatTemplate ('javascriptlink ' , [
560
+ 'url ' => $ url ,
561
+ 'attrs ' => $ this ->_templater ->formatAttributes ($ options , ['block ' , 'once ' ]),
562
+ ]);
514
563
515
564
if (empty ($ options ['block ' ])) {
516
565
return $ out ;
@@ -545,8 +594,10 @@ public function scriptBlock($script, $options = array()) {
545
594
}
546
595
unset($ options ['inline ' ], $ options ['safe ' ]);
547
596
548
- $ attributes = $ this ->_parseAttributes ($ options , array ('block ' ), ' ' );
549
- $ out = sprintf ($ this ->_tags ['javascriptblock ' ], $ attributes , $ script );
597
+ $ out = $ this ->formatTemplate ('javascriptblock ' , [
598
+ 'attrs ' => $ this ->_templater ->formatAttributes ($ options , ['block ' ]),
599
+ 'content ' => $ script
600
+ ]);
550
601
551
602
if (empty ($ options ['block ' ])) {
552
603
return $ out ;
@@ -661,6 +712,7 @@ public function getCrumbs($separator = '»', $startText = false) {
661
712
* crumb was added with.
662
713
*
663
714
* ### Options
715
+ *
664
716
* - `separator` Separator content to insert in between breadcrumbs, defaults to ''
665
717
* - `firstClass` Class for wrapper tag on the first breadcrumb, defaults to 'first'
666
718
* - `lastClass` Class for wrapper tag on current active page, defaults to 'last'
@@ -702,9 +754,15 @@ public function getCrumbList($options = array(), $startText = false) {
702
754
if (!empty ($ separator ) && ($ crumbCount - $ which >= 2 )) {
703
755
$ elementContent .= $ separator ;
704
756
}
705
- $ result .= $ this ->tag ('li ' , $ elementContent , $ options );
706
- }
707
- return $ this ->tag ('ul ' , $ result , $ ulOptions );
757
+ $ result .= $ this ->formatTemplate ('li ' , [
758
+ 'content ' => $ elementContent ,
759
+ 'attrs ' => $ this ->_templater ->formatAttributes ($ options )
760
+ ]);
761
+ }
762
+ return $ this ->formatTemplate ('ul ' , [
763
+ 'content ' => $ result ,
764
+ 'attrs ' => $ this ->_templater ->formatAttributes ($ ulOptions )
765
+ ]);
708
766
}
709
767
710
768
/**
@@ -771,10 +829,17 @@ public function image($path, $options = array()) {
771
829
unset($ options ['url ' ]);
772
830
}
773
831
774
- $ image = sprintf ($ this ->_tags ['image ' ], $ path , $ this ->_parseAttributes ($ options , null , '' , ' ' ));
832
+ $ image = $ this ->formatTemplate ('image ' , [
833
+ 'url ' => $ path ,
834
+ 'attrs ' => $ this ->_templater ->formatAttributes ($ options ),
835
+ ]);
775
836
776
837
if ($ url ) {
777
- return sprintf ($ this ->_tags ['link ' ], $ this ->url ($ url ), null , $ image );
838
+ return $ this ->formatTemplate ('link ' , [
839
+ 'url ' => $ this ->url ($ url ),
840
+ 'attrs ' => null ,
841
+ 'content ' => $ image
842
+ ]);
778
843
}
779
844
return $ image ;
780
845
}
0 commit comments