Speed up template rendering, take 2

This speeds up _BlockTemplatesReplace (one of the bottlenecks in template
rendering) by about a factor of 3, and some template-heavy requests by 40%.

Instead of doing a global search&replace over the whole template for each
block, the whole template is split up into pieces, and re-assembled from these
data structures.

As a bonus it reduces the LOC count by ~30