xref: /webtrees/app/View.php (revision 1f3fb95c905e623518962b025974a0e6c19d068b)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2017 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16namespace Fisharebest\Webtrees;
17
18/**
19 * Simple view/template class.
20 */
21class View {
22	/**
23	 * @var string The (file) name of the view.
24	 */
25	private $name;
26
27	/**
28	 * @var mixed[] Data to be inserted into the view.
29	 */
30	private $data;
31
32	/**
33	 * Createa view from a template name and optional data.
34	 *
35	 * @param       $name
36	 * @param array $data
37	 */
38	public function __construct($name, $data = []) {
39		$this->name = $name;
40		$this->data = $data;
41	}
42
43	/**
44	 * Render a view.
45	 *
46	 * @return string
47	 */
48	public function render() {
49		extract($this->data);
50
51		ob_start();
52		require $this->getFilenameForView($this->name);
53
54		return ob_get_clean();
55	}
56
57	/**
58	 * Allow a theme to override the default views.
59	 *
60	 * @param string $view_name
61	 *
62	 * @return string
63	 */
64	public static function getFilenameForView($view_name) {
65		$view_file  = $view_name . '.php';
66		//$theme_view = WT_THEMES_DIR . Theme::theme()->themeId() . '/resources/views/' . $view_file;
67
68		//if (file_exists($theme_view)) {
69		//	return $theme_view;
70		//} else {
71			return WT_ROOT . 'resources/views/' . $view_file;
72		//}
73	}
74
75	/**
76	 * Cerate and render a view in a single operation.
77	 *
78	 * @param string  $name
79	 * @param mixed[] $data
80	 *
81	 * @return string
82	 */
83	public static function make($name, $data = []) {
84		$view = new static($name, $data);
85
86		return $view->render();
87	}
88}
89