Skip to content

Commit

Permalink
Refactor SessionHelper to use a string template.
Browse files Browse the repository at this point in the history
Consistency across the helpers is important. Having SessionHelper use
templates like other helpers works to that end.
  • Loading branch information
markstory committed Mar 6, 2014
1 parent 4ce49fc commit 0342843
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/View/Helper/SessionHelper.php
Expand Up @@ -18,6 +18,8 @@

use Cake\Network\Session;
use Cake\View\Helper;
use Cake\View\Helper\StringTemplateTrait;
use Cake\View\View;

/**
* Session Helper.
Expand All @@ -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.
*
Expand Down Expand Up @@ -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')
* ));
* }}}
*
Expand All @@ -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 {
Expand Down

0 comments on commit 0342843

Please sign in to comment.