mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-14 19:36:43 +00:00
allows macros to have the same kind of logic than the one in regular templates
git-svn-id: http://svn.twig-project.org/trunk@88 93ef8e89-cb99-4229-a87c-7fa0fa45744b
This commit is contained in:
+1
-17
@@ -8,22 +8,6 @@
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
abstract class Twig_Macro implements Twig_MacroInterface
|
||||
abstract class Twig_Macro extends Twig_Resource
|
||||
{
|
||||
protected $env;
|
||||
|
||||
public function __construct(Twig_Environment $env)
|
||||
{
|
||||
$this->env = $env;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the bound environment for this template.
|
||||
*
|
||||
* @return Twig_Environment The current environment
|
||||
*/
|
||||
public function getEnvironment()
|
||||
{
|
||||
return $this->env;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Twig.
|
||||
*
|
||||
* (c) 2009 Fabien Potencier
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
interface Twig_MacroInterface
|
||||
{
|
||||
/**
|
||||
* Returns the bound environment for this template.
|
||||
*
|
||||
* @return Twig_Environment The current environment
|
||||
*/
|
||||
public function getEnvironment();
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Twig.
|
||||
*
|
||||
* (c) 2009 Fabien Potencier
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
abstract class Twig_Resource
|
||||
{
|
||||
protected $env;
|
||||
|
||||
public function __construct(Twig_Environment $env)
|
||||
{
|
||||
$this->env = $env;
|
||||
}
|
||||
|
||||
public function getEnvironment()
|
||||
{
|
||||
return $this->env;
|
||||
}
|
||||
|
||||
protected function resolveMissingFilter($name)
|
||||
{
|
||||
throw new Twig_RuntimeError(sprintf('The filter "%s" does not exist', $name));
|
||||
}
|
||||
|
||||
protected function getAttribute($object, $item, array $arguments = array(), $arrayOnly = false)
|
||||
{
|
||||
$item = (string) $item;
|
||||
|
||||
if ((is_array($object) || is_object($object) && $object instanceof ArrayAccess) && isset($object[$item]))
|
||||
{
|
||||
return $object[$item];
|
||||
}
|
||||
|
||||
if ($arrayOnly)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (
|
||||
!is_object($object) ||
|
||||
(
|
||||
!method_exists($object, $method = $item) &&
|
||||
!method_exists($object, $method = 'get'.ucfirst($item))
|
||||
)
|
||||
)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->env->hasExtension('sandbox'))
|
||||
{
|
||||
$this->env->getExtension('sandbox')->checkMethodAllowed($object, $method);
|
||||
}
|
||||
|
||||
return call_user_func_array(array($object, $method), $arguments);
|
||||
}
|
||||
}
|
||||
+1
-51
@@ -9,15 +9,8 @@
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
abstract class Twig_Template implements Twig_TemplateInterface
|
||||
abstract class Twig_Template extends Twig_Resource implements Twig_TemplateInterface
|
||||
{
|
||||
protected $env;
|
||||
|
||||
public function __construct(Twig_Environment $env)
|
||||
{
|
||||
$this->env = $env;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the template with the given context and returns it as string.
|
||||
*
|
||||
@@ -32,47 +25,4 @@ abstract class Twig_Template implements Twig_TemplateInterface
|
||||
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
public function getEnvironment()
|
||||
{
|
||||
return $this->env;
|
||||
}
|
||||
|
||||
protected function resolveMissingFilter($name)
|
||||
{
|
||||
throw new Twig_RuntimeError(sprintf('The filter "%s" does not exist', $name));
|
||||
}
|
||||
|
||||
protected function getAttribute($object, $item, array $arguments = array(), $arrayOnly = false)
|
||||
{
|
||||
$item = (string) $item;
|
||||
|
||||
if ((is_array($object) || is_object($object) && $object instanceof ArrayAccess) && isset($object[$item]))
|
||||
{
|
||||
return $object[$item];
|
||||
}
|
||||
|
||||
if ($arrayOnly)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (
|
||||
!is_object($object) ||
|
||||
(
|
||||
!method_exists($object, $method = $item) &&
|
||||
!method_exists($object, $method = 'get'.ucfirst($item))
|
||||
)
|
||||
)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->env->hasExtension('sandbox'))
|
||||
{
|
||||
$this->env->getExtension('sandbox')->checkMethodAllowed($object, $method);
|
||||
}
|
||||
|
||||
return call_user_func_array(array($object, $method), $arguments);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user