rework util.template() a bit

as discussed in https://github.com/koreader/koreader/pull/1176