Skip to content

Instantly share code, notes, and snippets.

@nunof07
Created April 12, 2019 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nunof07/bd0dbdd7d8fb917afaaf00ec5e9a7d1f to your computer and use it in GitHub Desktop.
Save nunof07/bd0dbdd7d8fb917afaaf00ec5e9a7d1f to your computer and use it in GitHub Desktop.
Automatically register component aliases in Sage.
<?php
/**
* Register component aliases.
*/
add_action('after_setup_theme', function () {
$template_directory = "views/components/";
$path = get_stylesheet_directory() . '/' . $template_directory;
if (!is_dir($path)) {
return;
}
$dir = new \DirectoryIterator(\locate_template($template_directory));
foreach ($dir as $fileinfo) {
if ($fileinfo->isDot()) {
continue;
}
$slug = str_replace('.blade.php', '', $fileinfo->getFilename());
$alias = preg_replace('/[^\da-z]/i', '', $slug);
sage('blade')->compiler()->component("components.$slug", $alias);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment