Skip to content

Commit

Permalink
Merge pull request #25 from wireframe-framework/feature-render-view
Browse files Browse the repository at this point in the history
0.25.0
  • Loading branch information
teppokoivula authored Jun 30, 2022
2 parents cb07426 + 4746bdf commit c72fea8
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 5 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.25.0] - 2022-06-29

### Added
- New method View::renderView() for rendering View with specific view file, without layout.
- New method Page::renderView() for rendering Page with specific view file, without layout.

### Fixed
- Malformed sprintf statement for exceptions thrown by View::setViewsPath().

## [0.24.2] - 2022-04-11

### Fixed
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.24.2",
"version": "0.25.0",
"author": "Teppo Koivula",
"href": "https://wireframe-framework.com",
"requires": [
Expand Down
2 changes: 1 addition & 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.24.2
* @version 0.25.0
* @author Teppo Koivula <[email protected]>
* @license Mozilla Public License v2.0 https://mozilla.org/MPL/2.0/
*/
Expand Down
22 changes: 21 additions & 1 deletion lib/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* @internal This class is only intended for use within the Wireframe internals.
*
* @version 0.2.0
* @version 0.3.0
* @author Teppo Koivula <[email protected]>
* @license Mozilla Public License v2.0 https://mozilla.org/MPL/2.0/
*/
Expand Down Expand Up @@ -50,6 +50,9 @@ public function init() {
$this->addHookMethod('Page::getView', $this, 'pageView');
$this->addHookMethod('Page::setView', $this, 'pageView');

// helper method for rendering page with specified view
$this->addHookMethod('Page::renderView', $this, 'pageRenderView');

// helper methods for getting or setting page view template
$this->addHookMethod('Page::viewTemplate', $this, 'pageViewTemplate');
$this->addHookMethod('Page::getViewTemplate', $this, 'pageViewTemplate');
Expand Down Expand Up @@ -161,6 +164,23 @@ protected function pageView(HookEvent $event) {
}
}

/**
* Render page with specified view
*
* @param HookEvent $event
*/
protected function pageRenderView(HookEvent $event) {
$original_layout = $event->object->_wireframe_layout;
$original_view = $event->object->_wireframe_view;
$original_view_template = $event->object->_wireframe_view_template;
$event->object->_wireframe_layout = '';
$event->object->setView($event->arguments[0]);
$event->return = $event->object->render();
$event->object->_wireframe_layout = $original_layout;
$event->object->_wireframe_view = $original_view;
$event->object->_wireframe_view_template = $original_view_template;
}

/**
* This method is used by Page::viewTemplate(), Page::getViewTemplate(), and Page::setViewTemplate()
*
Expand Down
27 changes: 25 additions & 2 deletions lib/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @property ViewPlaceholders|null $placeholders ViewPlaceholders object.
* @property Partials|null $partials Object containing partial paths.
*
* @version 0.8.2
* @version 0.9.0
* @author Teppo Koivula <[email protected]>
* @license Mozilla Public License v2.0 https://mozilla.org/MPL/2.0/
*/
Expand Down Expand Up @@ -88,6 +88,29 @@ public function ___render() {
return $out;
}

/**
* Render specified view file
*
* @param string $view View file name
* @return string Rendered markup for the View.
*
* @throws Exception if provided view file cannot be located.
*/
public function renderView(string $view): string {
$original_layout = $this->getLayout();
$original_view = $this->getView();
$this->setLayout(null)->setView($view);
if ($this->getView() !== $view) {
throw new \Exception(sprintf(
'View not found: "%s"',
$view
));
}
$out = $this->render();
$this->setLayout($original_layout)->setView($original_view);
return $out;
}

/**
* Magic __set() method
*
Expand Down Expand Up @@ -354,7 +377,7 @@ public function getLayoutsPath(): ?string {
public function setViewsPath(string $views_path): View {
if (!\is_dir($views_path)) {
throw new \Exception(sprintf(
'Missing or unreadable path to the views directory: "%"',
'Missing or unreadable path to the views directory: "%s"',
$views_path
));
}
Expand Down

0 comments on commit c72fea8

Please sign in to comment.