Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/Modello.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,17 @@ public function __construct(string $viewPath = '', string $cachePath = '')

/**
* Compile and render the given view file! $view accepts dot notation, and looks in the $views path. Returns false on failure.
* Cache as specified filename if define so can use same template for different page
*/
public function view(string $view, array $data = []): string|false
public function view(string $view, array $data = [], string $cview = ''): string|false
{
// If no cache directory exists, create it
$this->makeCacheDirectory();

// Get the path to the view template, then if we can read it compile it and render it
$viewPath = $this->makeViewPath($view);
if ($template = $this->read($viewPath)) {
$compiled = $this->compile($view, $template);
$compiled = $this->compile($view, $template, $cview);

// Render the view in an output buffer sandbox and return the results.
ob_start();
Expand Down Expand Up @@ -93,11 +94,11 @@ public function setExtension(string $extension): string
}

// Compile the given view. $view takes the path to the template file, $template takes the content.
private function compile(string $view, string $template): string
private function compile(string $view, string $template, string $cview = ''): string
{
// Get the paths to both the view template and the cached file
$viewPath = $this->makeViewPath($view);
$cached = $this->makeCachePath($view);
$cached = $this->makeCachePath($view, $cview);

// If there's a cached view and we don't need to recompile it, we'll just return the
// path to the cached view
Expand Down Expand Up @@ -132,11 +133,16 @@ private function makeViewPath(string $view): string
return $this->views . str_replace('.', '/', $view) . $this->extension;
}

// Turn a given $view path into a real path for the compiled/cached view file.
private function makeCachePath(string $view): string
// Turn a given $view path into a real path for the compiled/cached view file. Additional specific cache name
private function makeCachePath(string $view, string $cview = ''): string
{
// 'foo.bar' => 'cache/views/foo-bar.php'
return $this->cache . str_replace('.', '-', $view) . '.php';
if(!empty($cview)){
// 'foo.bar' => 'cache/views/foo-bar.php'
return $this->cache . str_replace('.', '-', $view) . '_' . str_replace(' ', '-', strtolower($cview)) . '.php';
} else {
// 'foo.bar' => 'cache/views/foo-bar.php'
return $this->cache . str_replace('.', '-', $view) . '.php';
}
}

// Create the $this->cache directory with 0744 perms, if it doesn't exist.
Expand Down