-
Notifications
You must be signed in to change notification settings - Fork 6
/
pl_attach-library.function.php
40 lines (34 loc) · 1.31 KB
/
pl_attach-library.function.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
<?php
/**
* @file
* Add "attach_library" function for Pattern Lab.
*/
use Symfony\Component\Yaml\Yaml;
$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 = [];
// For each item in .libraries.yml file.
foreach($yamlOutput as $key => $value) {
// If the library exists.
if ($key === $libraryName) {
$files = $yamlOutput[$key]['js'];
// For each file, create an async script to insert to the Twig component.
foreach($files as $key => $file) {
// By default prefix paths with a /, 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 = \PatternLab\Template::getStringLoader();
$scriptTags[$key] = $stringLoader->render(array("string" => $scriptString, "data" => []));
}
}
}
return implode($scriptTags);
}, array('is_safe' => array('html')));