template 엔진 '일단' 추가

This commit is contained in:
2018-01-11 21:40:21 +09:00
parent ee4f524437
commit 3a025cfa70
20 changed files with 1666 additions and 4 deletions
+1 -1
View File
@@ -182,7 +182,7 @@ if($btn == "자동개최설정" && $me[userlevel] >= 5) {
} elseif($btn == "예선전부" && $me[userlevel] >= 5) { qualifyAll($connect, $admin[tnmt_type], $admin[tournament], $admin[phase]);
} elseif($btn == "추첨" && $me[userlevel] >= 5) { selection($connect, $admin[tnmt_type], $admin[tournament], $admin[phase]);
} elseif($btn == "추첨전부" && $me[userlevel] >= 5) { selectionAll($connect, $admin[tnmt_type], $admin[tournament], $admin[phase]);
} elseif($btn == "본선" && $me[userlevel] >= 5) { finally($connect, $admin[tnmt_type], $admin[tournament], $admin[phase]);
} elseif($btn == "본선" && $me[userlevel] >= 5) { finallySingle($connect, $admin[tnmt_type], $admin[tournament], $admin[phase]);
} elseif($btn == "본선전부" && $me[userlevel] >= 5) { finallyAll($connect, $admin[tnmt_type], $admin[tournament], $admin[phase]);
} elseif($btn == "배정" && $me[userlevel] >= 5) { final16set($connect);
} elseif($btn == "베팅마감" && $me[userlevel] >= 5) {
+3 -3
View File
@@ -42,7 +42,7 @@ function processTournament($connect) {
if($phase >= 32) { $tnmt = 4; $phase = 0; }
break;
case 4: //본선중
finally($connect, $type, $tnmt, $phase); $phase++;
finallySingle($connect, $type, $tnmt, $phase); $phase++;
if($phase >= 6) { $tnmt = 5; $phase = 0; }
break;
case 5: //배정중
@@ -454,7 +454,7 @@ function selectionAll($connect, $tnmt_type, $tnmt, $phase) {
}
}
function finally($connect, $tnmt_type, $tnmt, $phase) {
function finallySingle($connect, $tnmt_type, $tnmt, $phase) {
$cand = getTwo($tnmt, $phase);
//각 그룹 페이즈 실행
@@ -484,7 +484,7 @@ function finallyAll($connect, $tnmt_type, $tnmt, $phase) {
$start = $phase;
$end = $phase - ($phase % 2) + 2;
for($i=$start; $i < $end; $i++) {
finally($connect, $tnmt_type, $tnmt, $i);
finallySingle($connect, $tnmt_type, $tnmt, $i);
}
}
+279
View File
@@ -0,0 +1,279 @@
<?php
namespace League\Plates;
use League\Plates\Extension\ExtensionInterface;
use League\Plates\Template\Data;
use League\Plates\Template\Directory;
use League\Plates\Template\FileExtension;
use League\Plates\Template\Folders;
use League\Plates\Template\Func;
use League\Plates\Template\Functions;
use League\Plates\Template\Name;
use League\Plates\Template\Template;
/**
* Template API and environment settings storage.
*/
class Engine
{
/**
* Default template directory.
* @var Directory
*/
protected $directory;
/**
* Template file extension.
* @var FileExtension
*/
protected $fileExtension;
/**
* Collection of template folders.
* @var Folders
*/
protected $folders;
/**
* Collection of template functions.
* @var Functions
*/
protected $functions;
/**
* Collection of preassigned template data.
* @var Data
*/
protected $data;
/**
* Create new Engine instance.
* @param string $directory
* @param string $fileExtension
*/
public function __construct($directory = null, $fileExtension = 'php')
{
$this->directory = new Directory($directory);
$this->fileExtension = new FileExtension($fileExtension);
$this->folders = new Folders();
$this->functions = new Functions();
$this->data = new Data();
}
/**
* Set path to templates directory.
* @param string|null $directory Pass null to disable the default directory.
* @return Engine
*/
public function setDirectory($directory)
{
$this->directory->set($directory);
return $this;
}
/**
* Get path to templates directory.
* @return string
*/
public function getDirectory()
{
return $this->directory->get();
}
/**
* Set the template file extension.
* @param string|null $fileExtension Pass null to manually set it.
* @return Engine
*/
public function setFileExtension($fileExtension)
{
$this->fileExtension->set($fileExtension);
return $this;
}
/**
* Get the template file extension.
* @return string
*/
public function getFileExtension()
{
return $this->fileExtension->get();
}
/**
* Add a new template folder for grouping templates under different namespaces.
* @param string $name
* @param string $directory
* @param boolean $fallback
* @return Engine
*/
public function addFolder($name, $directory, $fallback = false)
{
$this->folders->add($name, $directory, $fallback);
return $this;
}
/**
* Remove a template folder.
* @param string $name
* @return Engine
*/
public function removeFolder($name)
{
$this->folders->remove($name);
return $this;
}
/**
* Get collection of all template folders.
* @return Folders
*/
public function getFolders()
{
return $this->folders;
}
/**
* Add preassigned template data.
* @param array $data;
* @param null|string|array $templates;
* @return Engine
*/
public function addData(array $data, $templates = null)
{
$this->data->add($data, $templates);
return $this;
}
/**
* Get all preassigned template data.
* @param null|string $template;
* @return array
*/
public function getData($template = null)
{
return $this->data->get($template);
}
/**
* Register a new template function.
* @param string $name;
* @param callback $callback;
* @return Engine
*/
public function registerFunction($name, $callback)
{
$this->functions->add($name, $callback);
return $this;
}
/**
* Remove a template function.
* @param string $name;
* @return Engine
*/
public function dropFunction($name)
{
$this->functions->remove($name);
return $this;
}
/**
* Get a template function.
* @param string $name
* @return Func
*/
public function getFunction($name)
{
return $this->functions->get($name);
}
/**
* Check if a template function exists.
* @param string $name
* @return boolean
*/
public function doesFunctionExist($name)
{
return $this->functions->exists($name);
}
/**
* Load an extension.
* @param ExtensionInterface $extension
* @return Engine
*/
public function loadExtension(ExtensionInterface $extension)
{
$extension->register($this);
return $this;
}
/**
* Load multiple extensions.
* @param array $extensions
* @return Engine
*/
public function loadExtensions(array $extensions = array())
{
foreach ($extensions as $extension) {
$this->loadExtension($extension);
}
return $this;
}
/**
* Get a template path.
* @param string $name
* @return string
*/
public function path($name)
{
$name = new Name($this, $name);
return $name->getPath();
}
/**
* Check if a template exists.
* @param string $name
* @return boolean
*/
public function exists($name)
{
$name = new Name($this, $name);
return $name->doesPathExist();
}
/**
* Create a new template.
* @param string $name
* @return Template
*/
public function make($name)
{
return new Template($this, $name);
}
/**
* Create a new template and render it.
* @param string $name
* @param array $data
* @return string
*/
public function render($name, array $data = array())
{
return $this->make($name)->render($data);
}
}
+85
View File
@@ -0,0 +1,85 @@
<?php
namespace League\Plates\Extension;
use League\Plates\Engine;
use League\Plates\Template\Template;
use LogicException;
/**
* Extension that adds the ability to create "cache busted" asset URLs.
*/
class Asset implements ExtensionInterface
{
/**
* Instance of the current template.
* @var Template
*/
public $template;
/**
* Path to asset directory.
* @var string
*/
public $path;
/**
* Enables the filename method.
* @var boolean
*/
public $filenameMethod;
/**
* Create new Asset instance.
* @param string $path
* @param boolean $filenameMethod
*/
public function __construct($path, $filenameMethod = false)
{
$this->path = rtrim($path, '/');
$this->filenameMethod = $filenameMethod;
}
/**
* Register extension function.
* @param Engine $engine
* @return null
*/
public function register(Engine $engine)
{
$engine->registerFunction('asset', array($this, 'cachedAssetUrl'));
}
/**
* Create "cache busted" asset URL.
* @param string $url
* @return string
*/
public function cachedAssetUrl($url)
{
$filePath = $this->path . '/' . ltrim($url, '/');
if (!file_exists($filePath)) {
throw new LogicException(
'Unable to locate the asset "' . $url . '" in the "' . $this->path . '" directory.'
);
}
$lastUpdated = filemtime($filePath);
$pathInfo = pathinfo($url);
if ($pathInfo['dirname'] === '.') {
$directory = '';
} elseif ($pathInfo['dirname'] === '/') {
$directory = '/';
} else {
$directory = $pathInfo['dirname'] . '/';
}
if ($this->filenameMethod) {
return $directory . $pathInfo['filename'] . '.' . $lastUpdated . '.' . $pathInfo['extension'];
}
return $directory . $pathInfo['filename'] . '.' . $pathInfo['extension'] . '?v=' . $lastUpdated;
}
}
@@ -0,0 +1,13 @@
<?php
namespace League\Plates\Extension;
use League\Plates\Engine;
/**
* A common interface for extensions.
*/
interface ExtensionInterface
{
public function register(Engine $engine);
}
+113
View File
@@ -0,0 +1,113 @@
<?php
namespace League\Plates\Extension;
use League\Plates\Engine;
use League\Plates\Template\Template;
use LogicException;
/**
* Extension that adds a number of URI checks.
*/
class URI implements ExtensionInterface
{
/**
* Instance of the current template.
* @var Template
*/
public $template;
/**
* The request URI.
* @var string
*/
protected $uri;
/**
* The request URI as an array.
* @var array
*/
protected $parts;
/**
* Create new URI instance.
* @param string $uri
*/
public function __construct($uri)
{
$this->uri = $uri;
$this->parts = explode('/', $this->uri);
}
/**
* Register extension functions.
* @param Engine $engine
* @return null
*/
public function register(Engine $engine)
{
$engine->registerFunction('uri', array($this, 'runUri'));
}
/**
* Perform URI check.
* @param null|integer|string $var1
* @param mixed $var2
* @param mixed $var3
* @param mixed $var4
* @return mixed
*/
public function runUri($var1 = null, $var2 = null, $var3 = null, $var4 = null)
{
if (is_null($var1)) {
return $this->uri;
}
if (is_numeric($var1) and is_null($var2)) {
return array_key_exists($var1, $this->parts) ? $this->parts[$var1] : null;
}
if (is_numeric($var1) and is_string($var2)) {
return $this->checkUriSegmentMatch($var1, $var2, $var3, $var4);
}
if (is_string($var1)) {
return $this->checkUriRegexMatch($var1, $var2, $var3);
}
throw new LogicException('Invalid use of the uri function.');
}
/**
* Perform a URI segment match.
* @param integer $key
* @param string $string
* @param mixed $returnOnTrue
* @param mixed $returnOnFalse
* @return mixed
*/
protected function checkUriSegmentMatch($key, $string, $returnOnTrue = null, $returnOnFalse = null)
{
if (array_key_exists($key, $this->parts) && $this->parts[$key] === $string) {
return is_null($returnOnTrue) ? true : $returnOnTrue;
}
return is_null($returnOnFalse) ? false : $returnOnFalse;
}
/**
* Perform a regular express match.
* @param string $regex
* @param mixed $returnOnTrue
* @param mixed $returnOnFalse
* @return mixed
*/
protected function checkUriRegexMatch($regex, $returnOnTrue = null, $returnOnFalse = null)
{
if (preg_match('#^' . $regex . '$#', $this->uri) === 1) {
return is_null($returnOnTrue) ? true : $returnOnTrue;
}
return is_null($returnOnFalse) ? false : $returnOnFalse;
}
}
+93
View File
@@ -0,0 +1,93 @@
<?php
namespace League\Plates\Template;
use LogicException;
/**
* Preassigned template data.
*/
class Data
{
/**
* Variables shared by all templates.
* @var array
*/
protected $sharedVariables = array();
/**
* Specific template variables.
* @var array
*/
protected $templateVariables = array();
/**
* Add template data.
* @param array $data;
* @param null|string|array $templates;
* @return Data
*/
public function add(array $data, $templates = null)
{
if (is_null($templates)) {
return $this->shareWithAll($data);
}
if (is_array($templates)) {
return $this->shareWithSome($data, $templates);
}
if (is_string($templates)) {
return $this->shareWithSome($data, array($templates));
}
throw new LogicException(
'The templates variable must be null, an array or a string, ' . gettype($templates) . ' given.'
);
}
/**
* Add data shared with all templates.
* @param array $data;
* @return Data
*/
public function shareWithAll($data)
{
$this->sharedVariables = array_merge($this->sharedVariables, $data);
return $this;
}
/**
* Add data shared with some templates.
* @param array $data;
* @param array $templates;
* @return Data
*/
public function shareWithSome($data, array $templates)
{
foreach ($templates as $template) {
if (isset($this->templateVariables[$template])) {
$this->templateVariables[$template] = array_merge($this->templateVariables[$template], $data);
} else {
$this->templateVariables[$template] = $data;
}
}
return $this;
}
/**
* Get template data.
* @param null|string $template;
* @return array
*/
public function get($template = null)
{
if (isset($template, $this->templateVariables[$template])) {
return array_merge($this->sharedVariables, $this->templateVariables[$template]);
}
return $this->sharedVariables;
}
}
+53
View File
@@ -0,0 +1,53 @@
<?php
namespace League\Plates\Template;
use LogicException;
/**
* Default template directory.
*/
class Directory
{
/**
* Template directory path.
* @var string
*/
protected $path;
/**
* Create new Directory instance.
* @param string $path
*/
public function __construct($path = null)
{
$this->set($path);
}
/**
* Set path to templates directory.
* @param string|null $path Pass null to disable the default directory.
* @return Directory
*/
public function set($path)
{
if (!is_null($path) and !is_dir($path)) {
throw new LogicException(
'The specified path "' . $path . '" does not exist.'
);
}
$this->path = $path;
return $this;
}
/**
* Get path to templates directory.
* @return string
*/
public function get()
{
return $this->path;
}
}
+45
View File
@@ -0,0 +1,45 @@
<?php
namespace League\Plates\Template;
/**
* Template file extension.
*/
class FileExtension
{
/**
* Template file extension.
* @var string
*/
protected $fileExtension;
/**
* Create new FileExtension instance.
* @param null|string $fileExtension
*/
public function __construct($fileExtension = 'php')
{
$this->set($fileExtension);
}
/**
* Set the template file extension.
* @param null|string $fileExtension
* @return FileExtension
*/
public function set($fileExtension)
{
$this->fileExtension = $fileExtension;
return $this;
}
/**
* Get the template file extension.
* @return string
*/
public function get()
{
return $this->fileExtension;
}
}
+109
View File
@@ -0,0 +1,109 @@
<?php
namespace League\Plates\Template;
use LogicException;
/**
* A template folder.
*/
class Folder
{
/**
* The folder name.
* @var string
*/
protected $name;
/**
* The folder path.
* @var string
*/
protected $path;
/**
* The folder fallback status.
* @var boolean
*/
protected $fallback;
/**
* Create a new Folder instance.
* @param string $name
* @param string $path
* @param boolean $fallback
*/
public function __construct($name, $path, $fallback = false)
{
$this->setName($name);
$this->setPath($path);
$this->setFallback($fallback);
}
/**
* Set the folder name.
* @param string $name
* @return Folder
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get the folder name.
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set the folder path.
* @param string $path
* @return Folder
*/
public function setPath($path)
{
if (!is_dir($path)) {
throw new LogicException('The specified directory path "' . $path . '" does not exist.');
}
$this->path = $path;
return $this;
}
/**
* Get the folder path.
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* Set the folder fallback status.
* @param boolean $fallback
* @return Folder
*/
public function setFallback($fallback)
{
$this->fallback = $fallback;
return $this;
}
/**
* Get the folder fallback status.
* @return boolean
*/
public function getFallback()
{
return $this->fallback;
}
}
+75
View File
@@ -0,0 +1,75 @@
<?php
namespace League\Plates\Template;
use LogicException;
/**
* A collection of template folders.
*/
class Folders
{
/**
* Array of template folders.
* @var array
*/
protected $folders = array();
/**
* Add a template folder.
* @param string $name
* @param string $path
* @param boolean $fallback
* @return Folders
*/
public function add($name, $path, $fallback = false)
{
if ($this->exists($name)) {
throw new LogicException('The template folder "' . $name . '" is already being used.');
}
$this->folders[$name] = new Folder($name, $path, $fallback);
return $this;
}
/**
* Remove a template folder.
* @param string $name
* @return Folders
*/
public function remove($name)
{
if (!$this->exists($name)) {
throw new LogicException('The template folder "' . $name . '" was not found.');
}
unset($this->folders[$name]);
return $this;
}
/**
* Get a template folder.
* @param string $name
* @return Folder
*/
public function get($name)
{
if (!$this->exists($name)) {
throw new LogicException('The template folder "' . $name . '" was not found.');
}
return $this->folders[$name];
}
/**
* Check if a template folder exists.
* @param string $name
* @return boolean
*/
public function exists($name)
{
return isset($this->folders[$name]);
}
}
+107
View File
@@ -0,0 +1,107 @@
<?php
namespace League\Plates\Template;
use League\Plates\Extension\ExtensionInterface;
use LogicException;
/**
* A template function.
*/
class Func
{
/**
* The function name.
* @var string
*/
protected $name;
/**
* The function callback.
* @var callable
*/
protected $callback;
/**
* Create new Func instance.
* @param string $name
* @param callable $callback
*/
public function __construct($name, $callback)
{
$this->setName($name);
$this->setCallback($callback);
}
/**
* Set the function name.
* @param string $name
* @return Func
*/
public function setName($name)
{
if (preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $name) !== 1) {
throw new LogicException(
'Not a valid function name.'
);
}
$this->name = $name;
return $this;
}
/**
* Get the function name.
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set the function callback
* @param callable $callback
* @return Func
*/
public function setCallback($callback)
{
if (!is_callable($callback, true)) {
throw new LogicException(
'Not a valid function callback.'
);
}
$this->callback = $callback;
return $this;
}
/**
* Get the function callback.
* @return callable
*/
public function getCallback()
{
return $this->callback;
}
/**
* Call the function.
* @param Template $template
* @param array $arguments
* @return mixed
*/
public function call(Template $template = null, $arguments = array())
{
if (is_array($this->callback) and
isset($this->callback[0]) and
$this->callback[0] instanceof ExtensionInterface
) {
$this->callback[0]->template = $template;
}
return call_user_func_array($this->callback, $arguments);
}
}
+78
View File
@@ -0,0 +1,78 @@
<?php
namespace League\Plates\Template;
use LogicException;
/**
* A collection of template functions.
*/
class Functions
{
/**
* Array of template functions.
* @var array
*/
protected $functions = array();
/**
* Add a new template function.
* @param string $name;
* @param callback $callback;
* @return Functions
*/
public function add($name, $callback)
{
if ($this->exists($name)) {
throw new LogicException(
'The template function name "' . $name . '" is already registered.'
);
}
$this->functions[$name] = new Func($name, $callback);
return $this;
}
/**
* Remove a template function.
* @param string $name;
* @return Functions
*/
public function remove($name)
{
if (!$this->exists($name)) {
throw new LogicException(
'The template function "' . $name . '" was not found.'
);
}
unset($this->functions[$name]);
return $this;
}
/**
* Get a template function.
* @param string $name
* @return Func
*/
public function get($name)
{
if (!$this->exists($name)) {
throw new LogicException('The template function "' . $name . '" was not found.');
}
return $this->functions[$name];
}
/**
* Check if a template function exists.
* @param string $name
* @return boolean
*/
public function exists($name)
{
return isset($this->functions[$name]);
}
}
+202
View File
@@ -0,0 +1,202 @@
<?php
namespace League\Plates\Template;
use League\Plates\Engine;
use LogicException;
/**
* A template name.
*/
class Name
{
/**
* Instance of the template engine.
* @var Engine
*/
protected $engine;
/**
* The original name.
* @var string
*/
protected $name;
/**
* The parsed template folder.
* @var Folder
*/
protected $folder;
/**
* The parsed template filename.
* @var string
*/
protected $file;
/**
* Create a new Name instance.
* @param Engine $engine
* @param string $name
*/
public function __construct(Engine $engine, $name)
{
$this->setEngine($engine);
$this->setName($name);
}
/**
* Set the engine.
* @param Engine $engine
* @return Name
*/
public function setEngine(Engine $engine)
{
$this->engine = $engine;
return $this;
}
/**
* Get the engine.
* @return Engine
*/
public function getEngine()
{
return $this->engine;
}
/**
* Set the original name and parse it.
* @param string $name
* @return Name
*/
public function setName($name)
{
$this->name = $name;
$parts = explode('::', $this->name);
if (count($parts) === 1) {
$this->setFile($parts[0]);
} elseif (count($parts) === 2) {
$this->setFolder($parts[0]);
$this->setFile($parts[1]);
} else {
throw new LogicException(
'The template name "' . $this->name . '" is not valid. ' .
'Do not use the folder namespace separator "::" more than once.'
);
}
return $this;
}
/**
* Get the original name.
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set the parsed template folder.
* @param string $folder
* @return Name
*/
public function setFolder($folder)
{
$this->folder = $this->engine->getFolders()->get($folder);
return $this;
}
/**
* Get the parsed template folder.
* @return string
*/
public function getFolder()
{
return $this->folder;
}
/**
* Set the parsed template file.
* @param string $file
* @return Name
*/
public function setFile($file)
{
if ($file === '') {
throw new LogicException(
'The template name "' . $this->name . '" is not valid. ' .
'The template name cannot be empty.'
);
}
$this->file = $file;
if (!is_null($this->engine->getFileExtension())) {
$this->file .= '.' . $this->engine->getFileExtension();
}
return $this;
}
/**
* Get the parsed template file.
* @return string
*/
public function getFile()
{
return $this->file;
}
/**
* Resolve template path.
* @return string
*/
public function getPath()
{
if (is_null($this->folder)) {
return $this->getDefaultDirectory() . DIRECTORY_SEPARATOR . $this->file;
}
$path = $this->folder->getPath() . DIRECTORY_SEPARATOR . $this->file;
if (!is_file($path) and $this->folder->getFallback() and is_file($this->getDefaultDirectory() . DIRECTORY_SEPARATOR . $this->file)) {
$path = $this->getDefaultDirectory() . DIRECTORY_SEPARATOR . $this->file;
}
return $path;
}
/**
* Check if template path exists.
* @return boolean
*/
public function doesPathExist()
{
return is_file($this->getPath());
}
/**
* Get the default templates directory.
* @return string
*/
protected function getDefaultDirectory()
{
$directory = $this->engine->getDirectory();
if (is_null($directory)) {
throw new LogicException(
'The template name "' . $this->name . '" is not valid. '.
'The default directory has not been defined.'
);
}
return $directory;
}
}
+346
View File
@@ -0,0 +1,346 @@
<?php
namespace League\Plates\Template;
use Exception;
use League\Plates\Engine;
use LogicException;
use Throwable;
/**
* Container which holds template data and provides access to template functions.
*/
class Template
{
/**
* Instance of the template engine.
* @var Engine
*/
protected $engine;
/**
* The name of the template.
* @var Name
*/
protected $name;
/**
* The data assigned to the template.
* @var array
*/
protected $data = array();
/**
* An array of section content.
* @var array
*/
protected $sections = array();
/**
* The name of the section currently being rendered.
* @var string
*/
protected $sectionName;
/**
* Whether the section should be appended or not.
* @var boolean
*/
protected $appendSection;
/**
* The name of the template layout.
* @var string
*/
protected $layoutName;
/**
* The data assigned to the template layout.
* @var array
*/
protected $layoutData;
/**
* Create new Template instance.
* @param Engine $engine
* @param string $name
*/
public function __construct(Engine $engine, $name)
{
$this->engine = $engine;
$this->name = new Name($engine, $name);
$this->data($this->engine->getData($name));
}
/**
* Magic method used to call extension functions.
* @param string $name
* @param array $arguments
* @return mixed
*/
public function __call($name, $arguments)
{
return $this->engine->getFunction($name)->call($this, $arguments);
}
/**
* Alias for render() method.
* @throws \Throwable
* @throws \Exception
* @return string
*/
public function __toString()
{
return $this->render();
}
/**
* Assign or get template data.
* @param array $data
* @return mixed
*/
public function data(array $data = null)
{
if (is_null($data)) {
return $this->data;
}
$this->data = array_merge($this->data, $data);
}
/**
* Check if the template exists.
* @return boolean
*/
public function exists()
{
return $this->name->doesPathExist();
}
/**
* Get the template path.
* @return string
*/
public function path()
{
return $this->name->getPath();
}
/**
* Render the template and layout.
* @param array $data
* @throws \Throwable
* @throws \Exception
* @return string
*/
public function render(array $data = array())
{
$this->data($data);
unset($data);
extract($this->data);
if (!$this->exists()) {
throw new LogicException(
'The template "' . $this->name->getName() . '" could not be found at "' . $this->path() . '".'
);
}
try {
$level = ob_get_level();
ob_start();
include $this->path();
$content = ob_get_clean();
if (isset($this->layoutName)) {
$layout = $this->engine->make($this->layoutName);
$layout->sections = array_merge($this->sections, array('content' => $content));
$content = $layout->render($this->layoutData);
}
return $content;
} catch (Throwable $e) {
while (ob_get_level() > $level) {
ob_end_clean();
}
throw $e;
} catch (Exception $e) {
while (ob_get_level() > $level) {
ob_end_clean();
}
throw $e;
}
}
/**
* Set the template's layout.
* @param string $name
* @param array $data
* @return null
*/
public function layout($name, array $data = array())
{
$this->layoutName = $name;
$this->layoutData = $data;
}
/**
* Start a new section block.
* @param string $name
* @return null
*/
public function start($name)
{
if ($name === 'content') {
throw new LogicException(
'The section name "content" is reserved.'
);
}
if ($this->sectionName) {
throw new LogicException('You cannot nest sections within other sections.');
}
$this->sectionName = $name;
ob_start();
}
/**
* Start a new append section block.
* @param string $name
* @return null
*/
public function push($name)
{
$this->appendSection = true;
$this->start($name);
}
/**
* Stop the current section block.
* @return null
*/
public function stop()
{
if (is_null($this->sectionName)) {
throw new LogicException(
'You must start a section before you can stop it.'
);
}
if (!isset($this->sections[$this->sectionName])) {
$this->sections[$this->sectionName] = '';
}
$this->sections[$this->sectionName] = $this->appendSection ? $this->sections[$this->sectionName] . ob_get_clean() : ob_get_clean();
$this->sectionName = null;
$this->appendSection = false;
}
/**
* Alias of stop().
* @return null
*/
public function end()
{
$this->stop();
}
/**
* Returns the content for a section block.
* @param string $name Section name
* @param string $default Default section content
* @return string|null
*/
public function section($name, $default = null)
{
if (!isset($this->sections[$name])) {
return $default;
}
return $this->sections[$name];
}
/**
* Fetch a rendered template.
* @param string $name
* @param array $data
* @return string
*/
public function fetch($name, array $data = array())
{
return $this->engine->render($name, $data);
}
/**
* Output a rendered template.
* @param string $name
* @param array $data
* @return null
*/
public function insert($name, array $data = array())
{
echo $this->engine->render($name, $data);
}
/**
* Apply multiple functions to variable.
* @param mixed $var
* @param string $functions
* @return mixed
*/
public function batch($var, $functions)
{
foreach (explode('|', $functions) as $function) {
if ($this->engine->doesFunctionExist($function)) {
$var = call_user_func(array($this, $function), $var);
} elseif (is_callable($function)) {
$var = call_user_func($function, $var);
} else {
throw new LogicException(
'The batch function could not find the "' . $function . '" function.'
);
}
}
return $var;
}
/**
* Escape string.
* @param string $string
* @param null|string $functions
* @return string
*/
public function escape($string, $functions = null)
{
static $flags;
if (!isset($flags)) {
$flags = ENT_QUOTES | (defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0);
}
if ($functions) {
$string = $this->batch($string, $functions);
}
return htmlspecialchars($string, $flags, 'UTF-8');
}
/**
* Alias to escape function.
* @param string $string
* @param null|string $functions
* @return string
*/
public function e($string, $functions = null)
{
return $this->escape($string, $functions);
}
}
+20
View File
@@ -0,0 +1,20 @@
<?php
include 'Engine.php';
include 'Extension/ExtensionInterface.php';
include 'Template/Data.php';
include 'Template/Directory.php';
include 'Template/FileExtension.php';
include 'Template/Folders.php';
include 'Template/Func.php';
include 'Template/Functions.php';
include 'Template/Name.php';
include 'Template/Template.php';
// Create new Plates instance
$templates = new League\Plates\Engine('templates');
// Preassign data to the layout
$templates->addData(['company' => 'The Company Name'], 'layout');
// Render a template
echo $templates->render('profile', ['name' => 'Jonathan']);
+14
View File
@@ -0,0 +1,14 @@
<?php
include __DIR__.'Engine.php';
include __DIR__.'Extension/ExtensionInterface.php';
include __DIR__.'Template/Data.php';
include __DIR__.'Template/Directory.php';
include __DIR__.'Template/FileExtension.php';
include __DIR__.'Template/Folders.php';
include __DIR__.'Template/Func.php';
include __DIR__.'Template/Functions.php';
include __DIR__.'Template/Name.php';
include __DIR__.'Template/Template.php';
?>
+12
View File
@@ -0,0 +1,12 @@
<html>
<head>
<title><?=$this->e($title)?> | <?=$this->e($company)?></title>
</head>
<body>
<?=$this->section('content')?>
<?=$this->section('scripts')?>
</body>
</html>
+12
View File
@@ -0,0 +1,12 @@
<?php $this->layout('layout', ['title' => 'User Profile']) ?>
<h1>User Profile</h1>
<p>Hello, <?=$this->e($name)?>!</p>
<?php $this->insert('sidebar') ?>
<?php $this->push('scripts') ?>
<script>
// Some JavaScript
</script>
<?php $this->end() ?>
+6
View File
@@ -0,0 +1,6 @@
<ul>
<li><a href="#link">Example sidebar link</a></li>
<li><a href="#link">Example sidebar link</a></li>
<li><a href="#link">Example sidebar link</a></li>
<li><a href="#link">Example sidebar link</a></li>
</ul>