1. Go to this page and download the library: Download brain/hierarchy library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
brain / hierarchy example snippets
// we will show template hierarchy for the main query
global $wp_query;
$hierarchy = new Brain\Hierarchy\Hierarchy();
var_export($hierarchy->hierarchy($wp_query));
add_action('template_redirect', function() {
$templates = (new Brain\Hierarchy\Hierarchy())->templates();
foreach($templates as $template) {
$path = get_theme_file_path("/templates/{$template}.phtml");
if (file_exists($path)) {
add_action('template_redirect', function(): void {
global $wp_query;
$queryTemplate = new \Brain\Hierarchy\QueryTemplate();
echo $queryTemplate->loadTemplate($wp_query);
exit();
});
add_action('template_redirect', function(): void {
$finder = new \Brain\Hierarchy\Finder\ByFolders([
__DIR__,
get_stylesheet_directory(),
get_template_directory(),
]);
$queryTemplate = new \Brain\Hierarchy\QueryTemplate($finder);
echo $queryTemplate->loadTemplate();
exit();
});
// This will look for *.phtml files.
$phtml_finder = new \Brain\Hierarchy\Finder\ByFolders(
[get_stylesheet_directory(), get_template_directory()],
'phtml'
);
// This will look for Twig files first, and fall back to standard PHP files if
// no matching Twig file was found.
$twig_finder = new \Brain\Hierarchy\Finder\ByFolders(
[get_stylesheet_directory(), get_template_directory()],
'twig',
'php'
);
add_action('template_redirect', function(): void {
$finder = new \Brain\Hierarchy\Finder\BySubfolder('templates');
$queryTemplate = new \Brain\Hierarchy\QueryTemplate($finder);
echo $queryTemplate->loadTemplate();
exit();
} );
add_action('template_redirect', function(): void {
$foldersFinder = new \Brain\Hierarchy\Finder\ByFolders();
$finder = new \Brain\Hierarchy\Finder\Localized($foldersFinder);
$queryTemplate = new \Brain\Hierarchy\QueryTemplate($finder);
echo $queryTemplate->loadTemplate();
exit();
} );
add_action('template_redirect', function() {
$symfonyFinder = new \Symfony\Component\Finder\Finder();
$symfonyFinder = $symfonyFinder->files()->in(__DIR__);
$finder = new \Brain\Hierarchy\Finder\SymfonyFinderAdapter($symfonyFinder);
$queryTemplate = new \Brain\Hierarchy\QueryTemplate($finder);
echo $queryTemplate->loadTemplate();
exit();
} );
$loader = new Loader\ExtensionMap([
'php|phtml' => new Loader\FileRequire(),
'mustache' => fn() => new MyMustacheAdapter(new Mustache_Engine),
'md' => MyMarkdownRenderer::class
]);
namespace My\Theme;
use Brain\Hierarchy\{Finder, Loader, QueryTemplate};
class MustacheTemplateLoader implements Loader\Loader
{
private $engine;
public function __construct(\Mustache_Engine $engine)
{
$this->engine = $engine;
}
public function load(string $templatePath): string
{
// let's use a filter to build some context for the template
$data = apply_filters('my_theme_data', ['query' => $GLOBALS['wp_query'], $templatePath);
return $this->engine->render(file_get_contents($templatePath), $data);
}
}
add_action('template_redirect', function() {
if (!QueryTemplate::mainQueryTemplateAllowed()) {
return;
}
$queryTemplate = new QueryTemplate(
// will look for "*.mustache" templates in theme's "/templates" subfolder
new Finder\BySubfolder('templates', 'mustache'),
// the loader class defined above
new MustacheTemplateLoader(new \Mustache_Engine())
);
// 3rd argument of loadTemplate() is passed by reference, and set to true if template is found
$content = $queryTemplate->loadTemplate(null, true, $found);
// if template was found, let's output it and exit, otherwise WordPress will continue its work
$found and die($content);
});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.