/** * A utility method for parsing token keyword arguments. * * @param array $bits A list containing remainder of the token (split by spaces) * that is to be checked for arguments. Valid arguments will be removed * from this list. * @param Parser $parser * @param bool $support_legacy If set to true ``True``, the legacy format * ``1 as foo`` will be accepted. Otherwise, only the standard ``foo=1`` * format is allowed. * * @return array A dictionary of the arguments retrieved from the ``bits`` token * list. * * There is no requirement for all remaining token ``bits`` to be keyword * arguments, so the dictionary will be returned as soon as an invalid * argument format is reached. */ public static function tokenKwargs(&$bits, $parser, $support_legacy = False) { if (!$bits) { return array(); } $match = py_re_match(DjaBase::$re_kwarg, $bits[0]); $kwarg_format = $match && $match->group(1); if (!$kwarg_format) { if (!$support_legacy) { return array(); } if (count($bits) < 3 || $bits[1] != 'as') { return array(); } } $kwargs = array(); while ($bits) { if ($kwarg_format) { $match = py_re_match(DjaBase::$re_kwarg, $bits[0]); if (!$match || !$match->group(1)) { return $kwargs; } list($key, $value) = $match->groups(); $bits = py_slice($bits, 1); } else { if (count($bits) < 3 || $bits[1] != 'as') { return $kwargs; } $key = $bits[2]; $value = $bits[0]; $bits = py_slice($bits, 3); } $kwargs[$key] = $parser->compileFilter($value); if ($bits && !$kwarg_format) { if ($bits[0] != 'and') { return $kwargs; } $bits = py_slice($bits, 1); } } return $kwargs; }
*/ $bits = $token->splitContents(); if (count($bits) < 2) { throw new TemplateSyntaxError('\'url\' takes at least one argument (path to a view)'); } $viewname = $parser->compileFilter($bits[1]); $args = array(); $kwargs = array(); $asvar = null; $bits = py_slice($bits, 2); if (count($bits) >= 2 && py_arr_get($bits, -2) == 'as') { $asvar = py_arr_get($bits, -1); $bits = py_slice($bits, null, -2); } if (count($bits)) { foreach ($bits as $bit) { $match = py_re_match(DjaBase::$re_kwarg, $bit); if (!$match) { throw new TemplateSyntaxError('Malformed arguments to url tag'); } list($name, $value) = $match->groups(); if ($name) { $kwargs[$name] = $parser->compileFilter($value); } else { $args[] = $parser->compileFilter($value); } } } return new URLNode($viewname, $args, $kwargs, $asvar, False); }); return $lib;