forked from Good-Old-Downloads/portal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twig.ext.php
53 lines (51 loc) · 1.93 KB
/
twig.ext.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
class AppExtension extends \Twig_Extension {
public function getFunctions(){
return array(
new \Twig_SimpleFunction('loadCSS', array($this, 'loadCSS'), array('is_safe' => array('html'))),
new \Twig_SimpleFunction('loadJS', array($this, 'loadJS'), array('is_safe' => array('html'))),
);
}
public function getFilters(){
return array(
new \Twig_SimpleFilter('long2ip', array($this, 'long2ip')),
new \Twig_SimpleFilter('convertBytes', array($this, 'convertBytes')),
);
}
public function loadCSS($styles, $integrity = null){
$ret = array();
if (is_array($styles)) {
foreach ($styles as $key => $value) {
if ($integrity !== null) {
if (!empty($integrity[$key])) {
$integStr = ' integrity="'.$integrity[$key].'"';
} else {
$integStr = '';
}
}
if (file_exists(__DIR__.'/web/'.$value)) {
$ret[] = '<link rel="stylesheet" href="'.$value.'?'.filemtime(__DIR__.'/web/'.$value).'"'.$integStr.'>';
}
}
}
return join($ret, "\n");
}
public function loadJS($scripts, $integrity = null){
$ret = array();
if (is_array($scripts)) {
foreach ($scripts as $key => $value) {
if ($integrity !== null) {
if (!empty($integrity[$key])) {
$integStr = ' integrity="'.$integrity[$key].'"';
} else {
$integStr = '';
}
}
if (file_exists(__DIR__.'/web/'.$value)) {
$ret[] = '<script src="'.$value.'?'.filemtime(__DIR__.'/web/'.$value).'"'.$integStr.'></script>';
}
}
}
return join($ret, "\n");
}
}