Skip to content

Commit

Permalink
[UPD] refactoring to accesse layout path via getter method
Browse files Browse the repository at this point in the history
  • Loading branch information
bim-g committed Sep 22, 2024
1 parent 0672cf7 commit d02c459
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
38 changes: 31 additions & 7 deletions src/Core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ class Application
*/
public static string $LAYOUT_CONTENT;
/**
* @var string|null
* @var string
*/
public static string $LAYOUT;
private static string $layout;

/**
* @var string
Expand All @@ -64,11 +64,14 @@ public function __construct(string $path, AppConfiguration $config)
self::$APP_LANG = self::$config_params['lang'] ?? 'fr';
self::$APP_TEMPLATE = self::$config_params['app_template'] ?? '';
self::$LAYOUT_CONTENT = 'layout_content';
self::$LAYOUT = '';
self::$layout = '';
self::$VIEW_FOLDER = '';
$this->router = new Router();
}

/**
* @return string
*/
public static function getRootDir(): string
{
return self::$root_dir;
Expand All @@ -88,32 +91,53 @@ public static function dumper($ex): void
}

/**
* Set the layout at the top of your application to be available everywhere.
* Define a layout to be used by all pages in the application.
* can be set at the top of your application to be available everywhere.
* @param string $layout
* @return void
*/
public static function setLayout(string $layout)
{
self::$LAYOUT = self::getRootDir().'/views/'.$layout;
self::$layout = self::getRootDir() . '/views/' . trim($layout, '/');
}

/**
* @param string $layout_name
* @return void
*/
public static function setLayoutContent(string $layout_name)
{
self::$LAYOUT_CONTENT = $layout_name;
}

/**
* @param string $folder_name
* @return void
*/
public static function setViewFolder(string $folder_name)
{
self::$VIEW_FOLDER = $folder_name;
}
public static function getLayout()

/**
* @return string|null
*/
public static function getLayout(): ?string
{
return self::$LAYOUT ;
return strlen(trim(self::$layout )) > 0 ? self::$layout : null;
}

/**
* @return string
*/
public static function getLayoutContent()
{
return self::$LAYOUT_CONTENT ;
}

/**
* @return string
*/
public static function getViewFolder()
{
return self::$VIEW_FOLDER ;
Expand Down
11 changes: 9 additions & 2 deletions src/Core/Views/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,22 @@ public function display(string $view): void
{
$view_file = $this->buildFilePath($view);
$render = $this->renderView($view_file);
if ($this->layout === '' && !$this->reset) {
if (! $this->getLayout() && !$this->reset) {
$this->layout = Application::getLayout();
}
if ($this->layout !== '') {
if ($this->getLayout()) {
$render = $this->renderLayout($render);
}
$this->buildAssetHead($render);
}

/**
* @return string|null
*/
public function getLayout(): ?string
{
return strlen(trim($this->layout)) > 0 ? $this->layout : null;
}
/**
* @param class-string<T> $file_name
*
Expand Down

0 comments on commit d02c459

Please sign in to comment.