Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactor SessionHelper to use a string template.

Consistency across the helpers is important. Having SessionHelper use
templates like other helpers works to that end.
  • Loading branch information...
commit 03428431a421944c7580bc51ce5d49ca2012abaf 1 parent 4ce49fc
@markstory markstory authored
Showing with 33 additions and 3 deletions.
  1. +33 −3 src/View/Helper/SessionHelper.php
View
36 src/View/Helper/SessionHelper.php
@@ -18,6 +18,8 @@
use Cake\Network\Session;
use Cake\View\Helper;
+use Cake\View\Helper\StringTemplateTrait;
+use Cake\View\View;
/**
* Session Helper.
@@ -28,6 +30,30 @@
*/
class SessionHelper extends Helper {
+ use StringTemplateTrait;
+
+/**
+ * Default templates to use.
+ *
+ * @var array
+ */
+ protected $_defaultTemplates = [
+ 'flash' => '<div id="{{key}}Message" class="{{class}}">{{message}}</div>'
+ ];
+
+/**
+ * Construct the helper and sets up templates
+ *
+ * @param \Cake\View\View $view The View this helper is being attached to.
+ * @param array $settings Configuration settings for the helper.
+ */
+ public function __construct(View $view, $settings = []) {
+ $settings += ['templates' => null];
+ parent::__construct($view, $settings);
+
+ $this->initStringTemplates($this->_defaultTemplates);
+ }
+
/**
* Used to read a session values set in a controller for a key or return values for all keys.
*
@@ -103,8 +129,8 @@ public function error() {
*
* {{{
* echo $this->Session->flash('flash', array(
- * 'element' => 'my_custom_element',
- * 'params' => array('plugin' => 'my_plugin')
+ * 'element' => 'my_custom_element',
+ * 'params' => array('plugin' => 'my_plugin')
* ));
* }}}
*
@@ -131,7 +157,11 @@ public function flash($key = 'flash', $attrs = array()) {
if (!empty($flash['params']['class'])) {
$class = $flash['params']['class'];
}
- $out = '<div id="' . $key . 'Message" class="' . $class . '">' . $message . '</div>';
+ $out = $this->formatTemplate('flash', [
+ 'class' => $class,
+ 'key' => $key,
+ 'message' => $message
+ ]);
} elseif (!$flash['element']) {
$out = $message;
} else {
Please sign in to comment.
Something went wrong with that request. Please try again.