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