Skip to content

Instantly share code, notes, and snippets.

@mikemix
Last active January 4, 2016 05:49
Show Gist options
  • Save mikemix/8577834 to your computer and use it in GitHub Desktop.
Save mikemix/8577834 to your computer and use it in GitHub Desktop.
Renders flashMessenger's messages using Semantic UI markup
<?php
namespace Application\Helper;
use Zend\View\Helper\AbstractHelper;
use Zend\View\Helper\FlashMessenger;
/**
* SemanticMessengerHelper class
*
* Register as plugin helper in application config:
* 'view_helpers' => array(
* 'invokables' => array(
* 'messenger' => 'YourModule\Helper\SemanticMessengerHelper'
* ),
* )
*
* Display messages in a view:
* <?= $this->messenger($this->flashMessenger(), 'success') ?>
*/
class SemanticMessengerHelper extends AbstractHelper
{
/**
* @param FlashMessenger $helper
* @param $class
* @param string $title
* @return string
*/
public function __invoke(FlashMessenger $helper, $class, $title='')
{
$messenger=$helper->getPluginFlashMessenger();
$method=sprintf('%sMessages', ucfirst($class));
if ($messenger->{'has' . $method}()) {
return $this->renderMessages($messenger->{'get' . $method}(), $class, $title);
}
return '';
}
/**
* @param array $messages
* @param string $title
* @param string $class
* @return string
*/
protected function renderMessages(array $messages, $class, $title)
{
$list=array();
foreach ($messages as $message) {
$list[] = '<li>' . $message . '</li>';
}
$title=$title == '' ?: sprintf('<div class="header">%s</div>', $title);
return sprintf(
'<script>$(".message .close").on("click", function() {$(this).closest(".message").fadeOut();});</script>'.
'<div class="ui %s message"><i class="close icon"></i>%s<ul class="list">%s</ul></div>',
$class, $title, implode('', $list)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment