WIP
commit
83e6b3577a
|
@ -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.
|
|
@ -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/"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,192 @@
|
|||
<?php
|
||||
|
||||
namespace Krutush\Template;
|
||||
|
||||
class Template{
|
||||
private $path;
|
||||
private $layout;
|
||||
private $data = array();
|
||||
private $content = array();
|
||||
private $section;
|
||||
private $sections = array();
|
||||
|
||||
public function __construct($path, $extention = true, $folder = true){
|
||||
$this->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); }
|
||||
}
|
Loading…
Reference in New Issue