diff options
| author | Greg Roach <fisharebest@gmail.com> | 2017-08-02 12:03:07 +0100 |
|---|---|---|
| committer | Greg Roach <fisharebest@gmail.com> | 2017-08-02 12:03:07 +0100 |
| commit | f3281ad6d4b0b46616c7f860894486fd3a9aaabc (patch) | |
| tree | b0dfe247f44aa1275ee2fa40d6d430fc664cc0c8 /app/View.php | |
| parent | 9a83633de0dbc581ccf24b2a59f88cb5595133a0 (diff) | |
| download | webtrees-f3281ad6d4b0b46616c7f860894486fd3a9aaabc.tar.gz webtrees-f3281ad6d4b0b46616c7f860894486fd3a9aaabc.tar.bz2 webtrees-f3281ad6d4b0b46616c7f860894486fd3a9aaabc.zip | |
Use views/templates for select/options
Diffstat (limited to 'app/View.php')
| -rw-r--r-- | app/View.php | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/app/View.php b/app/View.php new file mode 100644 index 0000000000..77352bb648 --- /dev/null +++ b/app/View.php @@ -0,0 +1,80 @@ +<?php +/** + * webtrees: online genealogy + * Copyright (C) 2017 webtrees development team + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +namespace Fisharebest\Webtrees; + +/** + * Simple view/template class. + */ +class View { + /** + * @var string The (file) name of the view. + */ + private $name; + + /** + * @var mixed[] Data to be inserted into the view. + */ + private $data; + + /** + * Createa view from a template name and optional data. + * + * @param $name + * @param array $data + */ + public function __construct($name, $data = []) { + $this->name = $name; + $this->data = $data; + } + + /** + * Render a view. + * + * @return string + */ + public function render() { + extract($this->data); + + ob_start(); + require WT_ROOT . 'resources/views/' . $this->name . '.php'; + return ob_get_clean(); + } + + /** + * Check whether a view exists. + * + * @param string $view_name + * + * @return bool + */ + public static function exists($view_name) { + return file_exists(WT_ROOT . 'resources/views/' . $view_name . '.php'); + } + + /** + * Cerate and render a view in a single operation. + * + * @param string $name + * @param mixed[] $data + * + * @return string + */ + public static function make($name, $data = []) { + $view = new static($name, $data); + + return $view->render(); + } +} |
