|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the core-bundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2020 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\TwigFilter; |
|
15
|
|
|
use Twig\TwigFunction; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Javascript Twig extension. |
|
19
|
|
|
* |
|
20
|
|
|
* @author webeweb <https://github.com/webeweb/> |
|
21
|
|
|
* @package WBW\Bundle\CoreBundle\Twig\Extension |
|
22
|
|
|
*/ |
|
23
|
|
|
class JavascriptTwigExtension extends AbstractTwigExtension { |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Service name. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
const SERVICE_NAME = "wbw.core.twig.extension.javascript"; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get the Twig filters. |
|
34
|
|
|
* |
|
35
|
|
|
* @return TwigFilter[] Returns the Twig filters. |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getFilters() { |
|
38
|
|
|
return [ |
|
39
|
|
|
new TwigFilter("jsGtag", [$this, "jsGtag"], ["is_safe" => ["html"]]), |
|
40
|
|
|
]; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Get the Twig functions. |
|
45
|
|
|
* |
|
46
|
|
|
* @return TwigFunction[] Returns the Twig functions. |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getFunctions() { |
|
49
|
|
|
return [ |
|
50
|
|
|
new TwigFunction("jsGtag", [$this, "jsGtag"], ["is_safe" => ["html"]]), |
|
51
|
|
|
]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Displays a Google tag manager. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $id The id. |
|
58
|
|
|
* @return string Returns the Google tag manager. |
|
59
|
|
|
*/ |
|
60
|
|
|
public function jsGtag($id) { |
|
61
|
|
|
|
|
62
|
|
|
if (null === $id || "" === $id) { |
|
63
|
|
|
return ""; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$content = file_get_contents(__DIR__ . "/JavascriptTwigExtension.jsGtag.html"); |
|
67
|
|
|
|
|
68
|
|
|
return str_replace("{id}", $id, $content); |
|
69
|
|
|
} |
|
70
|
|
|
} |