Skip to content

Instantly share code, notes, and snippets.

@AnrDaemon
Created September 23, 2017 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnrDaemon/47ec9ea9036a8c37d702ae73d92fc243 to your computer and use it in GitHub Desktop.
Save AnrDaemon/47ec9ea9036a8c37d702ae73d92fc243 to your computer and use it in GitHub Desktop.
<?php
/** Smarty template wrapper
*
* @version $Id: Smarty.php 674 2017-07-10 23:47:11Z anrdaemon $
*/
namespace AnrDaemon\CcWeb\Wrappers;
use
AnrDaemon\CcWeb\Interfaces;
class Smarty
implements Interfaces\TemplateManager
{
protected $tpl;
/** Embeddable Smarty templates.
*
* Script-embedded Smarty templates.
* Use __halt_compiler();?> to stop PHP parsing/execution and start
* template code. Template will start immediately past closing bracket.
*
* @return void
*/
public function enableEmbed()
{
$this->tpl->registerFilter('pre',
function($tpl_source, \Smarty_Internal_Template $template)
{
$stack = stristr($tpl_source, '__HALT_COMPILER');
return empty($stack) ? $tpl_source : substr($stack, strpos($stack, '?>') + 2);
}
);
}
public static function wrap(\Smarty_Internal_Template $tpl)
{
$self = new static;
$self->tpl = $tpl;
return $self;
}
// Magic!
public function __construct(...$args)
{
$tpl = new \Smarty(...$args);
$tpl::$_CHARSET = ini_get('default_charset');
$this->tpl = $tpl;
}
public function __call($method, $args)
{
$tpl = $this->tpl->$method(...$args);
if($tpl === $this->tpl)
{
return $this;
}
elseif($tpl instanceof \Smarty_Internal_Template)
{
return static::wrap($tpl);
}
else
{
return $tpl;
}
}
public function __get($name)
{
return $this->tpl->$name;
}
public function __set($name, $value)
{
return $this->tpl->$name = $value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment