1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- <?php
-
- namespace Blog\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, []);
- $this->variables = ['navbar' => [], 'messages' => ['msgs' => []], 'footer' => []];
- }
-
- protected function load($tmpl)
- {
- 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)
- {
- $current = $this->variables[$block];
- $new = array_merge($current, $data);
- $this->variables[$block] = $new;
- }
-
- public function render($tmpl, $vars)
- {
- $template = $this->load($tmpl);
- $variables = array_merge($this->variables, $vars);
- return $template->render($variables);
- }
- }
|