Fix variable scoping issue in Mako template