lollipop

A PHP-framework
Log | Files | Refs

commit 643460a81e08e51c1932464041eaefeca3ad3ccb
parent 3b41601274ca3a715ca8036e7e7787bbf72458cb
Author: Friedel Schön <[email protected]>
Date:   Mon, 12 Jun 2023 13:42:48 +0200

new templater

Diffstat:
MLollipop/Router.php | 15+++++++++++----
MLollipop/Template.php | 48++++++++++++++++++++++++++++++++++--------------
Mindex.php | 35+++++++++++++++++++++++++++--------
Mtemplate.php | 2+-
Mtemplate_test.html | 9---------
5 files changed, 73 insertions(+), 36 deletions(-)

diff --git a/Lollipop/Router.php b/Lollipop/Router.php @@ -5,11 +5,16 @@ namespace Lollipop { { protected array $routes = []; protected string $path; + protected final Template $temp; + + public function __contruct($temp) { + $this->temp = $temp; + } protected function match(string $match, array &$route_vars): bool { - $route_split = explode('/', $this->path); - $match_split = explode('/', $match); + $route_split = explode('/', trim($this->path, '/ ')); + $match_split = explode('/', trim($match, '/ '); if (sizeof($route_split) != sizeof($match_split)) { return false; @@ -63,9 +68,11 @@ namespace Lollipop { $vars = []; if ($this->match($route["match"], $vars)) { if (is_callable($route["func"])) { - return $route["func"]($vars); + $fil = $route["func"]($vars); + if (!is_null($fil)) + echo $this->temp->template($fil, $vars); } else { - return $this->includeRoute($route["func"], $vars); + echo $this->temp->template($route["func"], $vars); } } } diff --git a/Lollipop/Template.php b/Lollipop/Template.php @@ -38,6 +38,8 @@ use ErrorException; if ($c == '"' && !$in_string) { // string start $in_string = true; } else if ($c == '"') { // string end + $tokens[] = $buffer; + $buffer = ''; $in_string = false; } else if ($c == ' ' && !$in_string) { if ($buffer) { @@ -60,7 +62,7 @@ use ErrorException; $right = array_pop($tokens); $left = array_pop($tokens); - if ($left == null || $right == null) + if (is_null($left) || is_null($right)) throw new ErrorException("Stack is empty"); return $left + $right; @@ -69,7 +71,7 @@ use ErrorException; $right = array_pop($tokens); $left = array_pop($tokens); - if ($left == null || $right == null) + if (is_null($left) || is_null($right)) throw new ErrorException("Stack is empty"); return intval($left) - intval($right); @@ -78,7 +80,7 @@ use ErrorException; $right = array_pop($tokens); $left = array_pop($tokens); - if ($left == null || $right == null) + if (is_null($left) || is_null($right)) throw new ErrorException("Stack is empty"); return intval($left) * intval($right); @@ -87,7 +89,7 @@ use ErrorException; $right = array_pop($tokens); $left = array_pop($tokens); - if ($left == null || $right == null) + if (is_null($left) || is_null($right)) throw new ErrorException("Stack is empty"); return intval($left) / intval($right); @@ -96,7 +98,7 @@ use ErrorException; $right = array_pop($tokens); $left = array_pop($tokens); - if ($left == null || $right == null) + if (is_null($left) || is_null($right)) throw new ErrorException("Stack is empty"); return $left . $right; @@ -105,7 +107,7 @@ use ErrorException; "to_int" => function(array &$tokens) { $val = array_pop($val); - if ($val == null) + if (is_null($val)) throw new ErrorException("Stack is empty"); return inval($val); @@ -114,7 +116,7 @@ use ErrorException; "include" => function (array &$tokens) { $name = array_pop($tokens); - if (!$name) + if ($name == null) throw new ErrorException("Stack is empty"); include($name); @@ -122,25 +124,43 @@ use ErrorException; "eval" => function (array &$tokens) { $expr = array_pop($tokens); - if (!$expr) + if (is_null($expr)) throw new ErrorException("Stack is empty"); return eval("return ($expr);"); - } + }, + "format_if" => function (array &$stack) { + $format_false = array_pop($stack); + $format_true = array_pop($stack); + $expr = array_pop($stack); + + if (is_null($expr) || is_null($format_true) || is_null($format_false)) + throw new ErrorException("Stack is empty"); + + if ($expr == "") + return $format_false; + else + return str_replace("%%", $expr, $format_true); + }, ]; $stack = []; foreach ($tokens as $token) { - if ($token[0] != '!') { - $stack[] = array_key_exists($token, $data) ? $data[$token] : $token; + if ($token && $token[0] == '!') { + $val = $funcs[substr($token, 1)]($stack); + if (!is_null($val)) + $stack[] = $val; + } else if ($token && $token[0] == '$') { + $stack[] = array_key_exists(substr($token, 1), $data) ? $data[substr($token, 1)] : ""; } else { - $stack[] = $funcs[substr($token, 1)]($stack); + $stack[] = $token; } } - if (sizeof($stack) != 1) + if (sizeof($stack) > 1) throw new ErrorException("Stack-size is not 1"); - + if (sizeof($stack) == 0) + return ""; return $stack[0]; } } diff --git a/index.php b/index.php @@ -2,9 +2,11 @@ require_once "utils/autoloader.php"; -$router = new Lollipop\Router(); +$templater = new Lollipop\Template(); +$router = new Lollipop\Router($templater); -$router->addRoute(["GET", "POST"], "/user/:email/update", "views/alter_user.php"); +$router->addRoute(["GET"], "/user/:email/update", "views/alter_user.php"); +$router->addRoute(["POST"], "/user/:email/update", user_add); $router->addRoute(["GET", "POST"], "/user/:email/crud", "views/crud_user.php"); $router->addRoute(["GET", "POST"], "/user/search", "views/search_user.php"); $router->addRoute(["GET", "POST"], "/dashboard", "views/dashboard.php"); @@ -13,16 +15,33 @@ $router->addRoute(["GET", "POST"], "/logout", "logic/logout.php"); $router->addRoute(["GET", "POST"], "/course/search", "views/search_course.php"); $router->addRoute(["GET", "POST"], "/course/:enroll/enroll", "views/search_course.php"); $router->addRoute(["GET", "POST"], "/course/:unsubscribe/unsubscribe", "views/search_course.php"); -$router->addRoute(["GET"], "/test/template/:hello", function($vars) { - $t = new Lollipop\Template(); - echo $t->template("template_test.html", $vars); +$router->addRoute(["GET"], "/api/:token/weatherdata.json", get_datadata_json); +$router->addRoute(["GET"], "/api/:token/weatherdata.xml", get_datadata_xml); + +$router->addRoute(["GET"], "/test/template/:hello", function(&$vars){ + $vars["xxx"] = "email"; + return "views/template_test.html"; }); + $router->addRoute(["GET"], "/user/add", function($vars) { include "logic/user/add_get.php"; $vars = database_permissions(); - $t = new Lollipop\Template(); + $t = new Lollipop\Template(); echo $t->template("views/add_user.html", $vars); }); $router->addRoute(["POST"], "/user/add", "logic/add_user_post.php"); -$router->route(); -\ No newline at end of file +$router->route(); + + + + +$router->addRoute(["GET"], "/login", function() { + if (is_login()) { + return "/views/"; + } else { + return "/views/" + } +}); + +$router->addRoute(["POST"], "/contol/login", login_post); +\ No newline at end of file diff --git a/template.php b/template.php @@ -3,4 +3,4 @@ include_once "Lollipop/Template.php"; $t = new Lollipop\Template; -echo $t->template("template_test.html", [ "hello" => "world" ]); +echo $t->template("template_test.html", []); diff --git a/template_test.html b/template_test.html @@ -1,9 +0,0 @@ -<<<<<<< HEAD -{{ name "value" !set }} - -{{ "test_include.php" !include }} - -{{ "hello_foo()" !eval "- Mayor Monogram" !cat }} -======= -data: {{ 3 3 !add }} ->>>>>>> origin/views