-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpl_attach-library.function.php
More file actions
68 lines (60 loc) · 2.43 KB
/
pl_attach-library.function.php
File metadata and controls
68 lines (60 loc) · 2.43 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/**
* @file
* Add "attach_library" function for Pattern Lab.
*/
use Symfony\Component\Yaml\Yaml;
use PatternLab\Template;
$function = new Twig_SimpleFunction('attach_library', function ($string) {
// Get Library Name from string.
$libraryName = substr($string, strpos($string, "/") + 1);
// Find Library in libraries.yml file.
$yamlFile = glob('*.libraries.yml');
$yamlOutput = Yaml::parseFile($yamlFile[0]);
$scriptTags = [];
$styleTags = [];
// For each item in .libraries.yml file.
foreach ($yamlOutput as $key => $value) {
// If the library exists.
if ($key === $libraryName) {
if (isset($yamlOutput[$key]['js'])) {
$js_files = $yamlOutput[$key]['js'];
}
// Check if CSS files are defined.
if (isset($yamlOutput[$key]['css'])) {
// Create a single array from stylesheets groups (base, theme).
$css_files = array_reduce($yamlOutput[$key]['css'], 'array_merge', []);
}
// For each JS file, create an async script to insert to the Twig component.
if (isset($js_files)) {
foreach ($js_files as $key => $file) {
// By default prefix paths with relative path to dist folder,
// but remove this for external JS as it would break URLs.
$path_prefix = '/';
if (isset($file['type']) && $file['type'] === 'external') {
$path_prefix = '';
}
$scriptString = '<script data-name="reload" data-src="' . $path_prefix . $key . '"></script>';
$stringLoader = Template::getStringLoader();
$scriptTags[$key] = $stringLoader->render(["string" => $scriptString, "data" => []]);
}
}
// For each CSS file, create a stylesheet link to insert to the Twig component.
if (isset($css_files)) {
foreach ($css_files as $key => $file) {
// By default prefix paths with relative path to dist folder,
// but remove this for external CSS as it would break URLs.
$path_prefix = '/';
if (isset($file['type']) && $file['type'] === 'external') {
$path_prefix = '';
}
$linkString = '<link rel="stylesheet" href="' . $path_prefix . $key . '">';
$stringLoader = Template::getStringLoader();
$styleTags[$key] = $stringLoader->render(["string" => $linkString, "data" => []]);
}
}
}
}
echo implode($styleTags);
echo implode($scriptTags);
}, ['is_safe' => ['html']]);