mirror of
https://github.com/getgrav/grav.git
synced 2026-03-14 16:41:06 +01:00
fixed twig evaluate function
This commit is contained in:
@@ -123,8 +123,8 @@ class TwigExtension extends \Twig_Extension
|
||||
new \Twig_SimpleFunction('vardump', [$this, 'vardumpFunc']),
|
||||
new \Twig_SimpleFunction('print_r', 'print_r'),
|
||||
new \Twig_SimpleFunction('http_response_code', 'http_response_code'),
|
||||
new \Twig_SimpleFunction('evaluate', [$this, 'evaluateStringFunc'], ['needs_context' => true, 'needs_environment' => true]),
|
||||
new \Twig_SimpleFunction('evaluate_twig', [$this, 'evaluateTwigFunc'], ['needs_context' => true, 'needs_environment' => true]),
|
||||
new \Twig_SimpleFunction('evaluate', [$this, 'evaluateStringFunc'], ['needs_context' => true]),
|
||||
new \Twig_SimpleFunction('evaluate_twig', [$this, 'evaluateTwigFunc'], ['needs_context' => true]),
|
||||
new \Twig_SimpleFunction('gist', [$this, 'gistFunc']),
|
||||
new \Twig_SimpleFunction('nonce_field', [$this, 'nonceFieldFunc']),
|
||||
new \Twig_SimpleFunction('pathinfo', 'pathinfo'),
|
||||
@@ -703,48 +703,33 @@ class TwigExtension extends \Twig_Extension
|
||||
/**
|
||||
* This function will evaluate Twig $twig through the $environment, and return its results.
|
||||
*
|
||||
* @param \Twig_Environment $environment
|
||||
* @param array $context
|
||||
* @param string $twig
|
||||
* @return mixed
|
||||
*/
|
||||
public function evaluateTwigFunc( \Twig_Environment $environment, $context, $twig ) {
|
||||
$loader = $environment->getLoader( );
|
||||
public function evaluateTwigFunc($context, $twig ) {
|
||||
|
||||
$parsed = $this->parseString( $environment, $context, $twig );
|
||||
$loader = new \Twig_Loader_Filesystem('.');
|
||||
$env = new \Twig_Environment($loader);
|
||||
|
||||
$environment->setLoader( $loader );
|
||||
return $parsed;
|
||||
$template = $env->createTemplate($twig);
|
||||
return $template->render($context);
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will evaluate a $string through the $environment, and return its results.
|
||||
*
|
||||
* @param \Twig_Environment $environment
|
||||
* @param $context
|
||||
* @param $string
|
||||
* @return mixed
|
||||
*/
|
||||
public function evaluateStringFunc(\Twig_Environment $environment, $context, $string )
|
||||
public function evaluateStringFunc($context, $string )
|
||||
{
|
||||
$parsed = $this->evaluateTwigFunc($environment, $context, "{{ $string }}");
|
||||
$parsed = $this->evaluateTwigFunc($context, "{{ $string }}");
|
||||
return $parsed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the parser for the environment to Twig_Loader_String, and parsed the string $string.
|
||||
*
|
||||
* @param \Twig_Environment $environment
|
||||
* @param array $context
|
||||
* @param string $string
|
||||
* @return string
|
||||
*/
|
||||
protected function parseString( \Twig_Environment $environment, $context, $string ) {
|
||||
$environment->setLoader( new \Twig_Loader_String( ) );
|
||||
return $environment->render( $string, $context );
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Based on Twig_Extension_Debug / twig_var_dump
|
||||
|
||||
Reference in New Issue
Block a user