@@ -142,42 +142,47 @@ def validate(cls, text):
142142
143143 @classmethod
144144 def _evaluate_and_expand (cls , text , data = None ):
145- output = str_util .unicode (text )
146145 exprs = cls ._regex_parser .findall (text )
147146 block_exprs = cls ._regex_block_parser .findall (text )
148147 ctx = cls .contextualize (data )
149148 opts = {'undefined_to_none' : False }
150149
151150 try :
152- # Evaluate inline jinja expressions first.
153- for expr in exprs :
154- stripped = cls .strip_delimiter (expr )
155- compiled = cls ._jinja_env .compile_expression (stripped , ** opts )
156- result = compiled (** ctx )
157-
158- if inspect .isgenerator (result ):
159- result = list (result )
160-
161- if isinstance (result , six .string_types ):
162- result = cls ._evaluate_and_expand (result , data )
163-
164- # For StrictUndefined values, UndefinedError only gets raised when the value is
165- # accessed, not when it gets created. The simplest way to access it is to try
166- # and cast it to string. When StrictUndefined is cast to str below, this will
167- # raise an exception with error description.
168- if not isinstance (result , jinja2 .runtime .StrictUndefined ):
169- if len (exprs ) > 1 or block_exprs or len (output ) > len (expr ):
170- output = output .replace (expr , str_util .unicode (result , force = True ))
171- else :
172- output = str_util .unicode (result )
173-
174- # Evaluate jinja block(s) after inline expressions are evaluated.
175- if block_exprs and isinstance (output , six .string_types ):
176- output = cls ._jinja_env .from_string (output ).render (ctx )
151+ # If there is a Jinja block expression in the text, then process the whole text.
152+ if block_exprs :
153+ expr = text
154+ output = cls ._jinja_env .from_string (expr ).render (ctx )
155+ output = str_util .unicode (output )
177156
178157 # Traverse and evaulate again in case additional inline epxressions are
179158 # introduced after the jinja block is evaluated.
180159 output = cls ._evaluate_and_expand (output , data )
160+ else :
161+ # The output will first be the original text and the expressions
162+ # will be substituted by the evaluated value.
163+ output = str_util .unicode (text )
164+
165+ # Evaluate inline jinja expressions first.
166+ for expr in exprs :
167+ stripped = cls .strip_delimiter (expr )
168+ compiled = cls ._jinja_env .compile_expression (stripped , ** opts )
169+ result = compiled (** ctx )
170+
171+ if inspect .isgenerator (result ):
172+ result = list (result )
173+
174+ if isinstance (result , six .string_types ):
175+ result = cls ._evaluate_and_expand (result , data )
176+
177+ # For StrictUndefined values, UndefinedError only gets raised when the value is
178+ # accessed, not when it gets created. The simplest way to access it is to try
179+ # and cast it to string. When StrictUndefined is cast to str below, this will
180+ # raise an exception with error description.
181+ if not isinstance (result , jinja2 .runtime .StrictUndefined ):
182+ if len (exprs ) > 1 or block_exprs or len (output ) > len (expr ):
183+ output = output .replace (expr , str_util .unicode (result , force = True ))
184+ else :
185+ output = str_util .unicode (result )
181186
182187 except jinja2 .exceptions .UndefinedError as e :
183188 msg = "Unable to evaluate expression '%s'. %s: %s"
0 commit comments