purcell / airspeed

A lightweight Python template engine compatible with Velocity, used in OpenStack
Other
91 stars 37 forks source link

Macro referenced before used issue #45

Open abastyr opened 4 years ago

abastyr commented 4 years ago

Airspeed throws an exception if the macro is referenced in the template before it is defined. This works normally in Apache Velocity.

Code example:

import airspeed

t = airspeed.Template("""
#set($value = 'my test')
${instancesuper}
#if(!$!{instancesuper})
missing end
#end
Value is '$value'
#new_macro('A','B')
#macro(new_macro $name $surname)
Your name is $name $surname
#end
Testing macro
#new_macro('A','B')"""
)

print(t.merge(locals()))

This throws a following exception:

raise Exception('no such macro: ' + self.macro_name)
airspeed.TemplateExecutionError: Error in template '<string>' at position 102-121 in expression: #new_macro('A','B')
Exception: no such macro: new_macro
purcell commented 4 years ago

Hmm, I suspect the fix for this could be fiddly. I agree that compatibility would be good here, but I guess it would be fairly trivial to move the macro declaration earlier in the source code in this case.

abastyr commented 4 years ago

Sort of. The problem is that the actual macro is massive and we put it in the back so it doesn't cover what the template really does. But I think I can write a piece of code that will put all the macros in front of the execution.