From 83e6b3577ac5a833e8c4e634eb5cff9cf3fab2a4 Mon Sep 17 00:00:00 2001 From: sheychen Date: Sat, 10 Jun 2017 17:23:03 +0200 Subject: [PATCH] WIP --- LICENSE | 21 ++++++ composer.json | 18 +++++ src/Html.php | 0 src/Pdf.php | 0 src/Php.php | 0 src/Template.php | 192 +++++++++++++++++++++++++++++++++++++++++++++++ src/Text.php | 0 7 files changed, 231 insertions(+) create mode 100644 LICENSE create mode 100644 composer.json create mode 100644 src/Html.php create mode 100644 src/Pdf.php create mode 100644 src/Php.php create mode 100644 src/Template.php create mode 100644 src/Text.php diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..59d50c5 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 sheychen + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..4bc45e3 --- /dev/null +++ b/composer.json @@ -0,0 +1,18 @@ +{ + "name": "krutush/template", + "license": "MIT", + "authors": [ + { + "name": "sheychen", + "email": "contact@clementbois.fr" + } + ], + "require": { + "krutush/krutush": "dev-master" + }, + "autoload": { + "psr-4": { + "Krutush\\Template": "src/" + } + } +} \ No newline at end of file diff --git a/src/Html.php b/src/Html.php new file mode 100644 index 0000000..e69de29 diff --git a/src/Pdf.php b/src/Pdf.php new file mode 100644 index 0000000..e69de29 diff --git a/src/Php.php b/src/Php.php new file mode 100644 index 0000000..e69de29 diff --git a/src/Template.php b/src/Template.php new file mode 100644 index 0000000..d1061c5 --- /dev/null +++ b/src/Template.php @@ -0,0 +1,192 @@ +path = $this->path($path, $extention, $folder); + } + + public function set($key, $value){ + $this->data[$key] = $value; + return $this; + } + + public function sets(array $array){ + foreach($array as $key => $value){ + $this->set($key, $value); + } + return $this; + } + + public function content($key, $value){ + $this->content[$key] = $value; + return $this; + } + + public function contents(array $array){ + foreach($array as $key => $value){ + $this->content($key, $value); + } + return $this; + } + + public function run($output = 'direct'){ + switch($output){ + case 'array': + case 'direct': + break; + + case 'buffer': + ob_start(); + break; + + default: + trigger_error('Unknow output type '.$output); + break; + } + $callable = function($t, $path){ + include($path); + }; + $callable($this, $this->path); + if(isset($this->layout)){ + $layout = new Template($this->layout, false, false); + $layout->sets($this->data) + ->contents($this->sections) + ->run(); + } + switch($output){ + case 'direct': + break; + + case 'buffer': + return ob_get_clean(); + + case 'array': + return array( + 'sets' => $this->data, + 'contents' => $this->sections + ); + + default: + break; + } + } + + public function _load($path, $extention = true, $folder = true){ + $load = new Template($path, $extention, $folder); + $load->sets($this->data) + ->contents($this->sections) + ->run(); + $this->sets($load->data) + ->contents($load->sections); + return $this; + } + + public function _layout($path, $extention = true, $folder = true){ + $this->layout = $this->path($path, $extention, $folder); + return $this; + } + + public function path($path, $extention = true, $folder = true){ + switch($extention){ + case true: + $path .= '.phtml'; + break; + + case false: + break; + + default: + $path .= $extention; + break; + } + if($folder == true) + $path = Path::get('template').'/'.$path; + + return $path; + } + + public function _content($key){ + if(isset($this->content[$key])) + return $this->content[$key]; + + return ''; + } + + public function _section($key){ + if(isset($this->section)){ + trigger_error('Section precedente non cloturée : '.$this->section, E_USER_WARM); + return; + } + $this->section = $key; + ob_start(); + } + + public function _endsection($override = true){ + if(!isset($this->section)){ + trigger_error('Aucune section en cours', E_USER_WARM); + return; + } + $this->sections[$this->section] = ($override == false ? $this->sections[$this->section] : '').ob_get_clean(); + $this->section = null; + return $this; + } + + public function _escape($data){ + return htmlspecialchars($data, ENT_QUOTES, 'UTF-8'); + } + public function _e($data){ return $this->_escape($data); } + + public function _exists($keys, $all = true, $exception = false){ + if(is_array($keys)){ + foreach($keys as $key){ + if(isset($this->data[$key])){ + if(!$all) + return true; + }else{ + if($all) + return false; + } + } + return $all; + }else{ + if(is_string($keys)){ + return isset($this->data[$keys]); + }else{ + if($exception) + throw new \Exception('key must be a string'); + return false; + } + } + } + + public function _x($keys, $all = true){ return $this->_exists($keys, $all); } + + public function _get($key, $escape = true){ + if(!$this->_exists($key)) + return null; + + $value = $this->data[$key]; + if($escape && is_string($value)) + $value = $this->_escape($value); + + return $value; + } + public function _($key, $escape = true){ return $this->_get($key, $escape); } + + public function _print($key, $format, $escape = true){ + if(!$this->_exists($key)) + return ''; + + return str_replace('{?}', $this->_get($key, $escape), $format); + } + public function _p($key, $format, $escape = true){ return $this->_print($key, $format, $escape); } +} \ No newline at end of file diff --git a/src/Text.php b/src/Text.php new file mode 100644 index 0000000..e69de29