• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Cecilapp / Cecil / 25614781904

09 May 2026 11:45PM UTC coverage: 79.921%. First build
25614781904

Pull #2321

github

web-flow
Merge edcaf6748 into 26800c1a6
Pull Request #2321: feat: add sub-section support for pages

62 of 196 new or added lines in 4 files covered. (31.63%)

3451 of 4318 relevant lines covered (79.92%)

0.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.27
/src/Renderer/Layout.php
1
<?php
2

3
/**
4
 * This file is part of Cecil.
5
 *
6
 * (c) Arnaud Ligny <arnaud@ligny.fr>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace Cecil\Renderer;
15

16
use Cecil\Collection\Page\Page as CollectionPage;
17
use Cecil\Collection\Page\Type as PageType;
18
use Cecil\Exception\RuntimeException;
19
use Cecil\Util;
20

21
/**
22
 * Layout renderer class.
23
 *
24
 * This class is responsible for finding and returning the appropriate layout file
25
 * for a given page based on its type, section, and other variables.
26
 * It looks for layout files in various directories such as the site's layouts directory,
27
 * the theme's layouts directory, and the internal resources/layouts directory.
28
 */
29
class Layout
30
{
31
    /**
32
     * Twig template extension.
33
     * @var string
34
     */
35
    public const EXT = 'twig';
36

37
    /**
38
     * Layout files finder.
39
     *
40
     * @throws RuntimeException
41
     */
42
    public static function finder(CollectionPage $page, string $format, \Cecil\Config $config): array
43
    {
44
        $layout = 'unknown';
1✔
45

46
        // which layouts, in what format, could be used for the page?
47
        $layouts = self::lookup($page, $format, $config);
1✔
48

49
        // take the first available layout
50
        foreach ($layouts as $layout) {
1✔
51
            $layout = Util::joinFile($layout);
1✔
52
            // is it in `layouts/` dir?
53
            if (Util\File::getFS()->exists(Util::joinFile($config->getLayoutsPath(), $layout))) {
1✔
54
                return [
1✔
55
                    'scope' => 'site',
1✔
56
                    'file'  => $layout,
1✔
57
                ];
1✔
58
            }
59
            // is it in `<theme>/layouts/` dir?
60
            if ($config->hasTheme()) {
1✔
61
                $themes = $config->getTheme();
1✔
62
                foreach ($themes ?? [] as $theme) {
1✔
63
                    if (Util\File::getFS()->exists(Util::joinFile($config->getThemeDirPath($theme, 'layouts'), $layout))) {
1✔
64
                        return [
1✔
65
                            'scope' => $theme,
1✔
66
                            'file'  => $layout,
1✔
67
                        ];
1✔
68
                    }
69
                }
70
            }
71
            // is it in resources/layouts/ dir?
72
            if (Util\File::getFS()->exists(Util::joinPath($config->getLayoutsInternalPath(), $layout))) {
1✔
73
                return [
1✔
74
                    'scope' => 'cecil',
1✔
75
                    'file'  => $layout,
1✔
76
                ];
1✔
77
            }
78
        }
79

80
        throw new RuntimeException(\sprintf('Layout "%s" not found (page: %s).', $layout, $page->getId()));
×
81
    }
82

83
    /**
84
     * Templates lookup rules.
85
     *
86
     * @see self::finder()
87
     */
88
    protected static function lookup(CollectionPage $page, string $format, \Cecil\Config $config): array
89
    {
90
        $ext = self::EXT;
1✔
91

92
        // remove potential redundant extension
93
        $layout = str_replace(".$ext", '', (string) $page->getVariable('layout'));
1✔
94
        // page section or layout mapping
95
        $section = $config->getLayoutSection($page->getSection());
1✔
96

97
        switch ($page->getType()) {
1✔
98
            case PageType::HOMEPAGE->value:
1✔
99
                $layouts = [
1✔
100
                    // "$layout.$format.$ext",
101
                    "index.$format.$ext",
1✔
102
                    "home.$format.$ext",
1✔
103
                    "list.$format.$ext",
1✔
104
                ];
1✔
105
                if ($page->hasVariable('layout')) {
1✔
106
                    $layouts = array_merge(["$layout.$format.$ext"], $layouts, ["_default/$layout.$format.$ext"]);
×
107
                }
108
                $layouts = array_merge($layouts, [
1✔
109
                    // "_default/$layout.$format.$ext",
110
                    "_default/index.$format.$ext",
1✔
111
                    "_default/home.$format.$ext",
1✔
112
                    "_default/list.$format.$ext",
1✔
113
                    "_default/page.$format.$ext",
1✔
114
                ]);
1✔
115
                break;
1✔
116
            case PageType::SECTION->value:
1✔
117
                $layouts = [
1✔
118
                    // "$layout.$format.$ext",
119
                    // "$section/index.$format.$ext",
120
                    // "$section/list.$format.$ext",
121
                    // "section/$section.$format.$ext",
122
                    "_default/section.$format.$ext",
1✔
123
                    "list.$format.$ext",
1✔
124
                    "_default/list.$format.$ext",
1✔
125
                ];
1✔
126
                if ($section && $page->getPath()) {
1✔
127
                    $layouts = array_merge(["section/{$section}.$format.$ext"], $layouts);
1✔
128
                    $layouts = array_merge(["{$section}/list.$format.$ext"], $layouts);
1✔
129
                    $layouts = array_merge(["{$section}/index.$format.$ext"], $layouts);
1✔
130
                    // Sub-section support: also try parent section layouts.
131
                    // For "blog/tutorials", also try "blog/list", "blog/index", "section/blog".
132
                    if (str_contains($section, '/')) {
1✔
NEW
133
                        $parentSection = substr($section, 0, strrpos($section, '/'));
×
NEW
134
                        $layouts = array_merge(
×
NEW
135
                            [
×
NEW
136
                                "{$section}/index.$format.$ext",
×
NEW
137
                                "{$section}/list.$format.$ext",
×
NEW
138
                                "section/{$section}.$format.$ext",
×
NEW
139
                                "{$parentSection}/list.$format.$ext",
×
NEW
140
                                "section/{$parentSection}.$format.$ext",
×
NEW
141
                            ],
×
NEW
142
                            $layouts
×
NEW
143
                        );
×
144
                    }
145
                }
146
                if ($page->hasVariable('layout')) {
1✔
147
                    $layouts = array_merge(["$layout.$format.$ext"], $layouts);
×
148
                }
149
                break;
1✔
150
            case PageType::VOCABULARY->value:
1✔
151
                $layouts = [
1✔
152
                    // "taxonomy/$plural.$format.$ext", // e.g.: taxonomy/tags.html.twig
153
                    "vocabulary.$format.$ext",          // e.g.: vocabulary.html.twig
1✔
154
                    "_default/vocabulary.$format.$ext", // e.g.: _default/vocabulary.html.twig
1✔
155
                ];
1✔
156
                if ($page->hasVariable('plural')) {
1✔
157
                    $layouts = array_merge(["taxonomy/{$page->getVariable('plural')}.$format.$ext"], $layouts);
1✔
158
                }
159
                break;
1✔
160
            case PageType::TERM->value:
1✔
161
                $layouts = [
1✔
162
                    // "taxonomy/$term.$format.$ext",     // e.g.: taxonomy/velo.html.twig
163
                    // "taxonomy/$singular.$format.$ext", // e.g.: taxonomy/tag.html.twig
164
                    "term.$format.$ext",                  // e.g.: term.html.twig
1✔
165
                    "_default/term.$format.$ext",         // e.g.: _default/term.html.twig
1✔
166
                    "_default/list.$format.$ext",         // e.g.: _default/list.html.twig
1✔
167
                ];
1✔
168
                if ($page->hasVariable('term')) {
1✔
169
                    $layouts = array_merge(["taxonomy/{$page->getVariable('term')}.$format.$ext"], $layouts);
1✔
170
                }
171
                if ($page->hasVariable('singular')) {
1✔
172
                    $layouts = array_merge(["taxonomy/{$page->getVariable('singular')}.$format.$ext"], $layouts);
1✔
173
                }
174
                break;
1✔
175
            default:
176
                $layouts = [
1✔
177
                    // "$section/$layout.$format.$ext",
178
                    // "$layout.$format.$ext",
179
                    // "$section/page.$format.$ext",
180
                    // "_default/$layout.$format.$ext",
181
                    // "page.$format.$ext",
182
                    "_default/page.$format.$ext",
1✔
183
                ];
1✔
184
                $layouts = array_merge(["page.$format.$ext"], $layouts);
1✔
185
                if ($page->hasVariable('layout')) {
1✔
186
                    $layouts = array_merge(["_default/$layout.$format.$ext"], $layouts);
1✔
187
                }
188
                if ($section) {
1✔
189
                    $layouts = array_merge(["{$section}/page.$format.$ext"], $layouts);
1✔
190
                }
191
                if ($page->hasVariable('layout')) {
1✔
192
                    $layouts = array_merge(["$layout.$format.$ext"], $layouts);
1✔
193
                    if ($section) {
1✔
194
                        $layouts = array_merge(["{$section}/$layout.$format.$ext"], $layouts);
1✔
195
                    }
196
                }
197
        }
198

199
        // add localized layouts
200
        if ($page->getVariable('language') !== $config->getLanguageDefault()) {
1✔
201
            foreach ($layouts as $key => $value) {
1✔
202
                $layouts = array_merge(\array_slice($layouts, 0, $key), [str_replace(".$ext", ".{$page->getVariable('language')}.$ext", $value)], \array_slice($layouts, $key));
1✔
203
            }
204
        }
205

206
        return $layouts;
1✔
207
    }
208
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc