Skip to content

Commit

Permalink
Fix error on WebView::renderAjax with path relative to basePath e.g. …
Browse files Browse the repository at this point in the history
…"//layouts/main" (#139)
  • Loading branch information
vjik committed May 4, 2021
1 parent 811ea86 commit c636a5c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
9 changes: 5 additions & 4 deletions src/Event/ViewEvent.php
Expand Up @@ -10,21 +10,22 @@
abstract class ViewEvent
{
/**
* @var string the view file being rendered.
* @var string|null The view file being rendered. `null` if no view file is being rendered.
*/
private string $file;
private ?string $file;

/**
* @var array the parameter array passed to the {@see View::render()} method.
*/
private array $parameters;

public function __construct(string $file, array $parameters = [])
public function __construct(?string $file, array $parameters = [])
{
$this->file = $file;
$this->parameters = $parameters;
}

public function file(): string
public function file(): ?string
{
return $this->file;
}
Expand Down
14 changes: 7 additions & 7 deletions src/View.php
Expand Up @@ -299,7 +299,7 @@ protected function findTemplateFile(string $view): string
if (strncmp($view, '//', 2) === 0) {
// path relative to basePath e.g. "//layouts/main"
$file = $this->basePath . '/' . ltrim($view, '/');
} elseif (($currentViewFile = $this->getRequestedViewFile()) !== false) {
} elseif (($currentViewFile = $this->getRequestedViewFile()) !== null) {
// path relative to currently rendered view
$file = dirname($currentViewFile) . '/' . $view;
} elseif ($this->context instanceof ViewContextInterface) {
Expand Down Expand Up @@ -419,19 +419,19 @@ public function localize(string $file, ?string $language = null, ?string $source
}

/**
* @return bool|string the view file currently being rendered. False if no view file is being rendered.
* @return string|null The view file currently being rendered. `null` if no view file is being rendered.
*/
public function getViewFile()
public function getViewFile(): ?string
{
return empty($this->viewFiles) ? false : end($this->viewFiles)['resolved'];
return empty($this->viewFiles) ? null : end($this->viewFiles)['resolved'];
}

/**
* @return bool|string the requested view currently being rendered. False if no view file is being rendered.
* @return string|null The requested view currently being rendered. `null` if no view file is being rendered.
*/
protected function getRequestedViewFile()
protected function getRequestedViewFile(): ?string
{
return empty($this->viewFiles) ? false : end($this->viewFiles)['requested'];
return empty($this->viewFiles) ? null : end($this->viewFiles)['requested'];
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/WebViewTest.php
Expand Up @@ -98,4 +98,13 @@ public function testRegisterCss(): void
$html = $this->webView->renderFile($this->layoutPath, ['content' => '']);
$this->assertStringContainsString('<style id="mainCss">.red{color:red;}</style></head>', $html);
}

public function testRenderAjaxWithoutContext(): void
{
$content = 'Hello!';

$html = $this->webView->renderAjax('//only-content.php', ['content' => $content]);

$this->assertSame($content, $html);
}
}
7 changes: 7 additions & 0 deletions tests/public/view/only-content.php
@@ -0,0 +1,7 @@
<?php
/**
* @var $this \Yiisoft\View\WebView
* @var $content string;
*/

echo $content;

0 comments on commit c636a5c

Please sign in to comment.