Skip to content

Commit 7dfdfaf

Browse files
committed
Start updating HtmlHelper to use string templates.
1 parent dfc7487 commit 7dfdfaf

File tree

2 files changed

+101
-37
lines changed

2 files changed

+101
-37
lines changed

src/View/Helper/HtmlHelper.php

Lines changed: 101 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Cake\Network\Response;
2121
use Cake\Utility\Inflector;
2222
use Cake\View\Helper;
23+
use Cake\View\Helper\StringTemplateTrait;
2324
use Cake\View\View;
2425

2526
/**
@@ -31,6 +32,8 @@
3132
*/
3233
class HtmlHelper extends Helper {
3334

35+
use StringTemplateTrait;
36+
3437
/**
3538
* Reference to the Response object
3639
*
@@ -74,6 +77,42 @@ class HtmlHelper extends Helper {
7477
'javascriptend' => '</script>'
7578
);
7679

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+
77116
/**
78117
* Breadcrumbs.
79118
*
@@ -116,26 +155,22 @@ class HtmlHelper extends Helper {
116155
*
117156
* ### Settings
118157
*
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.
120161
*
121162
* ### Customizing tag sets
122163
*
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.
125165
*
126166
* @param View $View The View this helper is being attached to.
127167
* @param array $settings Configuration settings for the helper.
128168
*/
129169
public function __construct(View $View, $settings = array()) {
130170
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);
139174
}
140175

141176
/**
@@ -248,12 +283,20 @@ public function meta($type, $url = null, $options = array()) {
248283
if (isset($options['link'])) {
249284
$options['link'] = $this->assetUrl($options['link']);
250285
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+
]);
252290
$options['rel'] = 'shortcut icon';
253291
}
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+
]);
255296
} 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+
]);
257300
}
258301

259302
if (empty($options['block'])) {
@@ -274,7 +317,9 @@ public function charset($charset = null) {
274317
if (empty($charset)) {
275318
$charset = strtolower(Configure::read('App.encoding'));
276319
}
277-
return sprintf($this->_tags['charset'], (!empty($charset) ? $charset : 'utf-8'));
320+
return $this->formatTemplate('charset', [
321+
'charset' => (!empty($charset) ? $charset : 'utf-8')
322+
]);
278323
}
279324

280325
/**
@@ -338,7 +383,11 @@ public function link($title, $url = null, $options = array(), $confirmMessage =
338383
$options['onclick'] .= 'event.returnValue = false; return false;';
339384
unset($options['default']);
340385
}
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+
]);
342391
}
343392

344393
/**
@@ -417,18 +466,16 @@ public function css($path, $options = array()) {
417466
}
418467

419468
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+
]);
425473
} 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+
]);
432479
}
433480

434481
if (empty($options['block'])) {
@@ -509,8 +556,10 @@ public function script($url, $options = array()) {
509556
$url = $this->assetUrl($url, $options + array('pathPrefix' => Configure::read('App.jsBaseUrl'), 'ext' => '.js'));
510557
$options = array_diff_key($options, array('fullBase' => null, 'pathPrefix' => null));
511558
}
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+
]);
514563

515564
if (empty($options['block'])) {
516565
return $out;
@@ -545,8 +594,10 @@ public function scriptBlock($script, $options = array()) {
545594
}
546595
unset($options['inline'], $options['safe']);
547596

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+
]);
550601

551602
if (empty($options['block'])) {
552603
return $out;
@@ -661,6 +712,7 @@ public function getCrumbs($separator = '&raquo;', $startText = false) {
661712
* crumb was added with.
662713
*
663714
* ### Options
715+
*
664716
* - `separator` Separator content to insert in between breadcrumbs, defaults to ''
665717
* - `firstClass` Class for wrapper tag on the first breadcrumb, defaults to 'first'
666718
* - `lastClass` Class for wrapper tag on current active page, defaults to 'last'
@@ -702,9 +754,15 @@ public function getCrumbList($options = array(), $startText = false) {
702754
if (!empty($separator) && ($crumbCount - $which >= 2)) {
703755
$elementContent .= $separator;
704756
}
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+
]);
708766
}
709767

710768
/**
@@ -771,10 +829,17 @@ public function image($path, $options = array()) {
771829
unset($options['url']);
772830
}
773831

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+
]);
775836

776837
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+
]);
778843
}
779844
return $image;
780845
}

tests/TestCase/View/Helper/HtmlHelperTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1614,7 +1614,6 @@ public function testMeta() {
16141614

16151615
$result = $this->Html->meta('keywords', 'these, are, some, meta, keywords');
16161616
$this->assertTags($result, array('meta' => array('name' => 'keywords', 'content' => 'these, are, some, meta, keywords')));
1617-
$this->assertRegExp('/\s+\/>$/', $result);
16181617

16191618
$result = $this->Html->meta('description', 'this is the meta description');
16201619
$this->assertTags($result, array('meta' => array('name' => 'description', 'content' => 'this is the meta description')));

0 commit comments

Comments
 (0)