Completely rewritten. Removed foreach tag, added loop and raw tags