Ernest Debruyne 2 år sedan
förälder
incheckning
7584b2c7fb
3 ändrade filer med 64 tillägg och 29 borttagningar
  1. 20
    23
      public/index.php
  2. 34
    0
      src/View/Twig.php
  3. 10
    6
      templates/navbar.html.twig

+ 20
- 23
public/index.php Visa fil

@@ -10,26 +10,29 @@ use Blog\DB\User;
use Blog\DB\Blog;
use Blog\DB\Bloglist;
use Blog\DB\DB;
use Blog\View\Twig;

require __DIR__ . '/../vendor/autoload.php';

$twig = new Twig('../templates');

$app = AppFactory::create();

$loader = new \Twig\Loader\FilesystemLoader('../templates');
$twig = new \Twig\Environment($loader);


ini_set('display_errors', 'Off');

function addNavbar($response)
{
global $twig;

$urls = [
["link" => "https://www.google.be", "name" => "Google"],
["link" => "https://www.facebook.com", "name" => "Facebook"],
["link" => "https://www.twitter.com", "name" => "Twitter"],
];
$twig->addBlockVariable('navbar', ['urls' => $urls]);

$response->getBody()->write("<html><head></head><body>");
if (isset($_SESSION['username'])) {
$response->getBody()->write('<div><a href="/">Index</a> | <a href="/blog/create">Create Blog</a> | <a href="/logout">Logout</a></div>');
} else {
$response->getBody()->write('<div><a href="/">Index</a> | <a href="/login">Login</a></div>');
}
return;
}

function addFooter($response)
@@ -59,21 +62,15 @@ $app->get('/', function (Request $request, Response $response, array $args) {
init();
global $twig;
$template = $twig->load('index.html.twig');
$a = $template->render(["key" => "value", "key2" => ["val1", "val2", "val3"], "name" => "John", "lastname" => "Doe", "loggedIn" => isset($_SESSION['username'])]);
$response->getBody()->write($a);
// addNavbar($response);
// $response->getBody()->write('<hr/><h1>Onze blog</h1>');
// $response->getBody()->write('<ul>');
$vars = [
"key" => "value",
'key2' => ["val1", "val2", "val3"],
"key3" => ["x1" => "y1", "x2" => "y2"],
"loggedIn" => isset($_SESSION['username'])
];
// foreach ($_SESSION['blogs'] as $art) {
// $response->getBody()->write('<li><a href="/blog/' . $art['slug'] . '">' . $art['title'] . '</a></li>');
// }
// $response->getBody()->write('</ul>');
// $response->getBody()->write('<hr/>');
// addFooter($response);

$a = $twig->render('index.html.twig', $vars);
$response->getBody()->write($a);

return $response;
});

+ 34
- 0
src/View/Twig.php Visa fil

@@ -0,0 +1,34 @@
<?php

namespace Blog\View;

use \Twig\Loader\FilesystemLoader;
use \Twig\Environment;

class Twig {

protected $twig;
protected $variables;

public function __construct($tmpl_folder) {
$loader = new \Twig\Loader\FilesystemLoader($tmpl_folder);
$this->twig = new \Twig\Environment($loader, []);
$this->variables = ['navbar' => [], 'messages' => [], 'footer' => []];
}

protected function load($tmpl) {
return $this->twig->load($tmpl);
}

public function addBlockVariable($block, $data) {
$current = $this->variables[$block];
$new = array_merge($current, $data);
$this->variables = $new;
}

public function render($tmpl, $vars) {
$template = $this->load($tmpl);
$variables = array_merge($this->variables, $vars);
return $template->render($variables);
}
}

+ 10
- 6
templates/navbar.html.twig Visa fil

@@ -1,9 +1,13 @@
<div><a href="/">Index</a> |
{% if loggedIn %}
{% if loggedIn %}
<a href="/blog/create">Create Blog</a> |
<a href="/logout">Logout</a></div>
<a href="/blog/create">Create Blog</a> |
<a href="/logout">Logout</a></div>
{% else %}
<a href="/login">Login</a></div>
{% endif %}
{% else %}
<a href="/login">Login</a></div>
{% endif %}

{% for url in navbar.urls %}
<li><a href="{{url.link}}">{{url.name}}</a></li>
{% endfor %}

Laddar…
Avbryt
Spara