mirror of
https://github.com/getgrav/grav.git
synced 2026-07-08 16:53:16 +02:00
Improved evaluate() Twig function to use environment/context
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
* Added optional parameter to `|markdown(false)` filter to toggle block/line processing (default|true = `block`)
|
||||
* Added new `Page::folderExists()` method
|
||||
1. [](#improved)
|
||||
* `Twig::evaluate()` now takes current environment and context into account
|
||||
* Genericized `direct-install` so it can be called via Admin plugin
|
||||
1. [](#bugfix)
|
||||
* Fixed a minor bug in Number validation [#1329](https://github.com/getgrav/grav/issues/1329)
|
||||
|
||||
@@ -104,7 +104,8 @@ class TwigExtension extends \Twig_Extension
|
||||
new \Twig_simpleFunction('authorize', [$this, 'authorize']),
|
||||
new \Twig_SimpleFunction('debug', [$this, 'dump'], ['needs_context' => true, 'needs_environment' => true]),
|
||||
new \Twig_SimpleFunction('dump', [$this, 'dump'], ['needs_context' => true, 'needs_environment' => true]),
|
||||
new \Twig_SimpleFunction('evaluate', [$this, 'evaluateFunc']),
|
||||
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('gist', [$this, 'gistFunc']),
|
||||
new \Twig_SimpleFunction('nonce_field', [$this, 'nonceFieldFunc']),
|
||||
new \Twig_simpleFunction('random_string', [$this, 'randomStringFunc']),
|
||||
@@ -587,22 +588,51 @@ class TwigExtension extends \Twig_Extension
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluate a string
|
||||
* This function will evaluate Twig $twig through the $environment, and return its results.
|
||||
*
|
||||
* @example {{ evaluate('grav.language.getLanguage') }}
|
||||
*
|
||||
* @param string $input String to be evaluated
|
||||
*
|
||||
* @return string Returns the evaluated string
|
||||
* @param \Twig_Environment $environment
|
||||
* @param array $context
|
||||
* @param string $twig
|
||||
* @return mixed
|
||||
*/
|
||||
public function evaluateFunc($input)
|
||||
{
|
||||
if (!$input) { //prevent an obscure Twig error if $input is not set
|
||||
$input = '""';
|
||||
}
|
||||
return $this->grav['twig']->processString("{{ $input }}");
|
||||
public function evaluateTwigFunc( \Twig_Environment $environment, $context, $twig ) {
|
||||
$loader = $environment->getLoader( );
|
||||
|
||||
$parsed = $this->parseString( $environment, $context, $twig );
|
||||
|
||||
$environment->setLoader( $loader );
|
||||
return $parsed;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 )
|
||||
{
|
||||
$parsed = $this->evaluateTwigFunc($environment, $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
|
||||
* (c) 2011 Fabien Potencier
|
||||
|
||||
Reference in New Issue
Block a user