|
@@ -18,11 +18,11 @@ use Grav\Common\Twig\TokenParser\TwigTokenParserTryCatch;
|
|
|
use Grav\Common\Twig\TokenParser\TwigTokenParserMarkdown;
|
|
|
use Grav\Common\User\User;
|
|
|
use Grav\Common\Utils;
|
|
|
+use Grav\Common\Yaml;
|
|
|
use Grav\Common\Markdown\Parsedown;
|
|
|
use Grav\Common\Markdown\ParsedownExtra;
|
|
|
use Grav\Common\Helpers\Base32;
|
|
|
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
|
|
|
-use Symfony\Component\Yaml\Yaml;
|
|
|
|
|
|
class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface
|
|
|
{
|
|
@@ -72,7 +72,7 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
|
|
|
new \Twig_SimpleFilter('fieldName', [$this, 'fieldNameFilter']),
|
|
|
new \Twig_SimpleFilter('ksort', [$this, 'ksortFilter']),
|
|
|
new \Twig_SimpleFilter('ltrim', [$this, 'ltrimFilter']),
|
|
|
- new \Twig_SimpleFilter('markdown', [$this, 'markdownFunction']),
|
|
|
+ new \Twig_SimpleFilter('markdown', [$this, 'markdownFunction'], ['is_safe' => ['html']]),
|
|
|
new \Twig_SimpleFilter('md5', [$this, 'md5Filter']),
|
|
|
new \Twig_SimpleFilter('base32_encode', [$this, 'base32EncodeFilter']),
|
|
|
new \Twig_SimpleFilter('base32_decode', [$this, 'base32DecodeFilter']),
|
|
@@ -88,9 +88,6 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
|
|
|
new \Twig_SimpleFilter('safe_truncate_html', ['\Grav\Common\Utils', 'safeTruncateHTML']),
|
|
|
new \Twig_SimpleFilter('sort_by_key', [$this, 'sortByKeyFilter']),
|
|
|
new \Twig_SimpleFilter('starts_with', [$this, 'startsWithFilter']),
|
|
|
- new \Twig_SimpleFilter('t', [$this, 'translate']),
|
|
|
- new \Twig_SimpleFilter('tl', [$this, 'translateLanguage']),
|
|
|
- new \Twig_SimpleFilter('ta', [$this, 'translateArray']),
|
|
|
new \Twig_SimpleFilter('truncate', ['\Grav\Common\Utils', 'truncate']),
|
|
|
new \Twig_SimpleFilter('truncate_html', ['\Grav\Common\Utils', 'truncateHTML']),
|
|
|
new \Twig_SimpleFilter('json_decode', [$this, 'jsonDecodeFilter']),
|
|
@@ -100,6 +97,18 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
|
|
|
new \Twig_SimpleFilter('print_r', 'print_r'),
|
|
|
new \Twig_SimpleFilter('yaml_encode', [$this, 'yamlEncodeFilter']),
|
|
|
new \Twig_SimpleFilter('yaml_decode', [$this, 'yamlDecodeFilter']),
|
|
|
+
|
|
|
+ // Translations
|
|
|
+ new \Twig_SimpleFilter('t', [$this, 'translate']),
|
|
|
+ new \Twig_SimpleFilter('tl', [$this, 'translateLanguage']),
|
|
|
+ new \Twig_SimpleFilter('ta', [$this, 'translateArray']),
|
|
|
+
|
|
|
+ // Casting values
|
|
|
+ new \Twig_SimpleFilter('string', [$this, 'stringFilter']),
|
|
|
+ new \Twig_SimpleFilter('int', [$this, 'intFilter'], ['is_safe' => true]),
|
|
|
+ new \Twig_SimpleFilter('bool', [$this, 'boolFilter']),
|
|
|
+ new \Twig_SimpleFilter('float', [$this, 'floatFilter'], ['is_safe' => true]),
|
|
|
+ new \Twig_SimpleFilter('array', [$this, 'arrayFilter']),
|
|
|
];
|
|
|
}
|
|
|
|
|
@@ -111,7 +120,7 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
|
|
|
public function getFunctions()
|
|
|
{
|
|
|
return [
|
|
|
- new \Twig_SimpleFunction('array', [$this, 'arrayFunc']),
|
|
|
+ new \Twig_SimpleFunction('array', [$this, 'arrayFilter']),
|
|
|
new \Twig_SimpleFunction('array_key_value', [$this, 'arrayKeyValueFunc']),
|
|
|
new \Twig_SimpleFunction('array_key_exists', 'array_key_exists'),
|
|
|
new \Twig_SimpleFunction('array_unique', 'array_unique'),
|
|
@@ -132,9 +141,6 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
|
|
|
new \Twig_SimpleFunction('regex_replace', [$this, 'regexReplace']),
|
|
|
new \Twig_SimpleFunction('regex_filter', [$this, 'regexFilter']),
|
|
|
new \Twig_SimpleFunction('string', [$this, 'stringFunc']),
|
|
|
- new \Twig_simpleFunction('t', [$this, 'translate']),
|
|
|
- new \Twig_simpleFunction('tl', [$this, 'translateLanguage']),
|
|
|
- new \Twig_simpleFunction('ta', [$this, 'translateArray']),
|
|
|
new \Twig_SimpleFunction('url', [$this, 'urlFunc']),
|
|
|
new \Twig_SimpleFunction('json_decode', [$this, 'jsonDecodeFilter']),
|
|
|
new \Twig_SimpleFunction('get_cookie', [$this, 'getCookie']),
|
|
@@ -151,6 +157,10 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
|
|
|
new \Twig_SimpleFunction('nicefilesize', [$this, 'niceFilesizeFunc']),
|
|
|
new \Twig_SimpleFunction('nicetime', [$this, 'nicetimeFilter']),
|
|
|
|
|
|
+ // Translations
|
|
|
+ new \Twig_simpleFunction('t', [$this, 'translate']),
|
|
|
+ new \Twig_simpleFunction('tl', [$this, 'translateLanguage']),
|
|
|
+ new \Twig_simpleFunction('ta', [$this, 'translateArray']),
|
|
|
];
|
|
|
}
|
|
|
|
|
@@ -617,6 +627,62 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
|
|
|
return ltrim($value, $chars);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Casts input to string.
|
|
|
+ *
|
|
|
+ * @param mixed $input
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public function stringFilter($input)
|
|
|
+ {
|
|
|
+ return (string) $input;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Casts input to int.
|
|
|
+ *
|
|
|
+ * @param mixed $input
|
|
|
+ * @return int
|
|
|
+ */
|
|
|
+ public function intFilter($input)
|
|
|
+ {
|
|
|
+ return (int) $input;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Casts input to bool.
|
|
|
+ *
|
|
|
+ * @param mixed $input
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function boolFilter($input)
|
|
|
+ {
|
|
|
+ return (bool) $input;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Casts input to float.
|
|
|
+ *
|
|
|
+ * @param mixed $input
|
|
|
+ * @return float
|
|
|
+ */
|
|
|
+ public function floatFilter($input)
|
|
|
+ {
|
|
|
+ return (float) $input;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Casts input to array.
|
|
|
+ *
|
|
|
+ * @param mixed $input
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function arrayFilter($input)
|
|
|
+ {
|
|
|
+ return (array) $input;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* @return mixed
|
|
|
*/
|
|
@@ -693,7 +759,6 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
|
|
|
|
|
|
$template = $env->createTemplate($twig);
|
|
|
return $template->render($context);
|
|
|
-;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -748,7 +813,7 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
|
|
|
* Output a Gist
|
|
|
*
|
|
|
* @param string $id
|
|
|
- * @param string $file
|
|
|
+ * @param string|bool $file
|
|
|
*
|
|
|
* @return string
|
|
|
*/
|
|
@@ -788,19 +853,6 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
|
|
|
return str_pad($input, (int)$pad_length, $pad_string, $pad_type);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- /**
|
|
|
- * Cast a value to array
|
|
|
- *
|
|
|
- * @param $value
|
|
|
- *
|
|
|
- * @return array
|
|
|
- */
|
|
|
- public function arrayFunc($value)
|
|
|
- {
|
|
|
- return (array)$value;
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* Workaround for twig associative array initialization
|
|
|
* Returns a key => val array
|
|
@@ -976,7 +1028,7 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
|
|
|
public function redirectFunc($url, $statusCode = 303)
|
|
|
{
|
|
|
header('Location: ' . $url, true, $statusCode);
|
|
|
- die();
|
|
|
+ exit();
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -1060,7 +1112,7 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
|
|
|
|
|
|
if (file_exists($filepath)) {
|
|
|
return file_get_contents($filepath);
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
return false;
|
|
|
}
|
|
@@ -1245,11 +1297,12 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
|
|
|
* Dump/Encode data into YAML format
|
|
|
*
|
|
|
* @param $data
|
|
|
+ * @param $inline integer number of levels of inline syntax
|
|
|
* @return mixed
|
|
|
*/
|
|
|
- public function yamlEncodeFilter($data)
|
|
|
+ public function yamlEncodeFilter($data, $inline = 10)
|
|
|
{
|
|
|
- return Yaml::dump($data, 10);
|
|
|
+ return Yaml::dump($data, $inline);
|
|
|
}
|
|
|
|
|
|
/**
|