template 엔진 '일단' 추가
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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]);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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]);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user