Skip to content

Commit f554806

Browse files
authored
Add BaseView::getTheme(), add title cleanup, fix typo minor to docBlocks (#170)
1 parent 64bfdfd commit f554806

4 files changed

Lines changed: 52 additions & 40 deletions

File tree

src/BaseView.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,16 @@ public function getDefaultExtension(): string
182182
return $this->defaultExtension;
183183
}
184184

185+
/**
186+
* Gets the theme instance, or null if no theme has been set.
187+
*
188+
* @return Theme The theme instance, or null if no theme has been set.
189+
*/
190+
public function getTheme(): ?Theme
191+
{
192+
return $this->theme;
193+
}
194+
185195
/**
186196
* Sets a common parameters that is accessible in all view templates.
187197
*
@@ -331,8 +341,8 @@ final public function setPlaceholderSalt(string $salt): void
331341
*
332342
* The view to be rendered can be specified in one of the following formats:
333343
*
334-
* - the name of the view starting with a slash. to join the base path {@see getBasePath()} (e.g. "/site/index").
335-
* - the name of the view without the starting slash (e.g. "site/index"). The corresponding view file will be
344+
* - The name of the view starting with a slash to join the base path {@see getBasePath()} (e.g. "/site/index").
345+
* - The name of the view without the starting slash (e.g. "site/index"). The corresponding view file will be
336346
* looked for under the {@see ViewContextInterface::getViewPath()} of the context set via {@see withContext()}.
337347
* If the context instance was not set {@see withContext()}, it will be looked for under the directory containing
338348
* the view currently being rendered (i.e., this happens when rendering a view within another view).
@@ -362,10 +372,10 @@ public function render(string $view, array $parameters = []): string
362372
* If the theme was not set {@see withTheme()}, it will try to render the themed version of the view file
363373
* as long as it is available.
364374
*
365-
* If the theme was not set {@see withRenderers()}, the method will use it to render the view file. Otherwise,
375+
* If the renderer was set {@see withRenderers()}, the method will use it to render the view file. Otherwise,
366376
* it will simply include the view file as a normal PHP file, capture its output and return it as a string.
367377
*
368-
* @param string $viewFile The view file. This can be either an absolute file path or an alias of it.
378+
* @param string $viewFile The full absolute path of the view file.
369379
* @param array $parameters The parameters (name-value pairs) that will be extracted and made available in the view
370380
* file.
371381
*
@@ -524,7 +534,7 @@ public function clear(): void
524534
protected function findTemplateFile(string $view): string
525535
{
526536
if ($view !== '' && $view[0] === '/') {
527-
// path relative to basePath e.g. "//layouts/main"
537+
// path relative to basePath e.g. "/layouts/main"
528538
$file = $this->basePath . '/' . ltrim($view, '/');
529539
} elseif (($currentViewFile = $this->getRequestedViewFile()) !== null) {
530540
// path relative to currently rendered view

src/Theme.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@
1818
/**
1919
* Theme represents an application theme.
2020
*
21-
* When {@see View} renders a view file, it will check the {@see View::theme} to see if there is a themed
21+
* When {@see View} renders a view file, it will check the {@see View::$theme} to see if there is a themed
2222
* version of the view file exists. If so, the themed version will be rendered instead.
2323
*
2424
* A theme is a directory consisting of view files which are meant to replace their non-themed counterparts.
2525
*
26-
* Theme uses {@see pathMap} to achieve the view file replacement:
26+
* Theme uses {@see Theme::$pathMap} to achieve the view file replacement:
2727
*
28-
* 1. It first looks for a key in {@see pathMap} that is a substring of the given view file path;
28+
* 1. It first looks for a key in {@see Theme::$pathMap} that is a substring of the given view file path;
2929
* 2. If such a key exists, the corresponding value will be used to replace the corresponding part
3030
* in the view file path;
3131
* 3. It will then check if the updated view file exists or not. If so, that file will be used
3232
* to replace the original view file.
3333
* 4. If Step 2 or 3 fails, the original view file will be used.
3434
*
35-
* For example, if {@see pathMap} is `['/app/views' => '/app/themes/basic']`, then the themed version for
35+
* For example, if {@see Theme::$pathMap} is `['/app/views' => '/app/themes/basic']`, then the themed version for
3636
* a view file `/app/views/site/index.php` will be `/app/themes/basic/site/index.php`.
3737
*
3838
* It is possible to map a single path to multiple paths. For example:

src/WebView.php

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,11 @@ public function renderAjaxString(string $string): string
272272
}
273273

274274
/**
275-
* Clears up the registered meta tags, link tags, css/js scripts and files.
275+
* Clears up the registered meta tags, link tags, css/js scripts, files and title.
276276
*/
277277
public function clear(): void
278278
{
279+
$this->title = '';
279280
$this->metaTags = [];
280281
$this->linkTags = [];
281282
$this->css = [];
@@ -643,28 +644,6 @@ protected function renderBodyEndHtml(bool $ajaxMode): string
643644
return empty($lines) ? '' : implode("\n", $lines);
644645
}
645646

646-
/**
647-
* Get title in views.
648-
*
649-
* in Layout:
650-
*
651-
* ```php
652-
* <title><?= Html::encode($this->getTitle()) ?></title>
653-
* ```
654-
*
655-
* in Views:
656-
*
657-
* ```php
658-
* $this->setTitle('Web Application - Yii 3.0.');
659-
* ```
660-
*
661-
* @return string
662-
*/
663-
public function getTitle(): string
664-
{
665-
return $this->title;
666-
}
667-
668647
/**
669648
* It processes the CSS configuration generated by the asset manager and converts it into HTML code.
670649
*
@@ -750,6 +729,33 @@ public function addJsVars(array $jsVars): void
750729
}
751730
}
752731

732+
protected function createBeforeRenderEvent(string $viewFile, array $parameters): StoppableEventInterface
733+
{
734+
return new BeforeRender($this, $viewFile, $parameters);
735+
}
736+
737+
/**
738+
* Get title in views.
739+
*
740+
* in Layout:
741+
*
742+
* ```php
743+
* <title><?= Html::encode($this->getTitle()) ?></title>
744+
* ```
745+
*
746+
* in Views:
747+
*
748+
* ```php
749+
* $this->setTitle('Web Application - Yii 3.0.');
750+
* ```
751+
*
752+
* @return string
753+
*/
754+
public function getTitle(): string
755+
{
756+
return $this->title;
757+
}
758+
753759
/**
754760
* Set title in views.
755761
*
@@ -762,11 +768,6 @@ public function setTitle(string $value): void
762768
$this->title = $value;
763769
}
764770

765-
protected function createBeforeRenderEvent(string $viewFile, array $parameters): StoppableEventInterface
766-
{
767-
return new BeforeRender($this, $viewFile, $parameters);
768-
}
769-
770771
protected function createAfterRenderEvent(
771772
string $viewFile,
772773
array $parameters,

tests/ViewTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,13 @@ public function testRelativePathInView(): void
122122
$subViewContent = 'subviewcontent';
123123
file_put_contents($subView, $subViewContent);
124124

125-
$view = $this->createViewWithBasePath($this->tempDirectory)
126-
->withTheme(new Theme([$this->tempDirectory => $themePath]))
127-
;
125+
$theme = new Theme([$this->tempDirectory => $themePath]);
126+
$view = $this->createViewWithBasePath($this->tempDirectory)->withTheme($theme);
128127

129128
$this->assertSame($this->tempDirectory, $view->getBasePath());
130129
$this->assertSame('php', $view->getDefaultExtension());
131130
$this->assertSame(null, $view->getViewFile());
131+
$this->assertSame($theme, $view->getTheme());
132132
$this->assertSame($subViewContent, $view->render('/base'));
133133
$this->assertSame($subViewContent, $view->render('//base'));
134134
}
@@ -156,6 +156,7 @@ public function testRelativePathInViewWithContext(): void
156156
$view = $this->createViewWithBasePath($this->tempDirectory)
157157
->withContext($this->createContext($this->tempDirectory));
158158

159+
$this->assertSame(null, $view->getTheme());
159160
$this->assertSame($subViewContent, $view->render('test/base'));
160161
}
161162

0 commit comments

Comments
 (0)