Skip to content

Commit

Permalink
Merge pull request #2 from AuroraLumina/feature/render
Browse files Browse the repository at this point in the history
Template Engine
  • Loading branch information
thalysmarciobn authored Jun 2, 2024
2 parents fa2697f + f9a2ab2 commit c923291
Show file tree
Hide file tree
Showing 4 changed files with 1,193 additions and 27 deletions.
38 changes: 19 additions & 19 deletions src/View/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,41 @@
* Class View
* Represents a view renderer.
*/
class View
class View extends ViewTemplate
{
/**
* @var ViewConfiguration $configuration The configuration for view templates.
*/
private ViewConfiguration $configuration;

/**
* View constructor.
*
* @param ViewConfiguration $configuration The configuration for view templates.
* Constructs a new View object.
*
* Initializes the View with the provided configuration.
* Sets the compile location and template paths based on the configuration.
*
* @param ViewConfiguration $configuration The configuration for the view.
*/
public function __construct(ViewConfiguration $configuration)
{
$this->configuration = $configuration;
$this->setCompileLocation("cache/", false);
$this->setPaths($configuration->getTemplatePaths());
}

/**
* Render a view.
*
* @param string $view The name of the view to render.
* @return string The rendered content of the view.
* @throws Exception If the template file is not found.
*/
protected function renderView(string $view): string
protected function renderView(string $view, array $data): string
{
$viewPath = $this->configuration->getTemplatePath() . $view;
$this->defaultVariables();

$this->load($view);

if (!file_exists($viewPath))
foreach ($data as $key => $value)
{
throw new Exception('Template not found.');
$this->assign($key, $value);
}

$content = file_get_contents($viewPath);
return $content;
return $this->get();
}

/**
Expand All @@ -53,8 +53,8 @@ protected function renderView(string $view): string
* @return string The rendered content of the view.
* @throws Exception If the template file is not found.
*/
public function render(string $view): string
public function view(string $view, array $data = []): string
{
return $this->renderView($view);
return $this->renderView($view, $data);
}
}
Loading

0 comments on commit c923291

Please sign in to comment.