|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Common; |
|
4
|
|
|
|
|
5
|
|
|
use Twig_Environment; |
|
6
|
|
|
use Twig_SimpleFunction; |
|
7
|
|
|
use Rudra\Interfaces\ContainerInterface; |
|
8
|
|
|
|
|
9
|
|
|
trait TwigFunctions |
|
10
|
|
|
{ |
|
11
|
|
|
public function template(array $config): void |
|
12
|
|
|
{ |
|
13
|
|
|
parent::template($config); |
|
14
|
|
|
|
|
15
|
|
|
$this->getTwig()->addFunction(new Twig_SimpleFunction('d', function ($var) { |
|
16
|
|
|
return d($var); |
|
17
|
|
|
})); |
|
18
|
|
|
|
|
19
|
|
|
$this->getTwig()->addFunction(new Twig_SimpleFunction('date', function ($var) { |
|
20
|
|
|
return date($var); |
|
21
|
|
|
})); |
|
22
|
|
|
|
|
23
|
|
|
$this->getTwig()->addFunction(new Twig_SimpleFunction('auth', function () { |
|
24
|
|
|
return $this->container()->get('auth')->access(); |
|
25
|
|
|
})); |
|
26
|
|
|
|
|
27
|
|
|
$this->getTwig()->addFunction(new Twig_SimpleFunction('active', function ($link, $page) { |
|
28
|
|
|
if ($link == $page) { |
|
29
|
|
|
echo 'class="active"'; |
|
30
|
|
|
} |
|
31
|
|
|
})); |
|
32
|
|
|
|
|
33
|
|
|
$this->getTwig()->addFunction(new Twig_SimpleFunction('value', function ($var) { |
|
34
|
|
|
if ($this->container()->hasSession('value', $var)) { |
|
35
|
|
|
return $this->container()->getSession('value', $var); |
|
36
|
|
|
} |
|
37
|
|
|
})); |
|
38
|
|
|
|
|
39
|
|
|
$this->getTwig()->addFunction(new Twig_SimpleFunction('alert', function ($value, $style, $label = null) { |
|
40
|
|
|
if ($this->container()->hasSession('alert', $value)) { |
|
41
|
|
|
return '<div class="alert alert-' . $style . '" style="padding: 15px">' . $this->container()->getSession('alert', $value) . $label . '</div>'; |
|
42
|
|
|
} |
|
43
|
|
|
})); |
|
44
|
|
|
|
|
45
|
|
|
if ('development' == config('env')) { |
|
46
|
|
|
$debugbarRenderer = $this->container()->get('debugbar')->getJavascriptRenderer(); |
|
47
|
|
|
$this->getTwig()->addGlobal('debugbar', $debugbarRenderer); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$this->getTwig()->addGlobal('env', config('env')); |
|
51
|
|
|
$this->getTwig()->addGlobal('url', config('url')); |
|
52
|
|
|
$this->getTwig()->addGlobal('container', $this->container()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return Twig_Environment |
|
57
|
|
|
*/ |
|
58
|
|
|
public abstract function getTwig(): Twig_Environment; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return mixed |
|
62
|
|
|
*/ |
|
63
|
|
|
public abstract function container(): ContainerInterface; |
|
64
|
|
|
} |
|
65
|
|
|
|