Changeset 1101:7273894e61b8 for inc/libs/twig/Extension/Core.php
- Timestamp:
- 02/15/13 08:35:19 (13 years ago)
- Branch:
- twig
- Children:
- 1106:a4487f3ca4b4, 1147:2e5cb79e4782
- File:
-
- 1 edited
-
inc/libs/twig/Extension/Core.php (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
-
inc/libs/twig/Extension/Core.php
r991 r1101 127 127 $filters = array( 128 128 // formatting filters 129 'date' => new Twig_Filter_Function('twig_date_format_filter', array('needs_environment' => true)),130 'date_modify' => new Twig_Filter_Function('twig_date_modify_filter', array('needs_environment' => true)),131 'format' => new Twig_Filter_Function('sprintf'),132 'replace' => new Twig_Filter_Function('strtr'),133 'number_format' => new Twig_Filter_Function('twig_number_format_filter', array('needs_environment' => true)),134 'abs' => new Twig_Filter_Function('abs'),129 new Twig_SimpleFilter('date', 'twig_date_format_filter', array('needs_environment' => true)), 130 new Twig_SimpleFilter('date_modify', 'twig_date_modify_filter', array('needs_environment' => true)), 131 new Twig_SimpleFilter('format', 'sprintf'), 132 new Twig_SimpleFilter('replace', 'strtr'), 133 new Twig_SimpleFilter('number_format', 'twig_number_format_filter', array('needs_environment' => true)), 134 new Twig_SimpleFilter('abs', 'abs'), 135 135 136 136 // encoding 137 'url_encode' => new Twig_Filter_Function('twig_urlencode_filter'),138 'json_encode' => new Twig_Filter_Function('twig_jsonencode_filter'),139 'convert_encoding' => new Twig_Filter_Function('twig_convert_encoding'),137 new Twig_SimpleFilter('url_encode', 'twig_urlencode_filter'), 138 new Twig_SimpleFilter('json_encode', 'twig_jsonencode_filter'), 139 new Twig_SimpleFilter('convert_encoding', 'twig_convert_encoding'), 140 140 141 141 // string filters 142 'title' => new Twig_Filter_Function('twig_title_string_filter', array('needs_environment' => true)),143 'capitalize' => new Twig_Filter_Function('twig_capitalize_string_filter', array('needs_environment' => true)),144 'upper' => new Twig_Filter_Function('strtoupper'),145 'lower' => new Twig_Filter_Function('strtolower'),146 'striptags' => new Twig_Filter_Function('strip_tags'),147 'trim' => new Twig_Filter_Function('trim'),148 'nl2br' => new Twig_Filter_Function('nl2br', array('pre_escape' => 'html', 'is_safe' => array('html'))),142 new Twig_SimpleFilter('title', 'twig_title_string_filter', array('needs_environment' => true)), 143 new Twig_SimpleFilter('capitalize', 'twig_capitalize_string_filter', array('needs_environment' => true)), 144 new Twig_SimpleFilter('upper', 'strtoupper'), 145 new Twig_SimpleFilter('lower', 'strtolower'), 146 new Twig_SimpleFilter('striptags', 'strip_tags'), 147 new Twig_SimpleFilter('trim', 'trim'), 148 new Twig_SimpleFilter('nl2br', 'nl2br', array('pre_escape' => 'html', 'is_safe' => array('html'))), 149 149 150 150 // array helpers 151 'join' => new Twig_Filter_Function('twig_join_filter'),152 'split' => new Twig_Filter_Function('twig_split_filter'),153 'sort' => new Twig_Filter_Function('twig_sort_filter'),154 'merge' => new Twig_Filter_Function('twig_array_merge'),151 new Twig_SimpleFilter('join', 'twig_join_filter'), 152 new Twig_SimpleFilter('split', 'twig_split_filter'), 153 new Twig_SimpleFilter('sort', 'twig_sort_filter'), 154 new Twig_SimpleFilter('merge', 'twig_array_merge'), 155 155 156 156 // string/array filters 157 'reverse' => new Twig_Filter_Function('twig_reverse_filter', array('needs_environment' => true)), 158 'length' => new Twig_Filter_Function('twig_length_filter', array('needs_environment' => true)), 159 'slice' => new Twig_Filter_Function('twig_slice', array('needs_environment' => true)), 157 new Twig_SimpleFilter('reverse', 'twig_reverse_filter', array('needs_environment' => true)), 158 new Twig_SimpleFilter('length', 'twig_length_filter', array('needs_environment' => true)), 159 new Twig_SimpleFilter('slice', 'twig_slice', array('needs_environment' => true)), 160 new Twig_SimpleFilter('first', 'twig_first', array('needs_environment' => true)), 161 new Twig_SimpleFilter('last', 'twig_last', array('needs_environment' => true)), 160 162 161 163 // iteration and runtime 162 'default' => new Twig_Filter_Node('Twig_Node_Expression_Filter_Default'), 163 '_default' => new Twig_Filter_Function('_twig_default_filter'), 164 165 'keys' => new Twig_Filter_Function('twig_get_array_keys_filter'), 164 new Twig_SimpleFilter('default', '_twig_default_filter', array('node_class' => 'Twig_Node_Expression_Filter_Default')), 165 new Twig_SimpleFilter('keys', 'twig_get_array_keys_filter'), 166 166 167 167 // escaping 168 'escape' => new Twig_Filter_Function('twig_escape_filter', array('needs_environment' => true, 'is_safe_callback' => 'twig_escape_filter_is_safe')),169 'e' => new Twig_Filter_Function('twig_escape_filter', array('needs_environment' => true, 'is_safe_callback' => 'twig_escape_filter_is_safe')),168 new Twig_SimpleFilter('escape', 'twig_escape_filter', array('needs_environment' => true, 'is_safe_callback' => 'twig_escape_filter_is_safe')), 169 new Twig_SimpleFilter('e', 'twig_escape_filter', array('needs_environment' => true, 'is_safe_callback' => 'twig_escape_filter_is_safe')), 170 170 ); 171 171 … … 186 186 { 187 187 return array( 188 'range' => new Twig_Function_Function('range'), 189 'constant' => new Twig_Function_Function('constant'), 190 'cycle' => new Twig_Function_Function('twig_cycle'), 191 'random' => new Twig_Function_Function('twig_random', array('needs_environment' => true)), 192 'date' => new Twig_Function_Function('twig_date_converter', array('needs_environment' => true)), 188 new Twig_SimpleFunction('range', 'range'), 189 new Twig_SimpleFunction('constant', 'twig_constant'), 190 new Twig_SimpleFunction('cycle', 'twig_cycle'), 191 new Twig_SimpleFunction('random', 'twig_random', array('needs_environment' => true)), 192 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)), 193 194 ); 194 195 } … … 202 203 { 203 204 return array( 204 'even' => new Twig_Test_Node('Twig_Node_Expression_Test_Even'),205 'odd' => new Twig_Test_Node('Twig_Node_Expression_Test_Odd'),206 'defined' => new Twig_Test_Node('Twig_Node_Expression_Test_Defined'),207 'sameas' => new Twig_Test_Node('Twig_Node_Expression_Test_Sameas'),208 'none' => new Twig_Test_Node('Twig_Node_Expression_Test_Null'),209 'null' => new Twig_Test_Node('Twig_Node_Expression_Test_Null'),210 'divisibleby' => new Twig_Test_Node('Twig_Node_Expression_Test_Divisibleby'),211 'constant' => new Twig_Test_Node('Twig_Node_Expression_Test_Constant'),212 'empty' => new Twig_Test_Function('twig_test_empty'),213 'iterable' => new Twig_Test_Function('twig_test_iterable'),205 new Twig_SimpleTest('even', null, array('node_class' => 'Twig_Node_Expression_Test_Even')), 206 new Twig_SimpleTest('odd', null, array('node_class' => 'Twig_Node_Expression_Test_Odd')), 207 new Twig_SimpleTest('defined', null, array('node_class' => 'Twig_Node_Expression_Test_Defined')), 208 new Twig_SimpleTest('sameas', null, array('node_class' => 'Twig_Node_Expression_Test_Sameas')), 209 new Twig_SimpleTest('none', null, array('node_class' => 'Twig_Node_Expression_Test_Null')), 210 new Twig_SimpleTest('null', null, array('node_class' => 'Twig_Node_Expression_Test_Null')), 211 new Twig_SimpleTest('divisibleby', null, array('node_class' => 'Twig_Node_Expression_Test_Divisibleby')), 212 new Twig_SimpleTest('constant', null, array('node_class' => 'Twig_Node_Expression_Test_Constant')), 213 new Twig_SimpleTest('empty', 'twig_test_empty'), 214 new Twig_SimpleTest('iterable', 'twig_test_iterable'), 214 215 ); 215 216 } … … 268 269 $arguments = null; 269 270 if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '(')) { 270 $arguments = $parser->getExpressionParser()->parseArguments( );271 } 272 273 $class = $this->getTestNodeClass($parser ->getEnvironment(), $name);271 $arguments = $parser->getExpressionParser()->parseArguments(true); 272 } 273 274 $class = $this->getTestNodeClass($parser, $name, $node->getLine()); 274 275 275 276 return new $class($node, $name, $arguments, $parser->getCurrentToken()->getLine()); 276 277 } 277 278 278 protected function getTestNodeClass(Twig_Environment $env, $name) 279 { 279 protected function getTestNodeClass(Twig_Parser $parser, $name, $line) 280 { 281 $env = $parser->getEnvironment(); 280 282 $testMap = $env->getTests(); 281 if (isset($testMap[$name]) && $testMap[$name] instanceof Twig_Test_Node) { 282 return $testMap[$name]->getClass(); 283 } 284 285 return 'Twig_Node_Expression_Test'; 283 if (!isset($testMap[$name])) { 284 $message = sprintf('The test "%s" does not exist', $name); 285 if ($alternatives = $env->computeAlternatives($name, array_keys($env->getTests()))) { 286 $message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives)); 287 } 288 289 throw new Twig_Error_Syntax($message, $line, $parser->getFilename()); 290 } 291 292 if ($testMap[$name] instanceof Twig_SimpleTest) { 293 return $testMap[$name]->getNodeClass(); 294 } 295 296 return $testMap[$name] instanceof Twig_Test_Node ? $testMap[$name]->getClass() : 'Twig_Node_Expression_Test'; 286 297 } 287 298 … … 300 311 * Cycles over a value. 301 312 * 302 * @param ArrayAccess|array $values An array or an ArrayAccess instance303 * @param integer $ i The cycle value313 * @param ArrayAccess|array $values An array or an ArrayAccess instance 314 * @param integer $position The cycle position 304 315 * 305 316 * @return string The next value in the cycle 306 317 */ 307 function twig_cycle($values, $ i)318 function twig_cycle($values, $position) 308 319 { 309 320 if (!is_array($values) && !$values instanceof ArrayAccess) { … … 311 322 } 312 323 313 return $values[$ i% count($values)];324 return $values[$position % count($values)]; 314 325 } 315 326 … … 405 416 * 406 417 * <pre> 407 * {{ post.published_at| modify("-1day")|date("m/d/Y") }}418 * {{ post.published_at|date_modify("-1day")|date("m/d/Y") }} 408 419 * </pre> 409 420 * … … 459 470 $asString = (string) $date; 460 471 if (ctype_digit($asString) || (!empty($asString) && '-' === $asString[0] && ctype_digit(substr($asString, 1)))) { 461 $date = new DateTime('@'.$date); 472 $date = '@'.$date; 473 } 474 475 $date = new DateTime($date, $defaultTimezone); 476 if (false !== $timezone) { 462 477 $date->setTimezone($defaultTimezone); 463 464 return $date; 465 } 466 467 return new DateTime($date, $defaultTimezone); 478 } 479 480 return $date; 468 481 } 469 482 … … 621 634 622 635 /** 636 * Returns the first element of the item. 637 * 638 * @param Twig_Environment $env A Twig_Environment instance 639 * @param mixed $item A variable 640 * 641 * @return mixed The first element of the item 642 */ 643 function twig_first(Twig_Environment $env, $item) 644 { 645 $elements = twig_slice($env, $item, 0, 1, false); 646 647 return is_string($elements) ? $elements[0] : current($elements); 648 } 649 650 /** 651 * Returns the last element of the item. 652 * 653 * @param Twig_Environment $env A Twig_Environment instance 654 * @param mixed $item A variable 655 * 656 * @return mixed The last element of the item 657 */ 658 function twig_last(Twig_Environment $env, $item) 659 { 660 $elements = twig_slice($env, $item, -1, 1, false); 661 662 return is_string($elements) ? $elements[0] : current($elements); 663 } 664 665 /** 623 666 * Joins the values to a string. 624 667 * … … 774 817 function twig_in_filter($value, $compare) 775 818 { 776 $strict = is_object($value);777 778 819 if (is_array($compare)) { 779 return in_array($value, $compare, $strict);820 return in_array($value, $compare, is_object($value)); 780 821 } elseif (is_string($compare)) { 781 if (!strlen( (string)$value)) {822 if (!strlen($value)) { 782 823 return empty($compare); 783 824 } 784 825 785 826 return false !== strpos($compare, (string) $value); 786 } elseif ( is_object($compare) &&$compare instanceof Traversable) {787 return in_array($value, iterator_to_array($compare, false), $strict);827 } elseif ($compare instanceof Traversable) { 828 return in_array($value, iterator_to_array($compare, false), is_object($value)); 788 829 } 789 830 … … 1192 1233 } 1193 1234 1194 return false === $value || (empty($value) && '0' != $value);1235 return '' === $value || false === $value || null === $value || array() === $value; 1195 1236 } 1196 1237 … … 1213 1254 return $value instanceof Traversable || is_array($value); 1214 1255 } 1256 1257 /** 1258 * Renders a template. 1259 * 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 1265 * 1266 * @return string The rendered template 1267 */ 1268 function twig_include(Twig_Environment $env, $context, $template, $variables = array(), $withContext = true, $ignoreMissing = false, $sandboxed = false) 1269 { 1270 if ($withContext) { 1271 $variables = array_merge($context, $variables); 1272 } 1273 1274 if ($isSandboxed = $sandboxed && $env->hasExtension('sandbox')) { 1275 $sandbox = $env->getExtension('sandbox'); 1276 if (!$alreadySandboxed = $sandbox->isSandboxed()) { 1277 $sandbox->enableSandbox(); 1278 } 1279 } 1280 1281 try { 1282 return $env->resolveTemplate($template)->display($variables); 1283 } catch (Twig_Error_Loader $e) { 1284 if (!$ignoreMissing) { 1285 throw $e; 1286 } 1287 } 1288 1289 if ($isSandboxed && !$alreadySandboxed) { 1290 $sandbox->disableSandbox(); 1291 } 1292 } 1293 1294 /** 1295 * Provides the ability to get constants from instances as well as class/global constants. 1296 * 1297 * @param string $constant The name of the constant 1298 * @param null|object $object The object to get the constant from 1299 * 1300 * @return string 1301 */ 1302 function twig_constant($constant, $object = null) 1303 { 1304 if (null !== $object) { 1305 $constant = get_class($object).'::'.$constant; 1306 } 1307 1308 return constant($constant); 1309 }
Note: See TracChangeset
for help on using the changeset viewer.
