Skip to content

Commit

Permalink
update file
Browse files Browse the repository at this point in the history
  • Loading branch information
juancristobalgd1 authored Jun 21, 2024
1 parent 82a1b60 commit 2680ee6
Showing 1 changed file with 41 additions and 26 deletions.
67 changes: 41 additions & 26 deletions src/libraries/Views/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,25 +127,45 @@ public function esc(string $string): self
*/
public function resolver(): self
{
$data = $this->data;
$view = $this->contents;
$filename = $this->filename;
$content = $this->renderView();
$output = $this->includeLayout($content);
$this->saveCache($this->filename, $output, $this->data);
$this->contents = $output;
$this->resolved = true;

return $this;
}

/**
* Renders the view content based on the current environment.
*/
private function renderView(): string
{
$content = $this->isProduction()
? $this->contents
: "<!--VIEW START . {$this->filename} -->\n {$this->contents} \n<!-- VIEW END {$this->filename} -->\n\n";

$_content = app()->isProduction()
? $view : "<!--VIEW START . $filename -->\n $view \n<!-- VIEW END $filename -->\n\n";
return $content;
}

/**
* Includes the layout file if the layout is enabled.
*/
private function includeLayout(string $content): string
{
if ($this->withLayout) {
$output = $this->file($this->layoutPath . DIRECTORY_SEPARATOR . $this->layout . '.php', ['_content' => $_content]); // include layout
} else {
$output = $view;
return $this->file($this->layoutPath . DIRECTORY_SEPARATOR . $this->layout . '.php', ['_content' => $content]);
}

$this->saveCache($filename, $output, $data);
$this->contents = $output;
unset($data, $view, $output);
$this->resolved = true;
return $content;
}

return $this;
/**
* Determines if the application is running in production mode.
*/
private function isProduction(): bool
{
return app()->isProduction();
}

/**
Expand All @@ -157,7 +177,8 @@ public function resolver(): self
*/
function get(): string
{
if (!$this->resolved) $this->resolver();
if (!$this->resolved)
$this->resolver();

// If it is stored in cache
if (($output = $this->getFromCache($this->filename, $this->data)) !== false) {
Expand Down Expand Up @@ -201,21 +222,21 @@ private function saveCache(?string $filename, ?string $output, array $data = [])
}

/**
* Renderiza un archivo de plantilla y devuelve la salida como cadena.
* Renders a template file and returns the output as a string.
*/
public function file(string $templatePath, array $data = []): ?string
{
extract(array_merge(self::$tempData, $this->data, $data), EXTR_OVERWRITE);

$mergedData = array_merge(self::$tempData, $this->data, $data);
extract($mergedData, EXTR_OVERWRITE);
ob_start();
$_outputLevel = ob_get_level();
$outputLevel = ob_get_level();

try {
ob_implicit_flush(false);
require $templatePath;
return (string) ob_get_clean();
return ob_get_clean();
} catch (\Throwable $e) {
while (ob_get_level() >= $_outputLevel) {
while (ob_get_level() >= $outputLevel) {
ob_end_clean();
}

Expand All @@ -225,9 +246,6 @@ public function file(string $templatePath, array $data = []): ?string

/**
* Adds CSS styles to the <head> section of the HTML content.
*
* This method searches for the closing </head> tag in the HTML content and injects
* the provided CSS styles just before it.
*/
public function styles(string $styles): self
{
Expand All @@ -240,9 +258,6 @@ public function styles(string $styles): self

/**
* Adds JavaScript code to the end of the <body> section of the HTML content.
*
* This method searches for the closing </body> tag in the HTML content and injects
* the provided JavaScript code just before it.
*/
public function scripts(string $scripts): self
{
Expand Down

0 comments on commit 2680ee6

Please sign in to comment.