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\Bundle\CoreBundle\Service\TwigEnvironmentTrait; |
18
|
|
|
use WBW\Library\Core\Argument\StringHelper; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Abstract Twig extension. |
22
|
|
|
* |
23
|
|
|
* @author webeweb <https://github.com/webeweb/> |
24
|
|
|
* @package WBW\Bundle\CoreBundle\Twig\Extension |
25
|
|
|
* @abstract |
26
|
|
|
*/ |
27
|
|
|
abstract class AbstractTwigExtension extends Twig_Extension { |
28
|
|
|
|
29
|
|
|
use TwigEnvironmentTrait; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Default content. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
const DEFAULT_CONTENT = " "; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Default href. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
const DEFAULT_HREF = NavigationInterface::NAVIGATION_HREF_DEFAULT; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Constructor. |
47
|
|
|
* |
48
|
|
|
* @param Twig_Environment $twigEnvironment The twig environment. |
49
|
|
|
*/ |
50
|
|
|
protected function __construct(Twig_Environment $twigEnvironment) { |
51
|
|
|
$this->setTwigEnvironment($twigEnvironment); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Displays a Core HTML element. |
56
|
|
|
* |
57
|
|
|
* @param string $element The object. |
58
|
|
|
* @param string $content The content. |
59
|
|
|
* @param array $attrs The attributes. |
60
|
|
|
* @return string Returns the HTML element. |
61
|
|
|
*/ |
62
|
|
|
public static function coreHTMLElement($element, $content, array $attrs = []) { |
63
|
|
|
|
64
|
|
|
// Initialize the templates. |
65
|
|
|
$template = "<%element%%attributes%>%innerHTML%</%element%>"; |
66
|
|
|
|
67
|
|
|
// Initialize the attributes. |
68
|
|
|
$attributes = trim(StringHelper::parseArray($attrs)); |
69
|
|
|
if (0 < strlen($attributes)) { |
70
|
|
|
$attributes = " " . $attributes; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// Initialize the parameters. |
74
|
|
|
$innerHTML = null !== $content ? trim($content, " ") : ""; |
75
|
|
|
|
76
|
|
|
// Return the HTML. |
77
|
|
|
return StringHelper::replace($template, ["%element%", "%attributes%", "%innerHTML%"], [trim($element), $attributes, $innerHTML]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|