mirror of
https://github.com/getformwork/formwork.git
synced 2025-02-25 02:02:57 +01:00
Add Renderer
class
This commit is contained in:
parent
41bf263560
commit
9f518cbf56
50
formwork/Template/Renderer.php
Normal file
50
formwork/Template/Renderer.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace Formwork\Template;
|
||||
|
||||
use Closure;
|
||||
|
||||
class Renderer
|
||||
{
|
||||
/**
|
||||
* Renderer instance
|
||||
*
|
||||
* @var Renderer
|
||||
*/
|
||||
protected static $instance;
|
||||
|
||||
/**
|
||||
* Load a script passing variables and binding to given instance and context
|
||||
*
|
||||
* @param string $filename
|
||||
* @param array $vars
|
||||
* @param object $instance
|
||||
* @param string|null $context
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function load($filename, array $vars, $instance, $context = null)
|
||||
{
|
||||
if (is_null(static::$instance)) {
|
||||
static::$instance = new static();
|
||||
}
|
||||
$closure = static::$instance->getClosure($instance, $context);
|
||||
return $closure($filename, $vars);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return rendering closure bound to given instance and context
|
||||
*
|
||||
* @param object $instance
|
||||
* @param string|null $context
|
||||
*
|
||||
* @return Closure
|
||||
*/
|
||||
protected function getClosure($instance, $context = null)
|
||||
{
|
||||
return Closure::bind(function ($_filename, array $_vars) {
|
||||
extract($_vars);
|
||||
return include $_filename;
|
||||
}, $instance, $context);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user