cleaned up the code to pass query as part of the template locals instead of per page