Skip to content

Commit 0342843

Browse files
committed
Refactor SessionHelper to use a string template.
Consistency across the helpers is important. Having SessionHelper use templates like other helpers works to that end.
1 parent 4ce49fc commit 0342843

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

src/View/Helper/SessionHelper.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
use Cake\Network\Session;
2020
use Cake\View\Helper;
21+
use Cake\View\Helper\StringTemplateTrait;
22+
use Cake\View\View;
2123

2224
/**
2325
* Session Helper.
@@ -28,6 +30,30 @@
2830
*/
2931
class SessionHelper extends Helper {
3032

33+
use StringTemplateTrait;
34+
35+
/**
36+
* Default templates to use.
37+
*
38+
* @var array
39+
*/
40+
protected $_defaultTemplates = [
41+
'flash' => '<div id="{{key}}Message" class="{{class}}">{{message}}</div>'
42+
];
43+
44+
/**
45+
* Construct the helper and sets up templates
46+
*
47+
* @param \Cake\View\View $view The View this helper is being attached to.
48+
* @param array $settings Configuration settings for the helper.
49+
*/
50+
public function __construct(View $view, $settings = []) {
51+
$settings += ['templates' => null];
52+
parent::__construct($view, $settings);
53+
54+
$this->initStringTemplates($this->_defaultTemplates);
55+
}
56+
3157
/**
3258
* Used to read a session values set in a controller for a key or return values for all keys.
3359
*
@@ -103,8 +129,8 @@ public function error() {
103129
*
104130
* {{{
105131
* echo $this->Session->flash('flash', array(
106-
* 'element' => 'my_custom_element',
107-
* 'params' => array('plugin' => 'my_plugin')
132+
* 'element' => 'my_custom_element',
133+
* 'params' => array('plugin' => 'my_plugin')
108134
* ));
109135
* }}}
110136
*
@@ -131,7 +157,11 @@ public function flash($key = 'flash', $attrs = array()) {
131157
if (!empty($flash['params']['class'])) {
132158
$class = $flash['params']['class'];
133159
}
134-
$out = '<div id="' . $key . 'Message" class="' . $class . '">' . $message . '</div>';
160+
$out = $this->formatTemplate('flash', [
161+
'class' => $class,
162+
'key' => $key,
163+
'message' => $message
164+
]);
135165
} elseif (!$flash['element']) {
136166
$out = $message;
137167
} else {

0 commit comments

Comments
 (0)