-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstyleguide.php
More file actions
85 lines (65 loc) · 2.42 KB
/
styleguide.php
File metadata and controls
85 lines (65 loc) · 2.42 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/**
* Template for styleguide routes (/styleguide and subpages)
*/
use Timber\Timber;
$current_slug = get_query_var('styleguide_page');
$context = Timber::context();
$context['styleguide_menu'] = get_styleguide_sections_recursive('', '', $current_slug);
$subpage = get_query_var('styleguide_page');
if ($subpage) {
$context['section'] = $subpage;
$parts = explode('/', $subpage);
$last = end($parts);
$context['title'] = ucwords(str_replace(['-', '_'], ' ', $last));
$data_path = get_template_directory() . "/views/styleguide/{$subpage}/data.json";
if (file_exists($data_path)) {
$json_data = json_decode(file_get_contents($data_path), true);
if ($json_data) {
$context['data'] = $json_data;
}
}
$template_path = "styleguide/{$subpage}/template.twig";
if (locate_template("views/{$template_path}")) {
Timber::render($template_path, $context);
} else {
Timber::render("styleguide/section.twig", $context); // Fallback
}
} else {
Timber::render("styleguide.twig", $context);
}
function get_styleguide_sections_recursive($base_path = '', $url_prefix = '', $current_slug = '') {
$full_path = get_template_directory() . '/views/styleguide' . $base_path;
$sections = [];
if (!is_dir($full_path)) {
return $sections;
}
foreach (scandir($full_path) as $item) {
if ($item === '.' || $item === '..') continue;
$item_path = $full_path . '/' . $item;
$relative_path = ltrim($base_path . '/' . $item, '/');
$url = '/styleguide' . $url_prefix . '/' . $item;
if (is_dir($item_path)) {
// Check if it has a template.twig — it’s a renderable section
$has_template = file_exists($item_path . '/template.twig');
$children = get_styleguide_sections_recursive(
$base_path . '/' . $item,
$url_prefix . '/' . $item,
$current_slug
);
if ($has_template || !empty($children)) {
$section = [
'slug' => $relative_path,
'title' => ucwords(str_replace(['-', '_'], ' ', $item)),
'link' => $url,
'is_active' => ($relative_path === $current_slug),
];
if (!empty($children)) {
$section['children'] = $children;
}
$sections[] = $section;
}
}
}
return $sections;
}