@@ -0,0 +1,43 @@ | |||
<?php | |||
namespace Shop\View; | |||
use Twig\Loader\FilesystemLoader; | |||
use Twig\Environment; | |||
class Twig | |||
{ | |||
protected $twig; | |||
protected $variables; | |||
public function __construct($tmpl_folder) | |||
{ | |||
$loader = new FilesystemLoader($tmpl_folder); | |||
$this->twig = new Environment($loader, []); | |||
} | |||
protected function load($tmpl) | |||
{ | |||
return $this->twig->load($tmpl); | |||
} | |||
// add block variables to the global variable bag | |||
public function addBlockVariable($block, $data) | |||
{ | |||
$current = $this->variables[$block]; | |||
if ($current) { | |||
$new = array_merge($current, $data); | |||
} else { | |||
$new = $data; | |||
} | |||
$this->variables[$block] = $new; | |||
} | |||
public function render($tmpl, $vars) | |||
{ | |||
$template = $this->load($tmpl); | |||
$variables = array_merge($this->variables, $vars); | |||
return $template->render($variables); | |||
} | |||
} |
@@ -7,6 +7,7 @@ use Psr\Http\Message\ServerRequestInterface as Request; | |||
use Slim\Factory\AppFactory; | |||
use DI\Container; | |||
use Shop\DB\myDB; | |||
use Shop\View\Twig; | |||
require __DIR__ . '/../vendor/autoload.php'; | |||
@@ -14,10 +15,15 @@ $container = new Container(); | |||
AppFactory::setContainer($container); | |||
$app = AppFactory::create(); | |||
// $container->set('db', function() { | |||
// $db = new myDB(); | |||
// return $db; | |||
// }); | |||
$container->set('twig', function () { | |||
$twig = new Twig('../templates'); | |||
return $twig; | |||
}); | |||
$container->set('db', function() { | |||
$db = new myDB(); | |||
return $db; | |||
}); | |||
$app->map(['GET', 'POST'], "/orders/{client}/create", function (Request $request, Response $response, array $args) { | |||
if ($request->getMethod() == "GET") { |