Skip to content

Commit

Permalink
Fix for invalid template error in evaluate()
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Feb 6, 2018
1 parent 4c654ef commit f91678d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 10 additions & 25 deletions system/src/Grav/Common/Twig/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f91678d

Please sign in to comment.