That was my problem exactly until I found this post that solved my problem.
Solution is very simple:
It would be nice to have more generic solution but this is simple and clean. Only problem is that modifications of code cannot be avoided if template changes, but on the other hand templates can be very complex. In that case take a look at the docx or flexdoc