Dynamic routes by number template

Also take a look at this post. Different approach, but might be able to adapt to your needs.
Setting up Boss/Secretary using Dynamic Routes - FreePBX / Tips and Tricks - FreePBX Community Forums