From: Kevin Date: Sun, 10 May 2020 20:24:12 +0000 (-0400) Subject: Allow variable identifiers to be aliased in block params X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=70227394b02084f23114db47c509c7cb074705ad;p=thirdparty%2Fjinja.git Allow variable identifiers to be aliased in block params This is only really supported on the `{% from %}` block currently, but the ability exists to use this elsewhere if someone is looking for the ability to alias variable identifiers. This also allows value-only parameters to be comma-separated within the block parameters, since before that was only allows for key-value parameters. --- diff --git a/grammar.ebnf b/grammar.ebnf index 93e94172..f04cede1 100644 --- a/grammar.ebnf +++ b/grammar.ebnf @@ -98,7 +98,13 @@ implicit_identifier_tuple block_parameters = - @+:block_parameter { {SP}* @+:block_parameter }* + @+:block_parameter + [ {SP}* "," ] + { + {SP}* + @+:block_parameter + [ {SP}* "," ] + }* ; block_parameter @@ -110,7 +116,6 @@ block_parameter block_parameter_key_value = key:block_parameter_key {SP}* "=" {SP}* value:variable_accessor_call_parameter_value - [ {SP}* "," ] ; block_parameter_key @@ -122,6 +127,7 @@ block_parameter_key block_parameter_value_only = | value:implicit_identifier_tuple + | value:variable_identifier_with_alias | value:variable_accessor_call_parameter_value ; @@ -145,6 +151,13 @@ variable_identifier { {SP}* filters+:variable_filter }* ; +variable_identifier_with_alias + = + variable:IDENTIFIER + {SP}* "as" {SP}* + alias:IDENTIFIER + ; + variable_accessor = | variable_accessor_brackets diff --git a/new_parser.py b/new_parser.py index b42a7f00..fbaac6eb 100644 --- a/new_parser.py +++ b/new_parser.py @@ -105,7 +105,16 @@ def parse_block_from(ast): with_context = False if len(parameters) > 2: - names = []#parameters[2:] + for parameter in parameters[2:]: + if 'alias' in parameter['value']: + names.append( + ( + parameter['value']['variable'], + parameter['value']['alias'] + ) + ) + else: + names.append(parameter['value']['variable']) from_import = nodes.FromImport( template,