use kanellov/slim-twig-flash for rendering flash in templates

parent 1ff667a3
......@@ -23,9 +23,8 @@ $container['view'] = function ($c) {
$c->get('request')->getUri()
));
$view->addExtension(new Twig_Extension_Debug());
$view->addExtension(new GrEduLabs\Twig\Extension\Flash(
$c->get('flash'),
'flash.twig'
$view->addExtension(new Knlv\Slim\Views\TwigMessages(
$c->get('flash')
));
return $view;
......
<?php
/**
* gredu_labs
*
* @link https://github.com/eellak/gredu_labs for the canonical source repository
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
namespace GrEduLabs\Twig\Extension;
use Slim\Flash\Messages;
use Twig_Environment;
use Twig_Extension;
use Twig_SimpleFunction;
class Flash extends Twig_Extension
{
/**
* @var Twig_Environment
*/
protected $environment;
/**
* @var Messages
*/
protected $flash;
/**
* @var string
*/
protected $template;
public function __construct(Messages $flash, $template)
{
$this->flash = $flash;
$this->template = $template;
}
public function getName()
{
return 'slim-flash';
}
public function initRuntime(Twig_Environment $environment)
{
$this->environment = $environment;
}
public function getFunctions()
{
return [
new Twig_SimpleFunction('flash', [$this, 'messages'], [
'needs_environment' => true,
'is_safe' => ['all'],
]),
];
}
public function messages()
{
$response = '';
$allMessages = $this->flash->getMessages();
if (!empty($allMessages)) {
foreach ($allMessages as $class => $messages) {
$response .= $this->environment->render($this->template, [
'class' => $class,
'messages' => $messages,
]);
}
}
return $response;
}
}
<ul class="alert alert-{{ class }}" role="alert">
{% for class, messages in flash() %}
<ul class="list-unstyled alert alert-{{ class }}" role="alert">
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endfor %}
......@@ -7,7 +7,7 @@
<link href="{{ base_url() }}/css/style.css" rel="stylesheet" type="text/css">
</head>
<body>
{{ flash() }}
{% include 'flash.twig' %}
{% block content %}{% endblock %}
<script src="//code.jquery.com/jquery-2.2.0.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
......
......@@ -4,8 +4,8 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "6db91d83a5d7f09ca4aa148dd467aa37",
"content-hash": "9dbc2f0e135613f71e872c3845d833ba",
"hash": "6bab5bd43fd57ed6d83fd7df11328bb7",
"content-hash": "d8c7dd10bd4590b2b0978fca5f170872",
"packages": [
{
"name": "container-interop/container-interop",
......@@ -136,6 +136,61 @@
],
"time": "2015-03-16 10:17:44"
},
{
"name": "kanellov/slim-twig-flash",
"version": "0.1.0",
"source": {
"type": "git",
"url": "https://github.com/kanellov/slim-twig-flash.git",
"reference": "6c5ecfdbf7eeb9ea44bbc6dbc4a902c1930a392c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/kanellov/slim-twig-flash/zipball/6c5ecfdbf7eeb9ea44bbc6dbc4a902c1930a392c",
"reference": "6c5ecfdbf7eeb9ea44bbc6dbc4a902c1930a392c",
"shasum": ""
},
"require": {
"php": ">=5.5.0",
"slim/flash": "^0.1.0",
"twig/twig": "^1.18"
},
"require-dev": {
"fabpot/php-cs-fixer": "1.*",
"phpunit/phpcov": "2.*",
"phpunit/phpunit": "4.8.0",
"satooshi/php-coveralls": "dev-master"
},
"type": "library",
"autoload": {
"psr-4": {
"Knlv\\Slim\\Views\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"GNU GPLv3"
],
"authors": [
{
"name": "Vassilis Kanellopoulos",
"email": "contact@kanellov.com",
"homepage": "http://kanellov.com"
}
],
"description": "A Twig extension to access Slim Flash messages in templates",
"keywords": [
"extension",
"flash",
"framework",
"message",
"provider",
"slim",
"twig",
"view"
],
"time": "2016-01-20 09:08:28"
},
{
"name": "monolog/monolog",
"version": "1.17.2",
......
<?php
/**
* gredu_labs
*
* @link https://github.com/eellak/gredu_labs for the canonical source repository
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
namespace GrEduLabsTest\Twig\Extension;
use GrEduLabs\Twig\Extension\Flash;
class FlashTest extends \PHPUnit_Framework_TestCase
{
private $flash;
private $mockMessages;
private $template;
protected function setUp()
{
$this->mockMessages = $this->getMock('\\Slim\\Flash\\Messages');
$this->mockMessages->expects($this->any())
->method('getMessages')
->will($this->returnValue([
'first' => ['test message 1'],
'other' => [
'test message 2',
'test message 3',
],
]));
$this->template = 'flashtemplate.twig';
$this->flash = new Flash($this->mockMessages, $this->template);
}
public function testConstructorSetArgs()
{
$this->assertAttributeSame($this->mockMessages, 'flash', $this->flash);
$this->assertAttributeSame($this->template, 'template', $this->flash);
}
public function testGetName()
{
$this->assertSame('slim-flash', $this->flash->getName());
}
public function testInitRuntimeSetEnvironment()
{
$env = $this->getMock('\\Twig_Environment');
$this->flash->initRuntime($env);
$this->assertAttributeSame($env, 'environment', $this->flash);
}
public function testGetFunctionsReturnsArrayWithSimpleFunction()
{
$functions = $this->flash->getFunctions();
$this->assertInternalType('array', $functions);
$this->assertNotEmpty($functions);
$theFunction = reset($functions);
$this->assertInstanceOf('\\Twig_SimpleFunction', $theFunction);
$this->assertSame('flash', $theFunction->getName());
$this->assertTrue($theFunction->needsEnvironment());
$theFunctionRefl = new \ReflectionClass($theFunction);
$options = $theFunctionRefl->getProperty('options');
$options->setAccessible(true);
$options = $options->getValue($theFunction);
$this->assertSame(['all'], $options['is_safe']);
}
public function testMessagesReturnsStringWithMessages()
{
$env = new \Twig_Environment(
new \Twig_Loader_Filesystem(__DIR__)
);
$this->flash->initRuntime($env);
$response = $this->flash->messages();
$expected = <<< EOF
first: test message 1
other: test message 2
other: test message 3
EOF;
$this->assertContains($expected, $response);
}
}
{% for message in messages %}
{{ class }}: {{ message }}
{% endfor %}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment