Skip to content

Commit

Permalink
feat: override sections layout
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudLigny committed Feb 17, 2025
1 parent 0ae7a0a commit 259ef11
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
3 changes: 3 additions & 0 deletions config/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@
'dir' => 'components', // components directory
'ext' => 'twig', // components files extension
],
//'sections' => [ // override layout name of sections (optional)
// '<section>' => '<layout>',
//]
],
'themes' => [
'dir' => 'themes', // where themes are stored
Expand Down
12 changes: 12 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,18 @@ public function getLayoutsInternalPath(): string
return Util::joinPath(__DIR__, '..', (string) $this->get('layouts.internal.dir'));
}

/**
* Returns the layout for a section.
*/
public function getLayoutSection(?string $section): ?string
{
if ($layout = $this->get('layouts.sections')[$section] ?? null) {
return $layout;
}

return $section;
}

/**
* Returns the path of translations directory.
*/
Expand Down
16 changes: 9 additions & 7 deletions src/Renderer/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ protected static function lookup(CollectionPage $page, string $format, \Cecil\Co

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

switch ($page->getType()) {
case PageType::HOMEPAGE->value:
Expand Down Expand Up @@ -112,9 +114,9 @@ protected static function lookup(CollectionPage $page, string $format, \Cecil\Co
"_default/list.$format.$ext",
];
if ($page->getPath()) {
$layouts = array_merge(["section/{$page->getSection()}.$format.$ext"], $layouts);
$layouts = array_merge(["{$page->getSection()}/list.$format.$ext"], $layouts);
$layouts = array_merge(["{$page->getSection()}/index.$format.$ext"], $layouts);
$layouts = array_merge(["section/{$section}.$format.$ext"], $layouts);
$layouts = array_merge(["{$section}/list.$format.$ext"], $layouts);
$layouts = array_merge(["{$section}/index.$format.$ext"], $layouts);
}
if ($page->hasVariable('layout')) {
$layouts = array_merge(["$layout.$format.$ext"], $layouts);
Expand Down Expand Up @@ -156,13 +158,13 @@ protected static function lookup(CollectionPage $page, string $format, \Cecil\Co
if ($page->hasVariable('layout')) {
$layouts = array_merge(["_default/$layout.$format.$ext"], $layouts);
}
if ($page->getSection()) {
$layouts = array_merge(["{$page->getSection()}/page.$format.$ext"], $layouts);
if ($section) {
$layouts = array_merge(["{$section}/page.$format.$ext"], $layouts);
}
if ($page->hasVariable('layout')) {
$layouts = array_merge(["$layout.$format.$ext"], $layouts);
if ($page->getSection()) {
$layouts = array_merge(["{$page->getSection()}/$layout.$format.$ext"], $layouts);
if ($section) {
$layouts = array_merge(["{$section}/$layout.$format.$ext"], $layouts);
}
}
}
Expand Down

0 comments on commit 259ef11

Please sign in to comment.