Author: Pedro Lucas Porcellis <porcellis@eletrotupi.com>
all: add a very basic template mechanism
app/controllers/HelloController.php | 10 +++++++++- app/helpers/Template.php | 21 +++++++++++++++++++++ app/views/home.php | 13 +++++++++++++
diff --git a/app/controllers/HelloController.php b/app/controllers/HelloController.php index d71cfc4b87b4fcedc6a532aecea513ca58f8abc4..f11345cc1a9020d951b9638d4ef3b5bd2b58158f 100644 --- a/app/controllers/HelloController.php +++ b/app/controllers/HelloController.php @@ -1,7 +1,15 @@ <?php +require_once __DIR__ . '/../helpers/Template.php'; + class HelloController { public function index() { - return 'Hello, World!'; + $data = [ + 'title' => 'Dinheiro - Pagina Inicial', + 'appName' => 'Dinheiro', + 'message' => 'Essa é uma menasgem teste' + ]; + + echo Template::render('home', $data); } } diff --git a/app/helpers/Template.php b/app/helpers/Template.php new file mode 100644 index 0000000000000000000000000000000000000000..4f14a1a7965620a629d4a13dcea2bc3851bc6a9f --- /dev/null +++ b/app/helpers/Template.php @@ -0,0 +1,21 @@ +<?php + +class Template { + public static function render($view, $data = []) { + // TODO: Add support for multiple controller/pages? + $viewPath = __DIR__ . '/../views/' . $view . '.php'; + + if (!file_exists($viewPath)) { + throw new Exception("View not found: " . $view); + } + + extract($data); + + ob_start(); + include $viewPath; + $content = ob_get_clean(); + + return $content; + } +} + diff --git a/app/views/home.php b/app/views/home.php new file mode 100644 index 0000000000000000000000000000000000000000..2883997d456cbe5b78db80476bf868bfcc3d74c7 --- /dev/null +++ b/app/views/home.php @@ -0,0 +1,13 @@ +<!DOCTYPE html> +<html lang="en"> + <head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title><?= $title ?? 'Dinheiro' ?></title> + </head> + + <body> + <h1>Bem vindo! <?= htmlspecialchars($appName) ?>!</h1> + <p><?= htmlspecialchars($message) ?></p> + </body> +</html>