Explorar el Código

flash messages

Twig
Ruben De Baets hace 2 años
padre
commit
964bc1afaa
Se han modificado 2 ficheros con 12 adiciones y 6 borrados
  1. 4
    5
      public/index.php
  2. 8
    1
      src/View/Twig.php

+ 4
- 5
public/index.php Ver fichero

@@ -32,6 +32,8 @@ function addNavbar()
["link" => "https://www.facebook.com", "name" => "Facebook"],
["link" => "https://www.twiter.com", "name" => "Twitter"]
];
$err = ['type' => 'notice', 'text' => "we did it!"];
$twig->addMessage($err);
$twig->addBlockVariable('navbar', ['urls' => $urls]);
}

@@ -58,15 +60,12 @@ $app->get('/', function (Request $request, Response $response, array $args) {
}

$vars = [
"key" => "value",
'key2' => ["val1", "val2", "val3"],
"key3" => ["x1" => "y1", "x2" => "y2"],
"loggedIn" => isset($_SESSION['username']),
"content" => "Dit is de main body met info"

];

global $twig;
$err = ['type' => 'error', 'text' => "something's wrong"];
$twig->addMessage($err);
$a = $twig->render('index.html.twig', $vars);
$response->getBody()->write($a);
return $response;

+ 8
- 1
src/View/Twig.php Ver fichero

@@ -15,7 +15,7 @@ class Twig
{
$loader = new FilesystemLoader($tmpl_folder);
$this->twig = new Environment($loader, []);
$this->variables = ['navbar' => [], 'messages' => [], 'footer' => []];
$this->variables = ['navbar' => [], 'messages' => ['msgs' => []], 'footer' => []];
}

protected function load($tmpl)
@@ -23,6 +23,13 @@ class Twig
return $this->twig->load($tmpl);
}

public function addMessage($msg)
{
$current = $this->variables['messages']['msgs'];
$current[] = $msg;
$this->variables['messages']['msgs'] = $current;
}

// add block variables to the global variable bag
public function addBlockVariable($block, $data)
{

Cargando…
Cancelar
Guardar