diff --git a/CHANGELOG.md b/CHANGELOG.md index 73639b4e3d..4400a01062 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ * Stopped Chrome from auto-completing admin user profile form [#1847](https://github.com/getgrav/grav/issues/1847) 1. [](#bugfix) * Fixed OpenGraph metatags so only Twitter uses `name=`, and all others use `property=` [#1849](https://github.com/getgrav/grav/issues/1849) + * Fixed an issue with `evaluate()` and `evaluate_twig()` Twig functions that throws invalid template error # v1.4.0-rc.1 ## 01/22/2018 diff --git a/system/src/Grav/Common/Twig/TwigExtension.php b/system/src/Grav/Common/Twig/TwigExtension.php index 578f97987c..e4cd9f40d2 100644 --- a/system/src/Grav/Common/Twig/TwigExtension.php +++ b/system/src/Grav/Common/Twig/TwigExtension.php @@ -120,8 +120,8 @@ public function getFunctions() 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'), @@ -687,48 +687,33 @@ public function urlFunc($input, $domain = false) /** * 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