Skip to content

Add ability to attach library stylesheets and/or scripts #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 44 additions & 16 deletions pl_attach-library.function.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?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.
Expand All @@ -14,27 +16,53 @@
$yamlFile = glob('*.libraries.yml');
$yamlOutput = Yaml::parseFile($yamlFile[0]);
$scriptTags = [];
$styleTags = [];

// For each item in .libraries.yml file.
foreach($yamlOutput as $key => $value) {
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 = '';
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" => []]);
}
$scriptString = '<script data-name="reload" data-src="' . $path_prefix . $key . '"></script>';
$stringLoader = \PatternLab\Template::getStringLoader();
$scriptTags[$key] = $stringLoader->render(array("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" => []]);
}
}
}
}

return implode($scriptTags);
}, array('is_safe' => array('html')));
echo implode($styleTags);
echo implode($scriptTags);
}, ['is_safe' => ['html']]);