Skip to content

Commit

Permalink
Add PartialView class, fix issue with stashing/restoring context in W…
Browse files Browse the repository at this point in the history
…ireframe::render()
  • Loading branch information
teppokoivula committed Jan 10, 2024
1 parent 88736da commit 77b91e1
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 6 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.29.0] - 2024-01-10

### Added
- New PartialView class that extends TemplateFile core class.

### Fixed
- Fixed an issue where rendering page within page did not properly restore the $view API variable to the state it was in before rendering said page.

## [0.28.0] - 2024-01-10

### Added
Expand Down
2 changes: 1 addition & 1 deletion Wireframe.info.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Wireframe",
"summary": "Wireframe is an output framework for ProcessWire CMS/CMF.",
"version": "0.28.0",
"version": "0.29.0",
"author": "Teppo Koivula",
"href": "https://wireframe-framework.com",
"requires": [
Expand Down
3 changes: 2 additions & 1 deletion Wireframe.module.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/
*/
Expand Down Expand Up @@ -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) {
Expand Down
17 changes: 13 additions & 4 deletions lib/Partial.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ class Partial extends \ProcessWire\Wire {
*/
protected $filenames = [];

/**
* Partial View
*
* @var PartialView
*/
protected $partial_view;

/**
* Constructor
*
Expand Down Expand Up @@ -72,10 +79,12 @@ public function ___render(array $args = []): string {
if (empty($filename)) {
return '';
}
$template = $this->wire(new \ProcessWire\TemplateFile());
$template->setFilename($filename);
$template->data($args);
return $template->render() ?: '';
if (!$this->partial_view) {
$this->partial_view = $this->wire(new PartialView());
}
$this->partial_view->setFilename($filename);
$this->partial_view->data($args);
return $this->partial_view->render() ?: '';
}

/**
Expand Down
63 changes: 63 additions & 0 deletions lib/PartialView.php
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;
}

}

0 comments on commit 77b91e1

Please sign in to comment.