Revision 1938
Added by darkviper almost 11 years ago
Core.php | ||
---|---|---|
152 | 152 |
new Twig_SimpleFilter('split', 'twig_split_filter'), |
153 | 153 |
new Twig_SimpleFilter('sort', 'twig_sort_filter'), |
154 | 154 |
new Twig_SimpleFilter('merge', 'twig_array_merge'), |
155 |
new Twig_SimpleFilter('batch', 'twig_array_batch'), |
|
155 | 156 |
|
156 | 157 |
// string/array filters |
157 | 158 |
new Twig_SimpleFilter('reverse', 'twig_reverse_filter', array('needs_environment' => true)), |
... | ... | |
170 | 171 |
); |
171 | 172 |
|
172 | 173 |
if (function_exists('mb_get_info')) { |
173 |
$filters['upper'] = new Twig_Filter_Function('twig_upper_filter', array('needs_environment' => true));
|
|
174 |
$filters['lower'] = new Twig_Filter_Function('twig_lower_filter', array('needs_environment' => true));
|
|
174 |
$filters[] = new Twig_SimpleFilter('upper', 'twig_upper_filter', array('needs_environment' => true));
|
|
175 |
$filters[] = new Twig_SimpleFilter('lower', 'twig_lower_filter', array('needs_environment' => true));
|
|
175 | 176 |
} |
176 | 177 |
|
177 | 178 |
return $filters; |
... | ... | |
190 | 191 |
new Twig_SimpleFunction('cycle', 'twig_cycle'), |
191 | 192 |
new Twig_SimpleFunction('random', 'twig_random', array('needs_environment' => true)), |
192 | 193 |
new Twig_SimpleFunction('date', 'twig_date_converter', array('needs_environment' => true)), |
193 |
new Twig_SimpleFunction('include', 'twig_include', array('needs_environment' => true, 'needs_context' => true)), |
|
194 |
new Twig_SimpleFunction('include', 'twig_include', array('needs_environment' => true, 'needs_context' => true, 'is_safe' => array('all'))),
|
|
194 | 195 |
); |
195 | 196 |
} |
196 | 197 |
|
... | ... | |
514 | 515 |
} |
515 | 516 |
|
516 | 517 |
/** |
517 |
* URL encodes a string. |
|
518 |
* URL encodes a string as a path segment or an array as a query string.
|
|
518 | 519 |
* |
519 |
* @param string $url A URL
|
|
520 |
* @param bool $raw true to use rawurlencode() instead of urlencode |
|
520 |
* @param string|array $url A URL or an array of query parameters
|
|
521 |
* @param bool $raw true to use rawurlencode() instead of urlencode
|
|
521 | 522 |
* |
522 | 523 |
* @return string The URL encoded value |
523 | 524 |
*/ |
524 | 525 |
function twig_urlencode_filter($url, $raw = false) |
525 | 526 |
{ |
527 |
if (is_array($url)) { |
|
528 |
return http_build_query($url, '', '&'); |
|
529 |
} |
|
530 |
|
|
526 | 531 |
if ($raw) { |
527 | 532 |
return rawurlencode($url); |
528 | 533 |
} |
... | ... | |
842 | 847 |
*/ |
843 | 848 |
function twig_escape_filter(Twig_Environment $env, $string, $strategy = 'html', $charset = null, $autoescape = false) |
844 | 849 |
{ |
845 |
if ($autoescape && is_object($string) && $string instanceof Twig_Markup) {
|
|
850 |
if ($autoescape && $string instanceof Twig_Markup) { |
|
846 | 851 |
return $string; |
847 | 852 |
} |
848 | 853 |
|
849 |
if (!is_string($string) && !(is_object($string) && method_exists($string, '__toString'))) { |
|
850 |
return $string; |
|
854 |
if (!is_string($string)) { |
|
855 |
if (is_object($string) && method_exists($string, '__toString')) { |
|
856 |
$string = (string) $string; |
|
857 |
} else { |
|
858 |
return $string; |
|
859 |
} |
|
851 | 860 |
} |
852 | 861 |
|
853 | 862 |
if (null === $charset) { |
854 | 863 |
$charset = $env->getCharset(); |
855 | 864 |
} |
856 | 865 |
|
857 |
$string = (string) $string; |
|
866 |
switch ($strategy) { |
|
867 |
case 'html': |
|
868 |
// see http://php.net/htmlspecialchars |
|
858 | 869 |
|
859 |
switch ($strategy) { |
|
870 |
// Using a static variable to avoid initializing the array |
|
871 |
// each time the function is called. Moving the declaration on the |
|
872 |
// top of the function slow downs other escaping strategies. |
|
873 |
static $htmlspecialcharsCharsets = array( |
|
874 |
'ISO-8859-1' => true, 'ISO8859-1' => true, |
|
875 |
'ISO-8859-15' => true, 'ISO8859-15' => true, |
|
876 |
'utf-8' => true, 'UTF-8' => true, |
|
877 |
'CP866' => true, 'IBM866' => true, '866' => true, |
|
878 |
'CP1251' => true, 'WINDOWS-1251' => true, 'WIN-1251' => true, |
|
879 |
'1251' => true, |
|
880 |
'CP1252' => true, 'WINDOWS-1252' => true, '1252' => true, |
|
881 |
'KOI8-R' => true, 'KOI8-RU' => true, 'KOI8R' => true, |
|
882 |
'BIG5' => true, '950' => true, |
|
883 |
'GB2312' => true, '936' => true, |
|
884 |
'BIG5-HKSCS' => true, |
|
885 |
'SHIFT_JIS' => true, 'SJIS' => true, '932' => true, |
|
886 |
'EUC-JP' => true, 'EUCJP' => true, |
|
887 |
'ISO8859-5' => true, 'ISO-8859-5' => true, 'MACROMAN' => true, |
|
888 |
); |
|
889 |
|
|
890 |
if (isset($htmlspecialcharsCharsets[$charset])) { |
|
891 |
return htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, $charset); |
|
892 |
} |
|
893 |
|
|
894 |
if (isset($htmlspecialcharsCharsets[strtoupper($charset)])) { |
|
895 |
// cache the lowercase variant for future iterations |
|
896 |
$htmlspecialcharsCharsets[$charset] = true; |
|
897 |
|
|
898 |
return htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, $charset); |
|
899 |
} |
|
900 |
|
|
901 |
$string = twig_convert_encoding($string, 'UTF-8', $charset); |
|
902 |
$string = htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); |
|
903 |
|
|
904 |
return twig_convert_encoding($string, $charset, 'UTF-8'); |
|
905 |
|
|
860 | 906 |
case 'js': |
861 | 907 |
// escape all non-alphanumeric characters |
862 | 908 |
// into their \xHH or \uHHHH representations |
... | ... | |
910 | 956 |
|
911 | 957 |
return $string; |
912 | 958 |
|
913 |
case 'html': |
|
914 |
// see http://php.net/htmlspecialchars |
|
915 |
|
|
916 |
// Using a static variable to avoid initializing the array |
|
917 |
// each time the function is called. Moving the declaration on the |
|
918 |
// top of the function slow downs other escaping strategies. |
|
919 |
static $htmlspecialcharsCharsets = array( |
|
920 |
'iso-8859-1' => true, 'iso8859-1' => true, |
|
921 |
'iso-8859-15' => true, 'iso8859-15' => true, |
|
922 |
'utf-8' => true, |
|
923 |
'cp866' => true, 'ibm866' => true, '866' => true, |
|
924 |
'cp1251' => true, 'windows-1251' => true, 'win-1251' => true, |
|
925 |
'1251' => true, |
|
926 |
'cp1252' => true, 'windows-1252' => true, '1252' => true, |
|
927 |
'koi8-r' => true, 'koi8-ru' => true, 'koi8r' => true, |
|
928 |
'big5' => true, '950' => true, |
|
929 |
'gb2312' => true, '936' => true, |
|
930 |
'big5-hkscs' => true, |
|
931 |
'shift_jis' => true, 'sjis' => true, '932' => true, |
|
932 |
'euc-jp' => true, 'eucjp' => true, |
|
933 |
'iso8859-5' => true, 'iso-8859-5' => true, 'macroman' => true, |
|
934 |
); |
|
935 |
|
|
936 |
if (isset($htmlspecialcharsCharsets[strtolower($charset)])) { |
|
937 |
return htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, $charset); |
|
938 |
} |
|
939 |
|
|
940 |
$string = twig_convert_encoding($string, 'UTF-8', $charset); |
|
941 |
$string = htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); |
|
942 |
|
|
943 |
return twig_convert_encoding($string, $charset, 'UTF-8'); |
|
944 |
|
|
945 | 959 |
case 'url': |
946 |
if (version_compare(PHP_VERSION, '5.3.0', '<')) { |
|
960 |
// hackish test to avoid version_compare that is much slower, this works unless PHP releases a 5.10.* |
|
961 |
// at that point however PHP 5.2.* support can be removed |
|
962 |
if (PHP_VERSION < '5.3.0') { |
|
947 | 963 |
return str_replace('%7E', '~', rawurlencode($string)); |
948 | 964 |
} |
949 | 965 |
|
... | ... | |
1257 | 1273 |
/** |
1258 | 1274 |
* Renders a template. |
1259 | 1275 |
* |
1260 |
* @param string template The template to render |
|
1261 |
* @param array variables The variables to pass to the template |
|
1262 |
* @param Boolean with_context Whether to pass the current context variables or not |
|
1263 |
* @param Boolean ignore_missing Whether to ignore missing templates or not |
|
1264 |
* @param Boolean sandboxed Whether to sandbox the template or not |
|
1276 |
* @param string $template The template to render
|
|
1277 |
* @param array $variables The variables to pass to the template
|
|
1278 |
* @param Boolean $with_context Whether to pass the current context variables or not
|
|
1279 |
* @param Boolean $ignore_missing Whether to ignore missing templates or not
|
|
1280 |
* @param Boolean $sandboxed Whether to sandbox the template or not
|
|
1265 | 1281 |
* |
1266 | 1282 |
* @return string The rendered template |
1267 | 1283 |
*/ |
... | ... | |
1279 | 1295 |
} |
1280 | 1296 |
|
1281 | 1297 |
try { |
1282 |
return $env->resolveTemplate($template)->display($variables);
|
|
1298 |
return $env->resolveTemplate($template)->render($variables);
|
|
1283 | 1299 |
} catch (Twig_Error_Loader $e) { |
1284 | 1300 |
if (!$ignoreMissing) { |
1285 | 1301 |
throw $e; |
... | ... | |
1307 | 1323 |
|
1308 | 1324 |
return constant($constant); |
1309 | 1325 |
} |
1326 |
|
|
1327 |
/** |
|
1328 |
* Batches item. |
|
1329 |
* |
|
1330 |
* @param array $items An array of items |
|
1331 |
* @param integer $size The size of the batch |
|
1332 |
* @param string $fill A string to fill missing items |
|
1333 |
* |
|
1334 |
* @return array |
|
1335 |
*/ |
|
1336 |
function twig_array_batch($items, $size, $fill = null) |
|
1337 |
{ |
|
1338 |
if ($items instanceof Traversable) { |
|
1339 |
$items = iterator_to_array($items, false); |
|
1340 |
} |
|
1341 |
|
|
1342 |
$size = ceil($size); |
|
1343 |
|
|
1344 |
$result = array_chunk($items, $size, true); |
|
1345 |
|
|
1346 |
if (null !== $fill) { |
|
1347 |
$last = count($result) - 1; |
|
1348 |
$result[$last] = array_merge( |
|
1349 |
$result[$last], |
|
1350 |
array_fill(0, $size - count($result[$last]), $fill) |
|
1351 |
); |
|
1352 |
} |
|
1353 |
|
|
1354 |
return $result; |
|
1355 |
} |
Also available in: Unified diff
update Twig template engine to version 1.13.1