-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PartialView class, fix issue with stashing/restoring context in W…
…ireframe::render()
- Loading branch information
1 parent
88736da
commit 77b91e1
Showing
5 changed files
with
87 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
* @method static string|Page|NullPage page($source, $args = []) Static getter (factory) method for Pages. | ||
* @method static string|null partial(string $partial_name, array $args = []) Static getter (factory) method for Partials. | ||
* | ||
* @version 0.28.0 | ||
* @version 0.29.0 | ||
* @author Teppo Koivula <[email protected]> | ||
* @license Mozilla Public License v2.0 https://mozilla.org/MPL/2.0/ | ||
*/ | ||
|
@@ -853,6 +853,7 @@ public function ___render(array $data = []): ?string { | |
$stashed_context = array_pop($this->stash); | ||
$this->page = $stashed_context['page']; | ||
$this->view = $stashed_context['view']; | ||
$this->wire('view', $this->view); | ||
$this->controller = $stashed_context['controller']; | ||
$this->data = $stashed_context['data']; | ||
if ($this->page && $this->controller) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace Wireframe; | ||
|
||
use ProcessWire\WireException; | ||
|
||
/** | ||
* Wireframe Partial View component | ||
* | ||
* This class is a wrapper for the ProcessWire TemplateFile class with some modifications and the | ||
* addition of the Wireframe namespace. | ||
* | ||
* @version 0.0.1 | ||
* @author Teppo Koivula <[email protected]> | ||
* @license Mozilla Public License v2.0 https://mozilla.org/MPL/2.0/ | ||
*/ | ||
class PartialView extends \ProcessWire\TemplateFile { | ||
|
||
/** | ||
* Render the partial | ||
* | ||
* This method is a copy of the TemplateFile::render() method with unnecessary parts, such as | ||
* prepend/append file handling and various configuration flags, removed. | ||
* | ||
* @return string The output of the partial file | ||
* @throws WireException if file not exist or if any exceptions is thrown by included file(s) | ||
*/ | ||
public function ___render() { | ||
|
||
if (!$this->filename) return ''; | ||
|
||
if (!file_exists($this->filename)) { | ||
$error = "Partial file does not exist: $this->filename"; | ||
throw new WireException($error); | ||
} | ||
|
||
$this->renderReady(); | ||
|
||
// make API variables available to PHP file | ||
$fuel = array_merge($this->getArray(), self::$globals); // so that script can foreach all vars to see what's there | ||
extract($fuel); | ||
ob_start(); | ||
|
||
// include main file to render | ||
try { | ||
$this->fileReady($this->filename); | ||
$this->returnValue = require($this->filename); | ||
$this->fileFinished(); | ||
} catch(\Exception $e) { | ||
if ($this->fileFailed($this->filename, $e)) throw $this->renderFailed($e); | ||
} | ||
|
||
$out = ob_get_contents(); | ||
ob_end_clean(); | ||
|
||
$this->renderFinished(); | ||
|
||
$out = trim($out); | ||
|
||
return $out; | ||
} | ||
|
||
} |