|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the core-bundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2018 WEBEWEB |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace WBW\Bundle\CoreBundle\Twig\Extension; |
|
13
|
|
|
|
|
14
|
|
|
use Twig_Environment; |
|
15
|
|
|
use Twig_Extension; |
|
16
|
|
|
use WBW\Bundle\CoreBundle\Navigation\NavigationInterface; |
|
17
|
|
|
use WBW\Library\Core\Argument\StringHelper; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Abstract Twig extension. |
|
21
|
|
|
* |
|
22
|
|
|
* @author webeweb <https://github.com/webeweb/> |
|
23
|
|
|
* @package WBW\Bundle\CoreBundle\Twig\Extension |
|
24
|
|
|
* @abstract |
|
25
|
|
|
*/ |
|
26
|
|
|
abstract class AbstractTwigExtension extends Twig_Extension { |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Default content. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
const DEFAULT_CONTENT = " "; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Default href. |
|
37
|
|
|
* |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
const DEFAULT_HREF = NavigationInterface::NAVIGATION_HREF_DEFAULT; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Twig environment. |
|
44
|
|
|
* |
|
45
|
|
|
* @var Twig_Environment |
|
46
|
|
|
*/ |
|
47
|
|
|
private $twigEnvironment; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Constructor. |
|
51
|
|
|
* |
|
52
|
|
|
* @param Twig_Environment $twigEnvironment The twig environment. |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function __construct(Twig_Environment $twigEnvironment) { |
|
55
|
|
|
$this->setTwigEnvironment($twigEnvironment); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Displays a Core HTML element. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $element The object. |
|
62
|
|
|
* @param string $content The content. |
|
63
|
|
|
* @param array $attrs The attributes. |
|
64
|
|
|
* @return string Returns the Bootstrap HTML element. |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function coreHTMLElement($element, $content, array $attrs = []) { |
|
67
|
|
|
|
|
68
|
|
|
// Initialize the templates. |
|
69
|
|
|
$template = "<%element%%attributes%>%innerHTML%</%element%>"; |
|
70
|
|
|
|
|
71
|
|
|
// Initialize the attributes. |
|
72
|
|
|
$attributes = trim(StringHelper::parseArray($attrs)); |
|
73
|
|
|
if (0 < strlen($attributes)) { |
|
74
|
|
|
$attributes = " " . $attributes; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// Initialize the parameters. |
|
78
|
|
|
$innerHTML = null !== $content ? trim($content, " ") : ""; |
|
79
|
|
|
|
|
80
|
|
|
// Return the HTML. |
|
81
|
|
|
return StringHelper::replace($template, ["%element%", "%attributes%", "%innerHTML%"], [trim($element), $attributes, $innerHTML]); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Get the twig environment. |
|
86
|
|
|
* |
|
87
|
|
|
* @return Twig_Environment Returns the twig environment. |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getTwigEnvironment() { |
|
90
|
|
|
return $this->twigEnvironment; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Set the twig. |
|
95
|
|
|
* |
|
96
|
|
|
* @param Twig_Environment $twigEnvironment The twig environment. |
|
97
|
|
|
* @return AbstractTwigExtension Returns this twig extension. |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function setTwigEnvironment(Twig_Environment $twigEnvironment) { |
|
100
|
|
|
$this->twigEnvironment = $twigEnvironment; |
|
101
|
|
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|